[英]func hitTest declared in SceneKit causes crash in Playground
I create a scene in playground and use hitTest (func hitTest(_ point: CGPoint, options: [SCNHitTestOption : Any]? = nil) -> [SCNHitTestResult]) function to determine if one of the nodes has been touched, but it causes a crash of simulator in playground only when nodes are touched. 我在操场上创建了一个场景,并使用hitTest(func hitTest(_ point:CGPoint,options:[SCNHitTestOption:Any]?= nil)-> [SCNHitTestResult])函数来确定是否已触摸其中一个节点,但这会导致仅在触摸节点时,模拟器才会在操场上崩溃。 This does not happen with the same code on iOS project with xcode. 使用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
}
}
}
This runs really, really, really slow in Playground... but maybe there's something else going on. 这在Playground中的运行速度非常,非常,非常慢……但是也许还有其他事情在发生。 Anyway, if you replace your func panGesture(sender: UIPanGestureRecognizer)
with this, it should at least let you run it in the Playground without crashing. 无论如何,如果您用它替换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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.