CALayer上的hitTest - 你如何找到被击中的实际图层? [英] hitTest on CALayer - how do you find which actual layer was hit?

查看:164
本文介绍了CALayer上的hitTest - 你如何找到被击中的实际图层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:需要找到用户触摸的图层。



问题:Apple说我们应该使用[CALayer presentationLayer]进行命中测试,以便它表示当时屏幕上实际显示的内容(它捕捉动画中期等信息)。



...除了:presentationLayer不返回原始图层,它返回副本他们......所以:hitTest将返回一个全新的CALayer实例,该实例与原版不同。



我们如何找到实际的CALayer被击中? / p>

例如

  CALayer * x = [CALayer layer]; 
CALayer * y = [CALayer图层];
[self.view.layer addSublayer:x];
[self.view.layer addSublayer:y];

...

CALayer * touchingLayer = [self.view.layer.presentationLayer hitTest:touchPoint];

...但是,touchLayerx,还是y?

  if(touchingLayer == x)//这不起作用,因为来自Apple的touchLayer是 - 一个新对象


解决方案

啊!刚想通了这个问题,在CALayer的另一个问题上阅读邮件列表帖子。



调用[CALayer presentationLayer]后,使用图层的演示克隆 -tree,你可以在该树中获取任何对象,并在其上调用[CALayer modelLayer]以返回原始树中相同位置的原始参考对象



这个参考是稳定的(测试 - 它可以工作)。



Apple的文档有点......模糊......在这一个。他们暗示它有时会失败( ...结果未定义) - 但现在对我来说已经足够了。


Situation: need to find which layer the user has touched.

Problem: Apple says we should use [CALayer presentationLayer] to do hit-testing, so that it represents what is actually on screen at the time (it captures info mid-animation etc).

...except: presentationLayer does NOT return the original layers, it returns copies of them ... so: the hitTest will return a brand new CALayer instance that is not equivalent to the original.

How do we find which actual CALayer was hit?

e.g.

CALayer* x = [CALayer layer];
CALayer* y = [CALayer layer];
[self.view.layer addSublayer: x];
[self.view.layer addSublayer: y];

...

CALayer* touchedLayer = [self.view.layer.presentationLayer hitTest:touchPoint];

...but, is touchedLayer "x", or is it "y"?

if( touchedLayer == x ) // this won't work, because touchedLayer is - by definition from Apple - a new object

解决方案

Ah! Just figured this out, reading a mailing list post on a different problem with CALayer.

After calling [CALayer presentationLayer], and working with the "presentation clone" of the layer-tree, you can take any object in that tree, and call [CALayer modelLayer] on it to get back the original reference object at the same position in the original tree.

This reference is stable (tested - it works).

Apple's docs are a bit ... obscure ... on this one. And they imply it will "sometimes" fail ("...results are undefined") - but it's good enough for me for now.

这篇关于CALayer上的hitTest - 你如何找到被击中的实际图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆