gpt4 book ai didi

ios - 如何实现 velocityInView : for a custom gesture recognizer?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:31 26 4
gpt4 key购买 nike

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

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

最佳答案

首先,如果您使用的是 Swift,则需要创建桥接 header 和 #import <UIKit/UIGestureRecognizerSubclass.h>所以你可以覆盖 UIGestureRegoniser的触摸开始/移动/取消/结束方法。如果您使用的是 Objective-C,只需输入 import .h 或 .m 文件中的语句。

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

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

第五,在您的 View Controller 中,您现在可以添加手势识别器并使用 action在屏幕上拖动时获得速度:

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

关于ios - 如何实现 velocityInView : for a custom gesture recognizer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28552408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com