SceneKit IOS的​​平滑捏手势

时间:2018-01-30 00:18:42

标签: ios swift arkit

我正在为Arkit的3D模型制作Pinch Gesture,我遇到了一个问题,当我缩放模型时,它会滞后并且不会给我平滑的响应。它工作正常,但我需要让它更顺畅。

 @objc func scalePiece(gestureRecognizer : UIPinchGestureRecognizer) {   guard gestureRecognizer.view != nil else { return }

        if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {

            let scale = Float(gestureRecognizer.scale)

            let newscalex = scale / currentscalex
            let newscaley = scale / currentscaley
            let newscalez = scale / currentscalez

            self.drone.scale = SCNVector3(newscalex, newscaley, newscalez)

        }}

任何提示将不胜感激。

2 个答案:

答案 0 :(得分:3)

您可能在currentscalenewscale后面有一些逻辑,但只需gestureRecognizer.scale即可进行平滑缩放。我认为你会因为你直接设置scale而得到不稳定的缩放。使用SCNAction.scale

let action = SCNAction.scale(by: gestureRecognizer.scale, duration: 0.1)
lv.runAction(action)
gestureRecognizer.scale = 1

而不是

self.drone.scale = SCNVector3(newscalex, newscaley, newscalez)

如果x,y,z的比例值不同,则使用customAction

let action = SCNAction.customAction(duration: 0.1) { (yourNode, elapsedTime) in
    let percentage = elapsedTime / 0.1
    let newscalex = scale / currentscalex * percentage
    let newscaley = scale / currentscaley * percentage
    let newscalez = scale / currentscalez * percentage
    yourNode.scale = SCNVector3(newscalex, newscaley, newscalez)
}

答案 1 :(得分:0)

替换所有三个轴的/ by *并重置手势识别器的比例。换句话说,乘以比例而不是除以比例。

要防止它以指数方式缩放,请在读取后将gestureRecognizer.scale重置为1.0。例如:

  if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
            let scale = Float(gestureRecognizer.scale)

            let newscalex = scale * self.drone.scale.x
            let newscaley = scale * self.drone.scale.y
            let newscalez = scale * self.drone.scale.z

            self.drone.scale = SCNVector3(newscalex, newscaley, newscalez)
            gestureRecognizer.scale = 1.0
   }

重置gestureRecognizer基本上是一种替代方法,用于将原始比例存储在开始状态,并在将比例因子分配给当前之前将比例因子应用于识别器。这种情况在重要说明中的文档:https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_pinch_gestures中有具体描述。