CALayer 并不关心任何的响应链事件,所以也不能直接处理触摸事件或者手势;但是它有俩个方法帮你处理事件
-containsPoint
和hitTest
-containtPoint
- 接收一个本图层坐标系下的
CGPoint
,如果这个点在图层的frame
范围内返回YES
123456789101112131415161718192021222324252627- (void)viewDidLoad {[super viewDidLoad];self.blueLayer = [CALayer layer];self.blueLayer.frame = CGRectMake(50, 50, 100, 100);self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;[self.layerView.layer addSublayer:self.blueLayer];}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{//使用containsPoint方法CGPoint point = [[touches anyObject] locationInView:self.view];point = [self.layerView.layer convertPoint:point fromLayer:self.view.layer];if ([self.layerView.layer containsPoint:point]) {point = [self.blueLayer convertPoint:point fromLayer:self.layerView.layer];if ([self.blueLayer containsPoint:point]) {NSLog(@"蓝色");}else{NSLog(@"白色");}}else{NSLog(@"灰色");}}
hitTest
- 同样接收一个
CGPoint
类型的参数,返回图层本身
|
|