當(dāng)給UIView添加Animation動畫時,項目需要添加點擊事件。
但是使用UIButton無效,不響應(yīng)點擊事件。
baidu / google 之。
發(fā)現(xiàn)UILayer不響應(yīng)事件。
換一種思路,發(fā)現(xiàn)可以給整個視圖添加點擊手勢,然后判斷點擊位置來觸發(fā)事件。
代碼片段
//創(chuàng)建手勢添加到視圖上
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];
//遍歷當(dāng)前視圖上的子視圖的presentationLayer 與點擊的點是否有交集
for (UIView *subView in self.view.subviews) {
if ([subView.layer.presentationLayer hitTest:touchPoint]) {
NSLog(@"點擊的是:%@",subView);
}
}
}