高德 ios 自定义气泡添加点击事件无效问题

在使用高德地图sdk开发的时候,需要自定义气泡吹出框,发现气泡添加的点击事件或者button都没响应.

原因:自定义的气泡是添加到大头针上的,而大头针的size只有下面很小一部分,所以calloutView是在大头针的外面的。
而 iOS 按钮超过父视图范围是无法响应事件的处理方法。

解决办法:

在CustomAnnotationView.m( @interface CustomAnnotationView : MAAnnotationView

)中重写hittest方法:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        CGPoint tempoint = [self.calloutView.navBtn convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.calloutView.navBtn.bounds, tempoint))
        {
            view = self.calloutView.navBtn;
        }
    }
    return view;
}

这里的self.calloutView.navBtn 就是你需要点击的按钮

亲测可用.

原文地址:https://www.cnblogs.com/bug-sniper/p/5236204.html