【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

相關文章
相關標籤/搜索