如何实现velocityInView:自定义手势识别器?

时间:2015-02-16 23:54:30

标签: ios objective-c uigesturerecognizer subclass

我正在实现自定义UIGestureRecognizer子类。

我希望以velocityInView:完成UIPanGestureRecognizer的方式实施{{1}}。但我不知道如何去做。如何以pts / sec计算速度?

1 个答案:

答案 0 :(得分:2)

首先,如果您使用的是Swift,则需要创建一个桥接标题和#import <UIKit/UIGestureRecognizerSubclass.h>,以便您可以覆盖UIGestureRegoniser的触摸开始/移动/取消/结束方法。如果您正在使用Objective-C,请将该import语句放在.h或.m文件中。

其次,一旦你创建了UIGestureRecogniser子类,你需要这些变量:

private lazy var displayLink: CADisplayLink = {
    let link = CADisplayLink(target: self, selector: Selector("timerCalled"))
    link.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSRunLoopCommonModes)
    return link
}()

private var previousTouchLocation: CGPoint?
private var currentTouchLocation : CGPoint?

private var previousTime: Double?
private var currentTime : Double?

显示链接用于更新currentTimepreviousTime

第三,继续设置一切你需要覆盖以下方法,我认为这些都是相当自我解释的:

override func touchesBegan(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    self.state = .Began
    displayLink.paused = false
}

override func touchesMoved(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    if let view = self.view,
        touch = touches.first as? UITouch {
        self.state = .Changed

        let newLocation = touch.locationInView(view)
        self.previousTouchLocation = self.currentTouchLocation
        self.currentTouchLocation = newLocation
    }
}

override func touchesEnded(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    self.state = .Ended
    displayLink.paused = true
}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    self.state = .Cancelled
    displayLink.paused = true
}

第四,现在我们可以得到更有趣的一点 - 计算速度:

func velocityInView(targetView: UIView) -> CGPoint {
    var velocity = CGPoint.zeroPoint

    if let view = self.view,
        prevTouchLocation = self.previousTouchLocation,
        currTouchLocation = self.currentTouchLocation,
        previousTime = self.previousTime,
        currentTime  = self.currentTime {

        let targetPrevLocation = view.convertPoint(prevTouchLocation, toView: targetView)
        let targetCurrLocation = view.convertPoint(currTouchLocation, toView: targetView)

        let deltaTime = CGFloat(currentTime - previousTime)

        let velocityX = (targetCurrLocation.x - targetPrevLocation.x) / deltaTime
        let velocityY = (targetCurrLocation.y - targetPrevLocation.y) / deltaTime

        velocity = CGPoint(x: velocityX, y: velocityY)
    }

    return velocity
}

使用以下等式计算速度:

velocity = deltaDistance / deltaTime

在这种情况下,取速度(x和y)的每个分量,并使用该等式计算每个轴的速度。如果你想要结合速度的两个组成部分,你可以像这样使用毕达哥拉斯定理:

let distance = hypot(targetCurrLocation.x - targetPrevLocation.x, 
                     targetCurrLocation.y - targetPrevLocation.y)
let velocity = distance / deltaTime

第五,在视图控制器中,您现在可以添加手势识别器,并使用action在屏幕上拖动时获取速度:

override func viewDidLoad() {
    super.viewDidLoad()

    let recogniser = VelocityGestureRecogniser(target: self, action: Selector("movedGesture:"))
    self.view.addGestureRecognizer(recogniser)
}

func movedGesture(recogniser: VelocityGestureRecogniser) {
    let v = recogniser.velocityInView(self.view)
    println("velocity = \(v)")
}