Swift:如何访问函数渲染器节点并通过 UI 操作保存

Swift: How to access function renderer node and save via UI action

我在看这个 post Face texture 来打印 x 和 y 顶点:

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard anchor == currentFaceAnchor,
            let contentNode = selectedContentController.contentNode,
            contentNode.parent == node
            else { return }
        let vertices = currentFaceAnchor!.geometry.vertices
        for (index, vertex) in vertices.enumerated() {
            let vertex = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
            let xVertex = CGFloat(vertex.x)
            let yVertex = CGFloat(vertex.y)
            let newPosition = CGPoint(x: xVertex, y: yVertex)
        print(newPosition)
        }
  }

我可以像这样打印 newPosition。例如:

(391.0479431152344, 577.9422607421875)
(379.6573791503906, 579.8109741210938)
(369.43145751953125, 583.1146240234375)
(362.12176513671875, 587.81787109375)...

目标是我想在 UIbutton 单击时保存 newPosition。

@IBAction private func stopPressed() {
    do {
        fpsTimer.invalidate() //turn off the timer
        let capturedData = captureData.map{[=14=].stringRepresentation}.joined(separator:"\(newPosition)>")
        let dir: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! as URL
        let url = dir.appendingPathComponent("testing.txt")
        try capturedData.appendLineToURL(fileURL: url as URL)
    }
    catch {
        print("Could not write to file")
    }
}

但我无法像在 captureData 中那样直接访问 newPosition:Use of unresolved identifier 'newPosition'

任何人都可以指导我如何访问渲染器并在 UIbutton 中被调用以便我可以保存顶点吗?

谢谢!

仅用这两段代码就很难提出一个好的解决方案。但是根据我收集到的信息 - 您想要使用按钮的新位置存储捕获的数据。

我的建议是在某种中间 属性 中捕获最后一个位置,例如:

    var lastPosition: CGPoint? 

    func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
        guard anchor == currentFaceAnchor,
            let contentNode = selectedContentController.contentNode,
            contentNode.parent == node
            else { return }
        let vertices = currentFaceAnchor!.geometry.vertices
        for (index, vertex) in vertices.enumerated() {
            let vertex = sceneView.projectPoint(node.convertPosition(SCNVector3(vertex), to: nil))
            let xVertex = CGFloat(vertex.x)
            let yVertex = CGFloat(vertex.y)
            self.newPosition = CGPoint(x: xVertex, y: yVertex)
        }
  }

那么当按钮不再被按下时你就可以使用它了。