Don_Mag icon

JCB WasserhaushaltViewController

Don_Mag | PRO | 09/02/20 09:38:20 PM UTC (Edited) | 0 ⭐ | 3784 👁️ | Never ⏰ | []
Swift |

4.69 KB

|

None

|

0 👍

/

0 👎

//
//  WasserhaushaltViewController.swift
//  deSynthTheOceans
//
//  Created by robinsonhus0 on 24.03.20.
//  Copyright © 2020 robinsonhus0. All rights reserved.
//
 
import UIKit
import AVFoundation
import Charts
import FSCalendar
import HealthKit
 
 
struct WasserSpeicher: Codable {
    let wassermenge: Double
    let speicherdatum: String
    let speicherStelle: Double
}
 
class WasserhaushaltViewController: UIViewController, UIScrollViewDelegate {
    @IBOutlet weak var diagrammView: UIView!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var pageControl: UIPageControl!
    
    let drinksImagesArray = ["tapWater", "water", "milk", "cola", "coffee", "tea", "juice", "beer"]
 
    var imageIndex = Int()
    
    struct Drinks {
        var name: String
        var tagesMengeFactor: Double
        var gesamtMengeFactor: Double
    }
    
    var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
    var pageNumber = CGFloat()
    
    @IBOutlet weak var todaysWaterConsumptionLabel: UILabel!
    @IBOutlet weak var waterGoalProgress: UIProgressView!
    @IBOutlet weak var waterGoalLabel: UILabel!
    @IBOutlet weak var wasserMengeStepper: UIStepper!
    @IBOutlet weak var motivationTextView: UITextView!
    @IBOutlet weak var wasserglasButton: UIBarButtonItem!
    @IBOutlet weak var kleineFlascheButton: UIBarButtonItem!
    @IBOutlet weak var grosseFlascheButton: UIBarButtonItem!
    @IBOutlet weak var overAllWaterConsumptionLabel: UILabel!
    
    // added
    let scrollingImagesStackView = UIStackView()
    var stackHeightConstraint: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        pageControl.numberOfPages = drinksImagesArray.count
        setupDrinkImages()
    }
    
    
    func setupDrinkImages() {
        // set stack view properties
        scrollingImagesStackView.axis = .horizontal
        scrollingImagesStackView.alignment = .fill
        scrollingImagesStackView.distribution = .fillEqually
        scrollingImagesStackView.spacing = 0
        
        // add the stack view to the scroll view
        scrollingImagesStackView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(scrollingImagesStackView)
        
        // use scroll view's contentLayoutGuide for content constraints
        let svCLG = scrollView.contentLayoutGuide
        
        NSLayoutConstraint.activate([
            
            // stack view constrained Top / Bottom / Leading / Trailing of scroll view CONTENT guide
            scrollingImagesStackView.topAnchor.constraint(equalTo: svCLG.topAnchor),
            scrollingImagesStackView.bottomAnchor.constraint(equalTo: svCLG.bottomAnchor),
            scrollingImagesStackView.leadingAnchor.constraint(equalTo: svCLG.leadingAnchor),
            scrollingImagesStackView.trailingAnchor.constraint(equalTo: svCLG.trailingAnchor),
            
        ])
        
        // create the stack view height constraint - it will be updated in viewDidLayoutSubviews
        stackHeightConstraint = scrollingImagesStackView.heightAnchor.constraint(equalToConstant: 0)
        stackHeightConstraint.isActive = true
        
        // create image views and add them to the stack view
        drinksImagesArray.forEach { imgName in
            let v = UIImageView()
            v.backgroundColor = .orange
            v.contentMode = .scaleAspectFit
            // make sure we load a valid image
            if let img = UIImage(named: imgName) {
                v.image = img
            }
            scrollingImagesStackView.addArrangedSubview(v)
        }
        
        // stack distribution is set to .fillEqually, so we only need to set the
        // width constraint on the first image view
        
        // unwrap it
        if let firstImageView = scrollingImagesStackView.arrangedSubviews.first {
            firstImageView.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor).isActive = true
        }
 
        scrollView.delegate = self
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        // since we have a UINavigationBar and a UIToolBar in the view hierarchy,
        //  we need to set this here
        //  Note: if the view size changes
        // stack view height == scroll view FRAME height
        stackHeightConstraint.constant = scrollView.frame.height
        
    }
    
//  func setupDrinkImages() {
//      for index in 0..<drinksImagesArray.count {
//          frame.origin.x = scrollView.frame.size.width * CGFloat(index)
//          frame.size = scrollView.frame.size
//
//          let imageView = UIImageView(frame: frame)
//          imageView.contentMode = .scaleAspectFit
//          imageView.image = UIImage(named: drinksImagesArray[index])
//          self.scrollView.addSubview(imageView)
//      }
//      scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(drinksImagesArray.count), height: scrollView.frame.size.height)
//      scrollView.delegate = self
//  }
    
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
        pageControl.currentPage = Int(pageNumber)
    }
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎