Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为自定义手势识别器实现VelocityView?_Ios_Objective C_Uigesturerecognizer_Subclass - Fatal编程技术网

Ios 如何为自定义手势识别器实现VelocityView?

Ios 如何为自定义手势识别器实现VelocityView?,ios,objective-c,uigesturerecognizer,subclass,Ios,Objective C,Uigesturerecognizer,Subclass,我正在实现一个自定义的uigesturecognizer子类 我想实现VelocityView:与UIPangestureRecognitizer的实现方式相同。但我不知道该怎么做。如何以点/秒为单位计算速度?首先,如果使用Swift,则需要创建桥接标头并导入,以便覆盖UIGestureRegoniser的触摸开始/移动/取消/结束方法。如果您使用的是Objective-C,只需将import语句放在.h或.m文件中即可 其次,一旦创建了UIgestureRecognitor子类,您需要在其中包

我正在实现一个自定义的
uigesturecognizer
子类


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

首先,如果使用Swift,则需要创建桥接标头并导入
,以便覆盖
UIGestureRegoniser
的触摸开始/移动/取消/结束方法。如果您使用的是Objective-C,只需将
import
语句放在.h或.m文件中即可

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

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?
显示链接用于更新
currentTime
previousTime

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

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
}
使用以下方程式计算速度:

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

let distance = hypot(targetCurrLocation.x - targetPrevLocation.x, 
                     targetCurrLocation.y - targetPrevLocation.y)
let velocity = distance / deltaTime
第五,在视图控制器中,您现在可以添加手势识别器,并使用
操作
在屏幕上拖动时获得速度:

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)")
}

不用swift,但没关系,我会翻译。
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)")
}