Let's say I have a view that I want to be draggable. Using just UIKit I would implement that with a variation of the following logic.
var viewStartY: CGFloat = 0
var panStartY: CGFloat = 0
func handlePan(recognizer: UIPanGestureRecognizer) {
var location = recognizer.locationInView(someView)
if recognizer.state == .Began {
viewStartY = someView.frame.origin.y
panStartY = location.y
}
let delta = location.y - panStartY
someView.frame.origin.y = viewStartY + delta.y
}
Now I was wondering if there was a way to deal with values like viewStartY that have to be stored when the gesture begins while avoiding side effects. Is there a way to continuously pass them through the pipeline?
FlatMap might work for you. Take a stream of beginning gestures, and flatMap a stream of changing gestures over it. This just gives you a stream of changes again, but you can capture the startValue inside the flatMapped function.
Pseudo-code that might explain the technique: