简体   繁体   中英

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

    @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).x / 60 - For horizontal

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

Hence, you can update the position just by adding it with your initial value like this:

var initialLocation: CGPoint? -> Global

initialLocation.x = (initialLocation ?? 0)

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

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

Thanks."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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