Skip to content

Commit

Permalink
Prevent the demo from crashing
Browse files Browse the repository at this point in the history
The button is now disabled when animation is completed
  • Loading branch information
twittemb committed Apr 4, 2018
1 parent e78ac14 commit 5b8e6a9
Showing 1 changed file with 21 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*:
> # IMPORTANT: To use `RxSwiftExtPlayground.playground`, please:

1. Make sure you have [Carthage](https://github.com/Carthage/Carthage) installed
1. Fetch Carthage dependencies from shell: `carthage bootstrap --platform ios`
1. Build scheme `RxSwiftExtPlayground` scheme for a simulator target
Expand All @@ -17,82 +17,85 @@ import UIKit

/*:
## animate

The `animate` operator provides a Completable that triggers the animation upon subscription and completes when the animation ends.

Please open the Assistant Editor (⌘⌥⏎) to see the Interactive Live View example.
*/

class AnimateViewController: UIViewController {

let disposeBag = DisposeBag()

lazy var box1: UIView = {
let view = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
view.backgroundColor = .red
return view
}()

lazy var box2: UIView = {
let view = UIView(frame: CGRect(x: 100, y: 220, width: 100, height: 100))
view.backgroundColor = .green
return view
}()

lazy var box3: UIView = {
let view = UIView(frame: CGRect(x: 100, y: 340, width: 100, height: 100))
view.backgroundColor = .blue
return view
}()

lazy var button: UIButton = {
let button = UIButton(frame: CGRect(x: 100, y: 500, width: 200, height: 50))
button.setTitle("Play animation", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.backgroundColor = .white
button.layer.borderColor = UIColor.black.cgColor
button.layer.borderWidth = 2

return button
}()

lazy var animator1 = {
UIViewPropertyAnimator(duration: 0.3, curve: .easeInOut) { [unowned self] in
self.box1.transform = CGAffineTransform(translationX: 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)
.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))
.rotated(by: CGFloat(M_PI))
}
}()

override func viewDidLoad() {
super.viewDidLoad()

// construct the main view
let views = [box1, box2, box3, button]
view.backgroundColor = .white

views.forEach {
view.addSubview($0)
}

// 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))
.do(onCompleted: { [unowned self] in
self.button.isEnabled = false
})
.debug("animation sequence")
}
.subscribe()
Expand Down

0 comments on commit 5b8e6a9

Please sign in to comment.