问题:在使用高德地图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;
}
网友评论