If you have a look at the documentation you will see that an ARImageAnchor
conforms to the ARTrackable Protocol
which:
is adopted by ARKit classes, such as the ARFaceAnchor class, that
represent moving objects in a scene.
ARSCNView and ARSKView automatically hide the nodes for anchors whose
isTracked property is false.
ARKit automatically manages representations of such objects in an
active AR session, ensuring that changes in the real-world object's
position and orientation (the transform property for anchors) are
reflected in corresponding ARKit objects. The isTracked property
indicates whether the current transform is valid with respect to
movement of the real-world object.
As such you can detect if your ARImageAnchor is currently being tracked using something like this as a starter:
//--------------------------
//MARK: - ARSessionDelegate
//--------------------------
extension ViewController: ARSessionDelegate{
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
//1. Enumerate Our Anchors To See If We Have Found Our Target Anchor
for anchor in anchors{
if let imageAnchor = anchor as? ARImageAnchor, imageAnchor == videoAnchor{
//2. If The ImageAnchor Is No Longer Tracked Then Handle The Event
if !imageAnchor.isTracked{
}else{
}
}
}
}
}
Whereby videoAnchor
is simply an ARImageAnchor
I have stored reference to as a Global Variable
.
Alternatively you if you want to cancel an operation when a particular SCNNode
is outside of the Frostrum
of the camera you can do something like so:
/// Stops All VideoPlayers Outside Of Frostrum
///
/// - Parameter currentVideoNode: SCNNode
func stopAllVideoPlayersOutsideOfPointOfView(){
//a. Get The Current Point Of You & Our SCNScene
guard let currentPointOfView = self.augmentedRealityView?.pointOfView,
let scnView = self.augmentedRealityView else { return }
//b. Loop Through All The Hierachy
if let childNodes = self.augmentedRealityView?.scene.rootNode.childNodes{
childNodes.forEach { (node) in
//c. If Our Node Isn't In View Of The Camera Then Stop The Video
if !scnView.isNode(node, insideFrustumOf: currentPointOfView){
}
}
}
}
Hope it helps...