当触摸下半部分时,图层命中测试仅返回图层

Cor*_*oyd 4 iphone cocoa-touch core-animation

我在图层支持的视图上有一个子图层.子图层的内容设置为图像参考,并且是25x25矩形.
当调用touchesBegan和touchesMoved方法时,我在超级层上执行命中测试.实际上,命中测试方法确实在触摸时返回子层,但仅在触摸图像的下半部分时才返回子层.如果触摸图像的上半部分,则返回超级图层.

我知道iPhone OS可以补偿用户触摸的趋势低于预期.即使我将子图层调整为更大的尺寸(50x50),它也表现出相同的行为.

有什么想法吗?

sch*_*hwa 11

hitTest的文档说:

/* Returns the farthest descendant of the layer containing point 'p'.
 * Siblings are searched in top-to-bottom order. 'p' is in the
 * coordinate system of the receiver's superlayer. */
Run Code Online (Sandbox Code Playgroud)

所以你需要做(像这样):

CGPoint thePoint = [touch locationInView:self];
thePoint = [self.layer convertPoint:thePoint toLayer:self.layer.superlayer];
CALayer *theLayer = [self.layer hitTest:thePoint];
Run Code Online (Sandbox Code Playgroud)

(为完整起见,重复其他帖子的回答)