Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import UIKit
  2.  
  3. class AnimatorVC: UIViewController {
  4. @IBOutlet weak var stackView: UIStackView!
  5.  
  6. init() {
  7. super.init(nibName: "AnimatorVC", bundle: nil)
  8. }
  9.  
  10. required init?(coder aDecoder: NSCoder) {
  11. fatalError("init(coder:) has not been implemented")
  12. }
  13.  
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16.  
  17. _ = (0..<5).map {
  18. let progressView = UIProgressView()
  19. progressView.tag = $0
  20. stackView.addArrangedSubview(progressView)
  21. }
  22. }
  23.  
  24. var animator: UIViewPropertyAnimator = UIViewPropertyAnimator(duration: 5.0, curve: .easeInOut)
  25. var currentIndex = 0
  26.  
  27.  
  28. @IBAction func reset(_ sender: Any) {
  29. currentIndex = 0
  30. stackView.arrangedSubviews.forEach {
  31. if let progressView = $0 as? UIProgressView {
  32. progressView.setProgress(0, animated: false)
  33. }
  34. }
  35. }
  36.  
  37. @IBAction func start(_ sender: Any) {
  38. print(#function, animator.state, animator.state.rawValue)
  39.  
  40. animator.addAnimations {
  41. self.stackView.arrangedSubviews.forEach {
  42. if let progressView = $0 as? UIProgressView {
  43. if progressView.tag == self.currentIndex {
  44. progressView.setProgress(1.0, animated: true)
  45. }
  46.  
  47. }
  48. }
  49. }
  50. animator.addCompletion { _ in
  51. self.complete()
  52. }
  53. animator.startAnimation()
  54. }
  55.  
  56. private func complete() {
  57. animator.stopAnimation(true)
  58.  
  59. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
  60. guard let self = self else { return }
  61. if self.currentIndex == 4 {
  62. self.reset(true)
  63. } else {
  64. self.currentIndex += 1
  65. self.start(true)
  66. }
  67. }
  68. }
  69.  
  70. @IBAction func pause(_ sender: Any) {
  71. }
  72.  
  73.  
  74. @IBAction func stop(_ sender: Any) {
  75. }
  76.  
  77. @IBAction func next(_ sender: Any) {
  78. print(#function, animator.state, animator.state.rawValue)
  79.  
  80. animator.stopAnimation(true)
  81.  
  82. currentIndex += 1
  83.  
  84. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
  85. self?.start(sender)
  86. }
  87. }
  88.  
  89. @IBAction func prev(_ sender: Any) {
  90. animator.stopAnimation(true)
  91.  
  92. currentIndex -= 1
  93.  
  94. stackView.arrangedSubviews.forEach {
  95. if let progressView = $0 as? UIProgressView {
  96. let progress: Float = progressView.tag < currentIndex ? 1.0 : 0
  97. progressView.setProgress(progress, animated: false)
  98. }
  99. }
  100.  
  101. DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
  102. self?.start(sender)
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement