ios - 我如何实现velocityInView : for a custom gesture recognizer?
<p><p>我正在实现一个自定义 <code>UIGestureRecognizer</code> 子类。 </p>
<p>我想以与 <code>UIPanGestureRecognizer</code> 相同的方式实现 <code>velocityInView:</code>。但我不知道该怎么做。如何计算以点/秒为单位的速度?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>首先,</strong>如果您使用的是 Swift,您将需要创建一个桥接头和 <code>#import <UIKit/UIGestureRecognizerSubclass.h></code> 以便您可以覆盖 <code>UIGestureRegoniser</code> 的触摸开始/移动/取消/结束方法。如果您使用的是 Objective-C,只需将该 <code>import</code> 语句放在 .h 或 .m 文件中。</p>
<p><strong>其次,</strong> 创建 <code>UIGestureRecognizer</code> 子类后,您需要在其中包含以下变量:</p>
<pre><code>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?
</code></pre>
<p>显示链接用于更新<code>currentTime</code>和<code>previousTime</code>。</p>
<p><strong>第三,</strong>要继续设置你需要覆盖以下方法的所有内容,我认为这一切都相当不言自明:</p>
<pre><code>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
}
</code></pre>
<p><strong>第四,</strong>现在我们可以进入更有趣的部分——计算速度:</p>
<pre><code>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
}
</code></pre>
<p>使用以下公式计算速度:</p>
<pre><code>velocity = deltaDistance / deltaTime
</code></pre>
<p>在这种情况下,获取速度的每个分量(x 和 y)并使用该方程计算每个轴上的速度。如果你想结合速度的两个分量,你可以像这样使用毕达哥拉斯定理:</p>
<pre><code>let distance = hypot(targetCurrLocation.x - targetPrevLocation.x,
targetCurrLocation.y - targetPrevLocation.y)
let velocity = distance / deltaTime
</code></pre>
<p><strong>第五,</strong>现在您可以在 ViewController 中添加手势识别器并使用 <code>action</code> 来获取在屏幕上拖动时的速度:</p>
<pre><code>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)")
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 我如何实现velocityInView : for a custom gesture recognizer?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28552408/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28552408/
</a>
</p>
页:
[1]