简体   繁体   English

快速改变手势速度

[英]Change pangesture speed swift

Is it possible to slow down pan gesture, to make it move with less speed.是否可以减慢平移手势,使其以较低的速度移动。

Here's the code I'm using to move a 3d object这是我用来移动 3d 对象的代码

    @objc func handlePan(gestureRecognize: UIPanGestureRecognizer) {

    let numberOfTouches = gestureRecognize.numberOfTouches
    let translation = gestureRecognize.translation(in: gestureRecognize.view!)
    var widthRatio = Float(translation.x) / Float(gestureRecognize.view!.frame.size.width) - lastWidthRatio

    if (numberOfTouches == fingersNeededToPan) {
        //  WIDTH constraints
        if(widthRatio >= maxWidthRatioRight) {
            widthRatio = maxWidthRatioRight
        }
        if(widthRatio <= maxWidthRatioLeft) {
            widthRatio = maxWidthRatioLeft
        }

        self.artRoomScene.boxnode.eulerAngles.y = Float(2 * Double.pi) * widthRatio
        //for final check on fingers number
        lastFingersNumber = fingersNeededToPan
    }

    lastFingersNumber = (numberOfTouches>0 ? numberOfTouches : lastFingersNumber)

    if (gestureRecognize.state == .ended && lastFingersNumber==fingersNeededToPan) {
        lastWidthRatio = widthRatio
    }
}

Access the velocity of a pan gesture 访问平移手势的速度

"sender.velocityInView(self.view) gives you the velocity in pixels per second. In order to get the exact velocity, you need to change the velocity to pixels per minute by dividing it by 60 like this: "sender.velocityInView(self.view) 为您提供每秒像素的速度。为了获得准确的速度,您需要将速度除以 60 将速度更改为每分钟像素,如下所示:

sender.velocityInView(self.view).x / 60 - For horizontal sender.velocityInView(self.view).x / 60 - 水平

sender.velocityInView(self.view).y / 60 - For vertical sender.velocityInView(self.view).y / 60 - 垂直

Hence, you can update the position just by adding it with your initial value like this:因此,您可以通过添加初始值来更新位置,如下所示:

var initialLocation: CGPoint? var initialLocation: CGPoint? -> Global -> 全球

initialLocation.x = (initialLocation ?? 0) initialLocation.x = (initialLocation ?? 0)

(initialLocation.x)! (initialLocation.x)! = (initialLocation.x)! = (initialLocation.x)! + (sender.velocity(in: colorSlider!).x / 60) - For horizontal + (sender.velocity(in: colorSlider!).x / 60) - 水平

(initialLocation.y)! (initialLocation.y)! = (initialLocation.y)! = (initialLocation.y)! + (sender.velocity(in: colorSlider!).y / 60) - For vertical + (sender.velocity(in: colorSlider!).y / 60) - 垂直

Thanks."谢谢。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM