Skip to content

Instantly share code, notes, and snippets.

@dangthaison91
Created May 31, 2016 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dangthaison91/8dc2ae412a675af322a376884f3f896e to your computer and use it in GitHub Desktop.
Save dangthaison91/8dc2ae412a675af322a376884f3f896e to your computer and use it in GitHub Desktop.
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