Advertisement
Don_Mag

JCB WasserhaushaltViewController

Sep 2nd, 2020 (edited)
3,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.69 KB | None | 0 0
  1. //
  2. //  WasserhaushaltViewController.swift
  3. //  deSynthTheOceans
  4. //
  5. //  Created by robinsonhus0 on 24.03.20.
  6. //  Copyright © 2020 robinsonhus0. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import AVFoundation
  11. import Charts
  12. import FSCalendar
  13. import HealthKit
  14.  
  15.  
  16. struct WasserSpeicher: Codable {
  17.     let wassermenge: Double
  18.     let speicherdatum: String
  19.     let speicherStelle: Double
  20. }
  21.  
  22. class WasserhaushaltViewController: UIViewController, UIScrollViewDelegate {
  23.     @IBOutlet weak var diagrammView: UIView!
  24.     @IBOutlet weak var scrollView: UIScrollView!
  25.     @IBOutlet weak var pageControl: UIPageControl!
  26.    
  27.     let drinksImagesArray = ["tapWater", "water", "milk", "cola", "coffee", "tea", "juice", "beer"]
  28.  
  29.     var imageIndex = Int()
  30.    
  31.     struct Drinks {
  32.         var name: String
  33.         var tagesMengeFactor: Double
  34.         var gesamtMengeFactor: Double
  35.     }
  36.    
  37.     var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
  38.     var pageNumber = CGFloat()
  39.    
  40.     @IBOutlet weak var todaysWaterConsumptionLabel: UILabel!
  41.     @IBOutlet weak var waterGoalProgress: UIProgressView!
  42.     @IBOutlet weak var waterGoalLabel: UILabel!
  43.     @IBOutlet weak var wasserMengeStepper: UIStepper!
  44.     @IBOutlet weak var motivationTextView: UITextView!
  45.     @IBOutlet weak var wasserglasButton: UIBarButtonItem!
  46.     @IBOutlet weak var kleineFlascheButton: UIBarButtonItem!
  47.     @IBOutlet weak var grosseFlascheButton: UIBarButtonItem!
  48.     @IBOutlet weak var overAllWaterConsumptionLabel: UILabel!
  49.    
  50.     // added
  51.     let scrollingImagesStackView = UIStackView()
  52.     var stackHeightConstraint: NSLayoutConstraint!
  53.    
  54.     override func viewDidLoad() {
  55.         super.viewDidLoad()
  56.        
  57.         pageControl.numberOfPages = drinksImagesArray.count
  58.         setupDrinkImages()
  59.     }
  60.    
  61.    
  62.     func setupDrinkImages() {
  63.         // set stack view properties
  64.         scrollingImagesStackView.axis = .horizontal
  65.         scrollingImagesStackView.alignment = .fill
  66.         scrollingImagesStackView.distribution = .fillEqually
  67.         scrollingImagesStackView.spacing = 0
  68.        
  69.         // add the stack view to the scroll view
  70.         scrollingImagesStackView.translatesAutoresizingMaskIntoConstraints = false
  71.         scrollView.addSubview(scrollingImagesStackView)
  72.        
  73.         // use scroll view's contentLayoutGuide for content constraints
  74.         let svCLG = scrollView.contentLayoutGuide
  75.        
  76.         NSLayoutConstraint.activate([
  77.            
  78.             // stack view constrained Top / Bottom / Leading / Trailing of scroll view CONTENT guide
  79.             scrollingImagesStackView.topAnchor.constraint(equalTo: svCLG.topAnchor),
  80.             scrollingImagesStackView.bottomAnchor.constraint(equalTo: svCLG.bottomAnchor),
  81.             scrollingImagesStackView.leadingAnchor.constraint(equalTo: svCLG.leadingAnchor),
  82.             scrollingImagesStackView.trailingAnchor.constraint(equalTo: svCLG.trailingAnchor),
  83.            
  84.         ])
  85.        
  86.         // create the stack view height constraint - it will be updated in viewDidLayoutSubviews
  87.         stackHeightConstraint = scrollingImagesStackView.heightAnchor.constraint(equalToConstant: 0)
  88.         stackHeightConstraint.isActive = true
  89.        
  90.         // create image views and add them to the stack view
  91.         drinksImagesArray.forEach { imgName in
  92.             let v = UIImageView()
  93.             v.backgroundColor = .orange
  94.             v.contentMode = .scaleAspectFit
  95.             // make sure we load a valid image
  96.             if let img = UIImage(named: imgName) {
  97.                 v.image = img
  98.             }
  99.             scrollingImagesStackView.addArrangedSubview(v)
  100.         }
  101.        
  102.         // stack distribution is set to .fillEqually, so we only need to set the
  103.         // width constraint on the first image view
  104.        
  105.         // unwrap it
  106.         if let firstImageView = scrollingImagesStackView.arrangedSubviews.first {
  107.             firstImageView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor).isActive = true
  108.         }
  109.  
  110.         scrollView.delegate = self
  111.     }
  112.    
  113.     override func viewDidLayoutSubviews() {
  114.         super.viewDidLayoutSubviews()
  115.        
  116.         // since we have a UINavigationBar and a UIToolBar in the view hierarchy,
  117.         //  we need to set this here
  118.         //  Note: if the view size changes
  119.         // stack view height == scroll view FRAME height
  120.         stackHeightConstraint.constant = scrollView.frame.height
  121.        
  122.     }
  123.    
  124. //  func setupDrinkImages() {
  125. //      for index in 0..<drinksImagesArray.count {
  126. //          frame.origin.x = scrollView.frame.size.width * CGFloat(index)
  127. //          frame.size = scrollView.frame.size
  128. //
  129. //          let imageView = UIImageView(frame: frame)
  130. //          imageView.contentMode = .scaleAspectFit
  131. //          imageView.image = UIImage(named: drinksImagesArray[index])
  132. //          self.scrollView.addSubview(imageView)
  133. //      }
  134. //      scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(drinksImagesArray.count), height: scrollView.frame.size.height)
  135. //      scrollView.delegate = self
  136. //  }
  137.    
  138.     func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
  139.         pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
  140.         pageControl.currentPage = Int(pageNumber)
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement