Getting details about the pinch gesture in ARKit

时间:2018-02-03 08:22:47

标签: ios swift scenekit augmented-reality arkit

In my project I have a pinch to resize option for the object that has been placed in scene view. But when someone pinch the screen to reduce or enlarge the actual size of the object I need to get that scale. I need to display the scale in which the object is being changed in the screen. How do I get the scale when the action is being performed?

Thank you

1 个答案:

答案 0 :(得分:3)

在ARSCNView的主ViewController类中

声明标签视图,标签本身位于顶部。

let scaleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 300, 70))
let labelView = UIView(frame: CGRect(x: 300, y: 300, width: 300, height: 70))

现在,在LoadView或ViewDidLoad中,您可以设置标签的属性,如backgroundColor,textColor等...还可以将视图和标签添加到sceneView。

// add your attributes for label,view

labelView.backgroundColor = .clear
scaleLabel.textColor = .white
scaleLabel.adjustsFontSizeToFitWidth

// add you views to sceneView

labelView.addSubview(scaleLabel)
sceneView.addSubview(labelView)

最后,使用缩放手势功能进行缩放..这看起来应该是这样的。

@objc func pinchGesture(_ gesture: UIPinchGestureRecognizer) {

 if nodeYouScale != nil {

   let action = SCNAction.scale(by: gesture.scale, duration: 0.1)
   nodeYouScale.runAction(action)
   gesture.scale = 1 

    // this part updates the label with the current scale factor 

 scaleLabel.text = "X: \(nodeYouScale.scale.x) Y: \(nodeYouScale.scale.y) Z:\(nodeYouScale.scale.z)"

  } else {
    return
}