CALayer----事件传递
事件的产生和传递
- 触摸后,系统会传递触摸到UIApplication管理的事件队列
- UIApplication从队列取事件,并分发处理(通常会发给程序的KeyWindow)
- KeyWindow依据视图层次找到合适的视图处理触摸事件
- 拿到合适的视图控件后,会调用控件的touches方法做具体事件处理
无法交互事件
用户交互 == NO,hiden == YES ,alpha >> 0/0.1
UIImageView 本身用户交互是 == NO,其上的所有子视图都是无交互事件的
父控件不能接收触摸事件,其子控件也是无法接受触摸事件的
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageV;
@property (weak, nonatomic) IBOutlet UIView *clipView;
//@property (nonatomic,strong) TestLable *label;
@property (nonatomic,strong) CALayer *colorLayer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.colorLayer = [CALayer layer];
self.colorLayer.frame = CGRectMake(50.0f, 50.0f, 100.0f, 100.0f);
self.colorLayer.backgroundColor = [UIColor redColor].CGColor;
[self.clipView.layer addSublayer:self.colorLayer];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGPoint point = [[touches anyObject] locationInView:self.view];
CALayer *laye = [self.clipView.layer hitTest:point];
if (laye == self.colorLayer) {
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"红红红" message:@"点了红" preferredStyle:UIAlertActionStyleDefault];
UIAlertAction *cat = [UIAlertAction actionWithTitle:@"hahah" style:UIAlertControllerStyleAlert handler:nil ];
[ac addAction:cat];
[self presentViewController:ac animated:YES completion:nil];
NSLog(@"hong");
}
}
-
效果图: