为什么必须使用CALayer的presentationLayer进行命中测试? [英] Why does one have to use CALayer's presentationLayer for hit-testing?
问题描述
我正在处理Cocoa Touch应用程序,并尝试通过确定视图图层的哪个子图层被触摸来处理触摸。我的控制器代码看起来像这样:
I was working on a Cocoa Touch application, and trying to handle touches by determining which sublayer of a view's layer was touched. My controller's code looked like this:
CALayer *hitLayer = [self.view.layer hitTest:point];
这不工作。它会工作,如果我轻拍在底层的底层,但不是在顶部。经过一个小时的拉发和谷歌,我终于弄清楚,如果一个人使用表示层来做击打测试,而不是图层本身:
This wasn't working. It would work if I tapped at the bottoms of sublayers, but not at the tops. After an hour of hair-pulling and Googling, I finally figured out that it works if one uses the presentation layer to do the hit-testing instead of the layer itself:
CALayer *hitLayer = [self.view.layer.presentationLayer hitTest:point];
所以,我已经解决了我的问题,但我的问题是:为什么?
So, I've solved my problem, but my question is: Why?
我已经阅读了Core Animation指南,并且我理解,演示树和渲染树可能与对象模型树不同。但我不明白为什么表示树(显然)有不同的命中测试行为。
I've read through the Core Animation guide, and I understand that the presentation tree and rendering tree can differ from the object-model tree. But I don't understand why the presentation tree would (apparently) have different hit-testing behavior. Wouldn't the presentation tree and object-model have the same frames, bounds, etc.?
推荐答案
根据 CALayer文档,似乎有一个关于呈现给用户的什么(取决于正在进行的动画)的模型和呈现树之间的差异。对于点击测试, presentationLayer
方法中有一个引用:
According to the CALayer documentation, there seems to be a difference between the model and the presentation tree regarding what is presented to the user (depending on the ongoing animation). For hit testing, there is a reference in the presentationLayer
method:
,发送hitTest:
消息到presentationLayer将
查询
层树的表示值。
For example, sending a hitTest: message to the presentationLayer will query the presentation values of the layer tree.
所以我怀疑只有表示层有正确的几何信息来执行命中测试。
So I suspect that only presentation layer has the right geometry information to perform the hit test.
这篇关于为什么必须使用CALayer的presentationLayer进行命中测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!