本系列文章是对<3D Apple Games by Tutorials>一书的学习记录和体会node
此书对应的代码地址git
SceneKit系列文章目录github
在平时开发中经常使用的touchesBegan方法在3D中仍然可用. 只不过在3D空间内采用了射线检测方法来返回触摸到的物体.swift
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)
}
}
复制代码