使用ReactiveCocoa实现Pan Gesture界面

时间:2015-06-19 10:56:07

标签: ios uipangesturerecognizer reactive-cocoa frp

假设我有一个想要拖延的观点。使用UIKit,我将使用以下逻辑的变体来实现它。

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
}

现在我想知道是否有办法处理像viewStartY这样的值,这些值必须在手势开始时存储,同时避免副作用。有没有办法让它们不断通过管道?

1 个答案:

答案 0 :(得分:1)

FlatMap可能适合您。采取一系列开始的手势,并在平面上绘制一系列不断变化的手势。这只会再次为您提供一组更改,但您可以捕获flatMapped函数中的startValue。

可能解释该技术的伪代码:

let gestures = recognizer.stream // Depends on your framework

// Filter gesture events by state
let gesture_begans = gestures.filter { $0.state == .Began }
let gesture_changeds = gestures.filter { $0.state == .Changed }

// Take a stream of beginning gestures...
let gesture_deltas = gesture_begans.flatMap { (began) -> in
    let startPoint = began.locationInView(outerView)

    // ... and flatMap into a 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 }