Created
May 31, 2016 20:10
-
-
Save dangthaison91/8dc2ae412a675af322a376884f3f896e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReactiveShortViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let pan = UIPanGestureRecognizer() | |
pan.delegate = self | |
let rotate = UIRotationGestureRecognizer() | |
rotate.delegate = self | |
view.gestureRecognizers = [pan, rotate] | |
let panStarted = pan.rx_event.map { $0.state == .Began }.filter { $0 } | |
let panEnded = pan.rx_event.map { $0.state == .Ended }.filter { $0 } | |
let rotateStarted = rotate.rx_event.map { $0.state == .Began }.filter { $0 } | |
let rotateEnded = rotate.rx_event.map { $0.state == .Ended }.filter { $0 } | |
// condition: when both pan and pinch started & ended | |
let bothGesturesStarted = Observable.zip(panStarted, rotateStarted) { ($0 && $1) } | |
let bothGesturesEnded = Observable.of(panEnded, rotateEnded).merge() | |
// Action | |
bothGesturesStarted.subscribeNext { _ in | |
// create a timer that ticks every second, until 3 or until pan and pinch ended | |
let timer = Observable<Int> | |
.timer(repeatEvery: 1) | |
.take(3) | |
.takeUntil(bothGesturesEnded) | |
timer.subscribe(onNext: { count in | |
print("tick: \(count)") | |
}, onCompleted: { | |
print("completed") | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment