CALayer响应点击事件
2021-03-31 本文已影响0人
雷霸龙
我们知道CALayer不能直接响应任何响应链事件,所以不能直接处理点击事件。但是依然有两种方法可以帮助我们实现捕捉并且处理CALayer的点击事件。
方法一、convertPoint:
@interface ViewController ()
@property (nonatomic, strong) CALayer *redLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.redLayer = [CALayer layer];
self.redLayer.frame = CGRectMake(100, 100, 100, 100);
self.redLayer.backgroundColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:self.redLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [[touches anyObject] locationInView:self.view];
CGPoint redPoint = [self.redLayer convertPoint:point fromLayer:self.view.layer];
if ([self.redLayer containsPoint:redPoint]) {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"红色" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
}
首先使用locationInView
方法获取到点击在view上的坐标。convertPoint: fromLayer :
方法传入一个CGPoint来转换坐标系,将在其父图层上的坐标转换为相对于图层自身的坐标,这样转换坐标系的方法还有以下几个:
- (CGPoint)convertPoint:(CGPoint)point fromLayer:(CALayer *)layer;
- (CGPoint)convertPoint:(CGPoint)point toLayer:(CALayer *)layer;
- (CGRect)convertRect:(CGRect)rect fromLayer:(CALayer *)layer;
- (CGRect)convertRect:(CGRect)rect toLayer:(CALayer *)layer;
得到触摸点相对于图层自身的坐标之后,调用containsPoint:
方法。containsPoint:
方法传入一个CGPoint类型参数,如果这个点在图层的frame内,则返回YES,否则返回NO。这样,就实现了对CALayer点击事件的处理。
方法二、hitTest:
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
CGPoint point = [[touches anyObject] locationInView:self.view];
CALayer *layer = [self.view.layer hitTest:point];
if (layer == self.redLayer) {
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"红色" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
}
hitTest:
同样传入一个CGPoint类型参数,但它的返回值不是BOOL类型,而是图层本身。如果点击的位置在最外层图层之外,则返回nil。
使用hitTest:
时有一点需要注意:hitTest:
返回的顺序严格按照图层树的图层顺序。