/** Hit testing methods. **/
/* Returns the farthest descendant of the layer containing point 'p'.
* Siblings are searched in top-to-bottom order. 'p' is defined to be
* in the coordinate space of the receiver's nearest ancestor that
* isn't a CATransformLayer (transform layers don't have a 2D
* coordinate space in which the point could be specified). */
- (nullable __kindof CALayer *)hitTest:(CGPoint)p;
/* Returns true if the bounds of the layer contains point 'p'. */
- (BOOL)containsPoint:(CGPoint)p;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
CALayer *layer = [self.view.layer hitTest:touchPoint];
//点击了图层(有可能为nil)
NSLog(@"%@",layer);
}

屏幕快照 2019-04-24 下午11.34.43.png

屏幕快照 2019-04-24 下午11.35.28.png
网友评论