Scenekit detecting User tapped object

后端 未结 3 678
遥遥无期
遥遥无期 2021-01-15 02:53

I recently started using scenekit for scenekit in iOS 8. I am facing difficulty in detecting whether the user has tapped or pressed on the object. Is there any way to do tha

相关标签:
3条回答
  • 2021-01-15 03:26

    Add a tap gesture to the object and check whether it is a SCNNode()

         @objc func tapGestureRec(sender: UIPanGestureRecognizer? = nil){
                let location: CGPoint = (sender?.location(in: self.view))!
                let hits = self.sceneKitView.hitTest(location, options: nil)
                if let tappedNode : SCNNode = hits.first?.node {
                    ...
                }
         }
    
    0 讨论(0)
  • 2021-01-15 03:41

    See the documentation for the hitTest method. Call that from wherever you're handling touch events to get a list of 3D scene objects/locations "under" a 2D screen point.

    0 讨论(0)
  • 2021-01-15 03:45

    An easy way to get sample code that shows the hitTest in action is to create a sample app using the Game template in XCode6. Create a new project, select the "Game" template.

    The hitTest code should be there in the implementation of:

    - (void) handleTap:(UIGestureRecognizer*)gestureRecognize
    
    0 讨论(0)
提交回复
热议问题