真實使用場景
如圖:
問題:
地圖標註Annotation(小孩圖標),當選中的時候會呼出一個彈框(callOut彈框).要在這個彈框中添加一個導航按鈕,但是在點擊的時候會出現無法響應到點擊事件的情況.
分析
這是由於響應者鏈條的關係.由於事件的傳遞會先判斷點擊的view,再判斷touch點是否在自己身上來判斷事件的傳遞.具體請點擊這裏查看關於hitTest:withEvent的介紹
解決問題
重寫annotationView的hitTest:withEvent:方法
代碼如下
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
if (view == nil) {
CGPoint tempoint = [self.calloutView.guideBtn convertPoint:point fromView:self];
if (CGRectContainsPoint(self.calloutView.guideBtn.bounds, tempoint))
{
view = self.calloutView.guideBtn;
}
}
return view;
}