我在游乐场创建一个场景并使用hitTest(func hitTest(_ point:CGPoint,options:[SCNHitTestOption:Any]?= nil) - > [SCNHitTestResult])函数来确定其中一个节点是否被触摸过,但只有在触摸节点时才会导致模拟器在操场上崩溃。使用xcode的iOS项目中的相同代码不会发生这种情况。
func panGesture(sender: UIPanGestureRecognizer) {
sender.view
let translation = sender.translation(in: sender.view!)
var newAngleX = (Float)(translation.y)*(Float)(M_PI)/180.0
newAngleX += currentXAngle
var newAngleY = (Float)(translation.x)*(Float)(M_PI)/180.0
newAngleY += currentYAngle
if (sender.numberOfTouches>0){
var point = sender.location(in: self)
print(point)
let hit = self.hitTest(point, options: nil)
let node = hit.first?.node
node?.eulerAngles.x = newAngleX
node?.eulerAngles.y = newAngleY
if(sender.state == UIGestureRecognizerState.ended) {
currentXAngle = newAngleX
currentYAngle = newAngleY
}
}
}
答案 0 :(得分:0)
这在游乐场真的非常非常慢......但也许还有别的东西在继续。无论如何,如果你用这个替换你的func panGesture(sender: UIPanGestureRecognizer)
,至少应该让你在Playground中运行它而不会崩溃。
func panGesture(sender: UIPanGestureRecognizer) {
sender.view
let translation = sender.translation(in: sender.view!)
var newAngleX = (Float)(translation.y)*(Float)(M_PI)/180.0
newAngleX += currentXAngle
var newAngleY = (Float)(translation.x)*(Float)(M_PI)/180.0
newAngleY += currentYAngle
let point = sender.location(in: self)
print(point)
// important
guard let hit = self.hitTest(point, options: nil) as? [SCNHitTestResult] else { return }
// and, don't go in here if hit.count == 0, because it won't have a .first
if (sender.numberOfTouches > 0 && hit.count > 0){
let node = hit.first?.node
node?.eulerAngles.x = newAngleX
node?.eulerAngles.y = newAngleY
if(sender.state == UIGestureRecognizerState.ended) {
currentXAngle = newAngleX
currentYAngle = newAngleY
}
}
}