Skip to content

Commit

Permalink
Fix playground crash by creating a fresh reversed animation as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
freak4pc committed Apr 5, 2018
1 parent 1d3ed22 commit 0433c25
Showing 1 changed file with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,30 @@ class AnimateViewController: UIViewController {
return button
}()

lazy var animator1 = {
UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) { [unowned self] in
self.box1.transform = CGAffineTransform(translationX: 0, y: -100)
var animator1: UIViewPropertyAnimator!
var animator2: UIViewPropertyAnimator!
var animator3: UIViewPropertyAnimator!

private func makeAnimators() {
animator1 = UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) { [unowned self] in
self.box1.transform = self.box1.transform != .identity ? .identity
: self.box1.transform.translatedBy(x: 0, y: -100)
}
}()

lazy var animator2 = {
UIViewPropertyAnimator(duration: 0.25, curve: .easeInOut) { [unowned self] in
self.box2.transform = CGAffineTransform(translationX: 0, y: -100)
.scaledBy(x: 1.2, y: 1.2)
animator2 = UIViewPropertyAnimator(duration: 0.25, curve: .easeInOut) { [unowned self] in
self.box2.transform = self.box2.transform != .identity ? .identity
: self.box2.transform
.translatedBy(x: 0, y: -100)
.scaledBy(x: 1.2, y: 1.2)
}
}()

lazy var animator3 = {
UIViewPropertyAnimator(duration: 0.15, curve: .easeInOut) { [unowned self] in
self.box3.transform = CGAffineTransform(translationX: 0, y: -100)
.rotated(by: CGFloat(M_PI))
animator3 = UIViewPropertyAnimator(duration: 0.15, curve: .easeInOut) { [unowned self] in
self.box3.transform = self.box3.transform != .identity ? .identity
: self.box3.transform
.translatedBy(x: 0, y: -100)
.rotated(by: .pi)
}
}()
}

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -87,12 +92,17 @@ class AnimateViewController: UIViewController {
view.addSubview($0)
}

makeAnimators()

// Trigger chained animations after a button tap
button.rx.tap
.flatMap { [unowned self] in
self.animator1.rx.animate()
.andThen(self.animator2.rx.animate(afterDelay: 0.2))
.andThen(self.animator3.rx.animate(afterDelay: 0.1))
.andThen(self.animator2.rx.animate())
.andThen(self.animator3.rx.animate())
.do(onCompleted: {
self.makeAnimators()
})
.debug("animation sequence")
}
.subscribe()
Expand Down

0 comments on commit 0433c25

Please sign in to comment.