我刚刚发现 Scene Kit 有一个非常奇怪的行为。我创建了一个干净的项目来查看是否会发生这种情况,它确实发生了。考虑以下 GameViewController
类:
import UIKit
import QuartzCore
import SceneKit
class GameViewController: UIViewController, SCNPhysicsContactDelegate {
func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) {
let label = UILabel(frame: CGRectMake(0, 0, 100, 100))
label.textColor = .whiteColor()
label.text = "Test"
self.view.addSubview(label)
}
override func viewDidLoad() {
// Controller
super.viewDidLoad()
// Scene
let scene = SCNScene()
scene.physicsWorld.contactDelegate = self
// Scene View
let scnView = self.view as! SCNView
scnView.scene = scene
// Camera
let camera = SCNCamera()
camera.usesOrthographicProjection = true
camera.orthographicScale = 8
// Camera Node
let cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: -3, y: 7.5, z: 10)
cameraNode.eulerAngles = SCNVector3(x: -0.5, y: -0.5, z: 0)
scene.rootNode.addChildNode(cameraNode)
// Light
let light = SCNLight()
light.type = SCNLightTypeDirectional
// Light Node
let lightNode = SCNNode()
lightNode.light = light
lightNode.eulerAngles = SCNVector3Make(-1, -0.5, 0)
scene.rootNode.addChildNode(lightNode)
// Box Shape
let geometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0)
geometry.materials.first?.diffuse.contents = UIColor(red: 255, green: 255, blue: 0, alpha: 1)
// Upper Box
let box = SCNNode(geometry: geometry)
box.physicsBody = SCNPhysicsBody(type: .Dynamic, shape: nil)
box.physicsBody?.categoryBitMask = 1
box.physicsBody?.contactTestBitMask = 2
scene.rootNode.addChildNode(box)
// Bottom Box
let box2 = SCNNode(geometry: geometry)
box2.position.y -= 5
box2.physicsBody = SCNPhysicsBody(type: .Static, shape: nil)
box2.physicsBody?.categoryBitMask = 2
box2.physicsBody?.contactTestBitMask = 1
box2.physicsBody?.affectedByGravity = false
scene.rootNode.addChildNode(box2)
}
}
这里发生的是,我们创建一个 SCNScene
,然后我们添加相机/闪电并创建两个将发生碰撞的盒子。我们还将 Controller 设置为物理接触委托(delegate)的委托(delegate)。
但是重要的代码在最上面,当两个对象发生接触的时候。我们创建一个 UILabel
并将其放置在屏幕的左上角。但问题是:标签显示大约需要 7 秒。而且这一次似乎总是如此。这看起来很奇怪,但您可以自己尝试:只需创建一个 SceneKit 项目(使用 Swift),然后将 Controller 代码替换为我提供的代码。
补充说明:如果在physicsWorld()
函数中添加print("a")
,它会立即运行,所以代码是在右边运行时间,但由于某些原因 UILabel
没有立即显示。
最佳答案
起初我以为你的场景在接触后没有被渲染;因此不显示标签,所以放入 scnView.playing = true
行。但是,这导致标签永远不会显示。
接下来的想法是,您正试图在线程上做一些您不应该做的事情。修改 UIKit View 实际上只能在主线程上完成,并且不清楚哪个线程调用了 SCNPhysicsContactDelegate
函数。鉴于确保它确实在主线程上运行相对容易...
func physicsWorld(world: SCNPhysicsWorld, didBeginContact contact: SCNPhysicsContact) {
dispatch_async(dispatch_get_main_queue()) {
let label = UILabel(frame: CGRectMake(0, 0, 100, 100))
label.textColor = .whiteColor()
label.text = "Test"
self.view.addSubview(label)
}
}
这为我解决了延迟问题,也适用于 scnView.playing = true
。
关于ios - SceneKit:两个节点之间接触后 UIView 加载缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38449508/