图是这样的,我上面蓝色的是一个CALayer,白的的是个UIView
@property (weak, nonatomic) IBOutlet UIView *white;
@property (strong, nonatomic) CALayer *blueLayer;
_blueLayer = [CALayer layer];
_blueLayer.frame = CGRectMake(50, 50, 100, 100);
_blueLayer.backgroundColor = [UIColor blueColor].CGColor;
[_white.layer addSublayer:_blueLayer];
图1.1.png
通过简单的hitTesting来获取点击的是哪一个视图
首先通过CGPoint point = [[touches anyObject] locationInView:self.view];
获取到点击的点的位置,然后使用CALayer *layer = [self.view.layer hitTest:point];
获取当点点击的点属于哪一个CALayer,通过判断即可获取到当前点击的是哪一个CALayer或者View
if (layer == self.blueLayer){
//do something
} else if (layer == self.white.layer) {
//do something
}
效果图.gif
网友评论