• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 14:19:38 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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



Best Answer-推荐答案


首先,如果您使用的是 Swift,您将需要创建一个桥接头和 #import 以便您可以覆盖 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap