在我目前正在设计的应用程序中,我有一个MKMapView
覆盖层(自定义MKPolylines
btw),我希望能够检测这些覆盖层上的触摸事件并为每个触摸事件分配一个特定的操作覆盖。任何人都可以帮我解决这个问题吗? 谢谢!在MKMapView的覆盖物上触摸事件
贝尼亚
在我目前正在设计的应用程序中,我有一个MKMapView
覆盖层(自定义MKPolylines
btw),我希望能够检测这些覆盖层上的触摸事件并为每个触摸事件分配一个特定的操作覆盖。任何人都可以帮我解决这个问题吗? 谢谢!在MKMapView的覆盖物上触摸事件
贝尼亚
万一它可能会帮助一些你...... 我无法找到一个方法来做到这一点,但我加的注释上我的叠加(反正我需要做的是显示一些信息),然后我可以在此注释中获得触摸事件。我知道这不是最好的办法,但在我的情况下,也许是你的,它的工作原理;)!
这可以通过组合How to intercept touches events on a MKMapView or UIWebView objects?和How to determine if an annotation is inside of MKPolygonView (iOS)来解决。加入看这个会引导:
WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init];
tapInterceptor.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.mapView];
CLLocationCoordinate2D coord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
MKMapPoint mapPoint = MKMapPointForCoordinate(coord);
for (id overlay in self.mapView.overlays)
{
if ([overlay isKindOfClass:[MKPolygon class]])
{
MKPolygon *poly = (MKPolygon*) overlay;
id view = [self.mapView viewForOverlay:poly];
if ([view isKindOfClass:[MKPolygonView class]])
{
MKPolygonView *polyView = (MKPolygonView*) view;
CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint];
BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path, NULL, polygonViewPoint, NO);
if (mapCoordinateIsInPolygon) {
debug(@"hit!")
} else {
debug(@"miss!");
}
}
}
}
};
[self.mapView addGestureRecognizer:tapInterceptor];
WildcardGestureRecognizer
是在第一个链接的答案。调用mapView:viewForOverlay:
不会很便宜,添加一个本地缓存将有所帮助。
辉煌。这正是我在寻找叠加层时需要检测的代码。不过,我把它放在称为购买轻敲手势识别器的方法里面。没有必要创建自定义拦截器,工作得很好。 – drekka
This停止在iOS7上工作! polyView.path始终为零! – Bach
用MKPolygonRenderer替换MKPolygonView - 前者在iOS7中不推荐使用 – guyh92