文章选自掘金苹果API搬运工的文章[SceneKit专题]5-3D空间的触摸事件
主要记录自己在学习ARKit的过程中看到的好的文章,避免到时候链接失效无法找到原文的情况,非常感谢原博主的辛勤付出,也在此分享出来跟大家一起学习。
在平时开发中常用的touchesBegan方法在3D中仍然可用. 只不过在3D空间内采用了射线检测方法来返回触摸到的物体.
![](https://img.haomeiwen.com/i1521229/b3175e13c5527af8.png)
当有触摸事件发生时:
- 拿到用户触摸在屏幕上的位置.
- 转换到SCNView的坐标系中.
- 当触摸点在SCNView上时,发射一个射线,返回与该射线相交的一系列物体.
override func touchesBegan(touches: Set<UITouch>, withEvent event:
UIEvent?) {
// 1 拿到触摸对象
let touch = touches.first!
// 2 转换坐标系
let location = touch.locationInView(scnView)
// 3 执行hitTest,发射射线,返回相交的物体
let hitResults = scnView.hitTest(location, options: nil)
// 4
if hitResults.count > 0 {
// 5 取出最近的物体
let result = hitResults.first!
// 6 处理该节点
handleTouchFor(result.node)
}
}
网友评论