Created
September 29, 2019 20:38
-
-
Save hungtruong/1166ad11e921cb21470feed64ffb9e3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func setupWorkout(_ workout: Workout) { | |
stackView.arrangedSubviews.forEach { view in | |
view.removeFromSuperview() | |
stackView.removeArrangedSubview(view) | |
} | |
workout.workoutSegments.forEach { workoutSegment in | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
let barView = UIView() | |
barView.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(barView) | |
let heightMultiplier = workoutSegment.highPower() / workout.maximumPower() | |
barView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: CGFloat(heightMultiplier)).isActive = true | |
stackView.addArrangedSubview(view) | |
let multiplier = workoutSegment.duration / workout.totalTime | |
view.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: CGFloat(multiplier)).isActive = true | |
barView.backgroundColor = workoutSegment.color() | |
barView.bottomAnchor.constraint(equalTo: barView.superview!.bottomAnchor).isActive = true | |
barView.leadingAnchor.constraint(equalTo: barView.superview!.leadingAnchor).isActive = true | |
barView.trailingAnchor.constraint(equalTo: barView.superview!.trailingAnchor, constant: -1).isActive = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment