首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为自定义手势识别器实现velocityInView:?

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

Stack Overflow用户
提问于 2015-02-16 23:54:30
回答 1查看 1.1K关注 0票数 2

我正在实现一个定制的UIGestureRecognizer子类。

我想用与velocityInView:相同的方式来实现UIPanGestureRecognizer。但我不知道该怎么做。如何以点/秒为单位计算速度?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-06 20:25:13

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

其次,一旦创建了UIGestureRecognizer子类,就需要在其中包含以下变量:

代码语言:javascript
复制
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

第三,继续设置您需要覆盖以下方法所需的一切,我认为这都是不言自明的:

代码语言:javascript
复制
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
}

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

代码语言:javascript
复制
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
}

速度是用以下公式计算的:

代码语言:javascript
复制
velocity = deltaDistance / deltaTime

在这种情况下,取速度的每个分量(x和y),并使用这个方程来计算每个轴上的速度。如果你想把速度的两个成分结合起来,你可以使用毕达哥拉斯定理,如下所示:

代码语言:javascript
复制
let distance = hypot(targetCurrLocation.x - targetPrevLocation.x, 
                     targetCurrLocation.y - targetPrevLocation.y)
let velocity = distance / deltaTime

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

代码语言:javascript
复制
override func viewDidLoad() {
    super.viewDidLoad()

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

func movedGesture(recognizer: VelocityGestureRecognizer) {
    let v = recognizer.velocityInView(self.view)
    println("velocity = \(v)")
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28552408

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档