gpt4 book ai didi

ios - SceneKit 中声明的 func hitTest 导致 Playground 崩溃

转载 作者:行者123 更新时间:2023-11-30 12:35:15 27 4
gpt4 key购买 nike

我在 Playground 中创建一个场景,并使用 hitTest (func hitTest(_ point: CGPoint, options: [SCNHitTestOption : Any]? = nil) -> [SCNHitTestResult]) 函数来确定是否已触摸其中一个节点,但只有当节点被触摸时,它才会导致 Playground 中模拟器崩溃。使用 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
}

}

}

最佳答案

这在 Playground 中运行得真的、真的、真的很慢……但也许还有其他原因。无论如何,如果你用这个替换你的 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
}

}

}

关于ios - SceneKit 中声明的 func hitTest 导致 Playground 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960892/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com