【iOS】经过给UIView添加手势以达到点击UILayer触发点击事件

当给UIView添加Animation动画时,项目须要添加点击事件。
可是使用UIButton无效,不响应点击事件。
baidu / google 之。
发现UILayer不响应事件。
换一种思路,发现能够给整个视图添加点击手势,而后判断点击位置来触发事件。动画

代码片断

//建立手势添加到视图上
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
[self.view addGestureRecognizer:self.tapGesture];

#pragma mark - 点击
/** 点击事件*/
-(void)click:(UITapGestureRecognizer *)tapGesture {

    CGPoint touchPoint = [tapGesture locationInView:self];
    //遍历当前视图上的子视图的presentationLayer 与点击的点是否有交集
    for (UIView *subView in self.view.subviews) {
        if ([subView.layer.presentationLayer hitTest:touchPoint]) {
            NSLog(@"点击的是:%@",subView);
        }
    }
}

收工google

相关文章
相关标签/搜索