ARKit –使用“ UIPinchGestureRecognizer”放大对象

时间:2018-08-21 14:14:45

标签: ios swift sprite-kit scenekit arkit

我想放大对象。我要缩放的位置应该居中。

是这样的: 1.我放大。 2.修改对象比例。 3.我缩放的点没有居中,因为对象只是缩放而不变形,并且停留在同一位置。

有什么建议吗?

我试图通过以下方式解决该问题:获得进行手势的点,然后将比例尺应用于此点,然后使用这些点以一种获取转换向量的方式将其应用于SCNNode ,但无效。以下是失败之一:

@objc fileprivate func handlePinch(_ gesture: UIPinchGestureRecognizer) {

    guard let terrain = terrain else {
        return
    }
    if gesture.state == UIGestureRecognizerState.began {
        startScale = terrain.scale.x
    }
    guard let startScale = startScale else {
        return
    }

    let identity = CGAffineTransform.identity

    // zoom point and screen center
    let point = gesture.location(in: self.sceneView)

    // 1. scale
    let newScale: Float = startScale * Float(gesture.scale)
    terrain.scale = SCNVector3(newScale, newScale, newScale)
    if gesture.state == .ended {
        self.startScale = nil
    }

    let pointScaled = point.applying(identity.scaledBy(x: gesture.scale, y:             gesture.scale)) 

    let vector: SCNVector3 = SCNVector3Make(Float(pointScaled.x - point.x), Float(pointScaled.y - point.y), 1)
    terrain.position += vector
}

0 个答案:

没有答案