Xcode描述说:
如果图层尚未提交,则返回nil。
这在字面意义上意味着什么?
我的代码是这样的:
guard animalView?.layer.presentationLayer() != nil else {
return QL4("Animal presentation layer was nil \(animalView?.layer)")
}
QL4打印日志,因此我们从用户那里收到的一些日志如下。我似乎无法重现这一点,偶尔也会发生这种情况。所以我的问题是presentationLayer()返回nil的真正原因是什么?我最好的猜测是设备没有足够的可用功率来立即执行此任务,所以它会延迟一点点吗?
动物表示层是nil可选(;动画= {position =; position-2 =;}; layer =&gt ;; sublayers =(,< _UILabelLayer:0x172147d0>); allowsGroupOpacity = YES ;; animations = [position = position-2 =]>)
动物表示层是nil可选(;动画= {position =; position-2 =;}; layer =&gt ;; sublayers =(,< _UILabelLayer:0x174289ce0>); allowsGroupOpacity = YES ;; animations = [position = position-2 =]>)
动物表示层是nil可选(;动画= {position =; position-2 =;}; layer =&gt ;; sublayers =(,< _UILabelLayer:0x170292d40>); allowsGroupOpacity = YES ;; animations = [position = position-2 =]>)
完整代码:
animalVC = AnimalVC()
VCOrganizer.mainVCInstance.addAsChildViewController(animalVC!, toView: VCOrganizer.mainVCInstance.view)
animalView = createAnimalView(animalItem!, x: 0, y: 0, sideSize: sideSize)
class AnimalVC: UIViewController {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
touchLocation = touch!.locationInView(self.view)
guard animalView != nil else {
return QL4("Animal was nil \(animalView)")
}
guard animalView?.layer.presentationLayer() != nil else {
return QL4("Animal presentation layer was nil \(animalView?.layer)")
}
if animalView!.layer.presentationLayer()!.hitTest(touchLocation) != nil {
// QL1("Clicked animal")
clickedAnimal()
} else {
// QL1("Clicked on background")
}
}
}