Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class AnimatorVC: UIViewController {
- @IBOutlet weak var stackView: UIStackView!
- init() {
- super.init(nibName: "AnimatorVC", bundle: nil)
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- _ = (0..<5).map {
- let progressView = UIProgressView()
- progressView.tag = $0
- stackView.addArrangedSubview(progressView)
- }
- }
- var animator: UIViewPropertyAnimator = UIViewPropertyAnimator(duration: 5.0, curve: .easeInOut)
- var currentIndex = 0
- @IBAction func reset(_ sender: Any) {
- currentIndex = 0
- stackView.arrangedSubviews.forEach {
- if let progressView = $0 as? UIProgressView {
- progressView.setProgress(0, animated: false)
- }
- }
- }
- @IBAction func start(_ sender: Any) {
- print(#function, animator.state, animator.state.rawValue)
- animator.addAnimations {
- self.stackView.arrangedSubviews.forEach {
- if let progressView = $0 as? UIProgressView {
- if progressView.tag == self.currentIndex {
- progressView.setProgress(1.0, animated: true)
- }
- }
- }
- }
- animator.addCompletion { _ in
- self.complete()
- }
- animator.startAnimation()
- }
- private func complete() {
- animator.stopAnimation(true)
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
- guard let self = self else { return }
- if self.currentIndex == 4 {
- self.reset(true)
- } else {
- self.currentIndex += 1
- self.start(true)
- }
- }
- }
- @IBAction func pause(_ sender: Any) {
- }
- @IBAction func stop(_ sender: Any) {
- }
- @IBAction func next(_ sender: Any) {
- print(#function, animator.state, animator.state.rawValue)
- animator.stopAnimation(true)
- currentIndex += 1
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
- self?.start(sender)
- }
- }
- @IBAction func prev(_ sender: Any) {
- animator.stopAnimation(true)
- currentIndex -= 1
- stackView.arrangedSubviews.forEach {
- if let progressView = $0 as? UIProgressView {
- let progress: Float = progressView.tag < currentIndex ? 1.0 : 0
- progressView.setProgress(progress, animated: false)
- }
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
- self?.start(sender)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement