Cesare
Cesare

Reputation: 9419

How to animate from node in ARKit to actual view

I would like to get the rectangular position in the scene view of a tapped node so that I can use that position to animate another UIView from that position to a bigger size (exactly like in the Measure app):

enter image description here

I get the tapped node with this code, though how do I get the rect or the dimensions or whatever to make the smooth transition from the node to the view?

public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesBegan(touches, with: event)
    guard let touch = touches.first else { return }        
    if (touch.view == self.sceneView) {
        let viewTouchLocation:CGPoint = touch.location(in: sceneView)
        guard let result = sceneView.hitTest(viewTouchLocation, options: nil).first else {
            return
        }
    }
}

I've looked into using hitTest or smartHitTest on the scene view given the touch position but I can't really seem to be able to get the size/position of the smaller view.

Upvotes: 2

Views: 334

Answers (1)

Alok Subedi
Alok Subedi

Reputation: 1611

Not exactly your answer but try it out: (edited to send it back)

var front = false
var boxView = UIView()

@objc func tap(_ sender: UITapGestureRecognizer){
    if !front{
        let position = sender.location(in: sceneView)
        let hitTest = sceneView.hitTest(position, options: nil)
        if let hitNode = hitTest.first?.node{

            self.boxView = UIView(frame: CGRect(x: position.x, y: position.y, width: 100, height: 40))
            self.boxView.backgroundColor = .red
            sceneView.addSubview(self.boxView)
            UIView.animate(withDuration: 0.2) {
                self.boxView.frame = CGRect(x: self.sceneView.frame.width/2 - 150, y: self.sceneView.frame.height/2 - 100, width: 300, height: 200)
            }
            front = true
        }
    }else{
        let p = plane.convertPosition(plane.position, to: sceneView.scene.rootNode)
        print(p, plane.position) // see difference between p and plane.position

        let projectedPoint = sceneView.projectPoint(p)
        let point = CGPoint(x: CGFloat(projectedPoint.x), y: CGFloat(projectedPoint.y))

        UIView.animate(withDuration: 0.2, animations: {
            self.boxView.frame = CGRect(x: point.x, y: point.y, width: 100, height: 40)
        }) { (completed) in
            self.boxView.removeFromSuperview()
        }
        front = false
    }
}

plane here is node in my test case. You might want to look about convertPosition and projectPoint

Upvotes: 1

Related Questions