如何从touchesMoved方法获取准确的CGpoint以便在SceneView上绘制UIBezierpath?

时间:2019-05-07 05:48:38

标签: ios scenekit arkit uibezierpath swift4.2

UIBezierpath在现实世界中的其他地方显示

从下面的代码收集CGPoint

 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let location = touches.first?.location(in: self.sceneView) else {
            return
        }
        arrayCGPoints.append(location)
    }

&使用以下代码显示Bazierpath

func updateBazierPath(){        

        if arrayCGPoints.count > 0 {

            let bezierPath = UIBezierPath()

            var i : Int = 0
            for varPoint in arrayCGPoints{

                if i == 0 {
                    bezierPath.move(to: varPoint)
                }
                else{
                    bezierPath.addLine(to: varPoint)
                }
                i += 1
            }
            bezierPath.close()
        }
        // Add shape
        let shape = SCNShape(path: bezierPath, extrusionDepth: 0.2)
        let shapeNode = SCNNode(geometry: shape)
    self.sceneView.scene.rootNode.addChildNode(shapeNode)

        shapeNode.geometry?.firstMaterial?.isDoubleSided = true
        shapeNode.geometry!.firstMaterial?.diffuse.contents = UIColor.blue
        shapeNode.position.z = -0.2
        shapeNode.eulerAngles.x = -.pi / 2
    }

还尝试将CGPoints转换为米

arrayCGPoints.append(CGPoint(x: location.x/100, y: location.y/100))

仍然无法正常工作

1 个答案:

答案 0 :(得分:1)

这将在@ 0,0,0处绘制一个节点,其中概述了该人从中拖动的内容,将屏幕坐标转换为世界。我创建了一个单独的SCNVector3数组,它们是3d空间中的坐标(您可以在这些点上在屏幕上放置一些节点以证明位置)。还要注意,附件是x,z,z(不是scenepoint.y)。我的相机从0,15,0.1看0,0,0。这至少可以使您直观地了解z的情况,并希望能够回答您提出的问题。

如果您希望生成的形状与用户触摸的位置保持一致,则此处无法解决。

var shapeNode = SCNNode()

func updateBazierPath()
{
    for vPoints in arrayCGPoints
    {
        let projectedPoint = gameScene.projectPoint(SCNVector3(0, 0, 0))
        let scenePoint = gameScene.unprojectPoint(SCNVector3(vPoints.x, vPoints.y, CGFloat(projectedPoint.z)))
        scenePoints.append(SCNVector3(scenePoint.x, scenePoint.z, scenePoint.z))
    }
    let bezierPath = UIBezierPath()
    bezierPath.move(to: CGPoint(x: CGFloat(scenePoints[0].x), y: CGFloat(scenePoints[0].y)))
    for vCount in 1...scenePoints.count - 1
    {
        bezierPath.addLine(to: CGPoint(x: CGFloat(scenePoints[vCount].x), y: CGFloat(scenePoints[vCount].y)))
    }
    bezierPath.close()

    shapeNode.removeFromParentNode()
    let shape = SCNShape(path: bezierPath, extrusionDepth: 0.5)
    shapeNode = SCNNode(geometry: shape)
    shapeNode.geometry?.firstMaterial?.isDoubleSided = true
    shapeNode.geometry!.firstMaterial?.diffuse.contents = UIColor.yellow
    //let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(1, 0, 0), duration: 5))
    //shapeNode.runAction(action)
    shapeNode.position = SCNVector3(0, 0, 0)
    shapeNode.eulerAngles.x = -.pi / 2
    gNodes.gameNodes.addChildNode(shapeNode)
}