我使用SpriteKit图像识别。 SpriteKit正常工作,我可以识别图像。识别后,我在该图片上添加了一个视频。当摄像机改变方向并且不在videoNode中时,如何删除视频?

简单的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    guard let referenceImages = ARReferenceImage.referenceImages(inGroupNamed: "Room", bundle: nil) else {
        fatalError("Missing expected asset catalog resources.")
    }

    let configuration = ARWorldTrackingConfiguration()
    configuration.detectionImages = referenceImages

    sceneView.session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

}



func view(_ view: ARSKView, didAdd node: SKNode, for anchor: ARAnchor) {

    if let imageAnchor = anchor as? ARImageAnchor,
        self.myImage == imageAnchor.referenceImage {
        node.addChild(self.addNode())
    }
}

private func addNode () -> SKVideoNode {
    let video = SKVideoNode(url: URL(string: "http://techslides.com/demos/sample-videos/small.mp4")!)
    video.size = CGSize(width: 40, height: 40)
    video.position = CGPoint(x: 0, y: 0)
    video.play()
    return video
}

最佳答案

如果要检查videoNode是否在摄像机中,请执行camera.contains(videoNode)

基本上在移动相机时(无论何时移动相机)都可以:

if !scene.camera.contains(videoNode){
    videoNode.pause()
}

关于ios - 不在摄像机视野内时停止播放SKVideoNode,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51422008/

10-11 00:37