當(dāng)給UIView添加Animation動(dòng)畫時(shí),項(xiàng)目需要添加點(diǎn)擊事件。
但是使用UIButton無(wú)效,不響應(yīng)點(diǎn)擊事件。
baidu / google 之。
發(fā)現(xiàn)UILayer不響應(yīng)事件。
換一種思路,發(fā)現(xiàn)可以給整個(gè)視圖添加點(diǎn)擊手勢(shì),然后判斷點(diǎn)擊位置來(lái)觸發(fā)事件。
代碼片段
//創(chuàng)建手勢(shì)添加到視圖上
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
[self.view addGestureRecognizer:self.tapGesture];
#pragma mark - 點(diǎn)擊
/** 點(diǎn)擊事件*/
-(void)click:(UITapGestureRecognizer *)tapGesture {
CGPoint touchPoint = [tapGesture locationInView:self];
//遍歷當(dāng)前視圖上的子視圖的presentationLayer 與點(diǎn)擊的點(diǎn)是否有交集
for (UIView *subView in self.view.subviews) {
if ([subView.layer.presentationLayer hitTest:touchPoint]) {
NSLog(@"點(diǎn)擊的是:%@",subView);
}
}
}