ios - Implement Pan Gesture Interface Using ReactiveCocoa -
let's have view want draggable. using uikit implement variation of 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 wondering if there way deal values viewstarty have stored when gesture begins while avoiding side effects. there way continuously pass them through pipeline?
flatmap might work you. take stream of beginning gestures, , flatmap stream of changing gestures on it. gives stream of changes again, can capture startvalue inside flatmapped function.
pseudo-code might explain technique:
let gestures = recognizer.stream // depends on framework // filter gesture events state let gesture_begans = gestures.filter { $0.state == .began } let gesture_changeds = gestures.filter { $0.state == .changed } // take stream of beginning gestures... let gesture_deltas = gesture_begans.flatmap { (began) -> in let startpoint = began.locationinview(outerview) // ... , flatmap stream of deltas return gesture_changeds.map { (changed) in let changedpoint = changed.locationinview(outerview) let delta = changedpoint.y - startpoint.y return delta } } gesture_deltas.onvalue { y in innerview.frame.origin.y = y }
Comments
Post a Comment