美文网首页
Swift小记:高德地图calloutView不响应点击事件解决

Swift小记:高德地图calloutView不响应点击事件解决

作者: 齐舞647 | 来源:发表于2019-07-11 10:25 被阅读0次

    问题:在使用高德地图SDK时,高度定制calloutView响应气泡,但发现自定义气泡里的点击事件并不响应。

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

    在CustomAnnotationView.m中重写hittest方法:

    swift版本:

        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            
            var view = super.hitTest(point, with: event)
            
            if view == nil {
                
                if self.calloutView == nil {
                    return view
                }
                
                let temPoint1: CGPoint = (self.calloutView?.friendBtn.convert(point, from: self))!
                let temPoint2: CGPoint = (self.calloutView?.settingBtn.convert(point, from: self))!
                
                if (self.calloutView?.friendBtn.bounds.contains(temPoint1))! {
                    view = self.calloutView!.friendBtn
                }
                
                if (self.calloutView?.settingBtn.bounds.contains(temPoint2))! {
                    view = self.calloutView!.settingBtn
                }
            }
            
            return view
        }
    

    OC版本:

    - (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;
    }
    

    相关文章

      网友评论

          本文标题:Swift小记:高德地图calloutView不响应点击事件解决

          本文链接:https://www.haomeiwen.com/subject/cwhrkctx.html