Skip to content

Instantly share code, notes, and snippets.

@hungtruong
Created September 29, 2019 20:38
Show Gist options
  • Save hungtruong/1166ad11e921cb21470feed64ffb9e3f to your computer and use it in GitHub Desktop.
Save hungtruong/1166ad11e921cb21470feed64ffb9e3f to your computer and use it in GitHub Desktop.
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