在我当前设计的应用程序中,我有一个带有覆盖层的 MKMapView
(顺便说一句,自定义的 MKPolylines
),我希望能够检测到触摸事件这些覆盖层并为每个覆盖层分配特定的操作。有人可以帮我解决这个问题吗?
谢谢!
本贾
最佳答案
这可以结合 How to intercept touches events on a MKMapView or UIWebView objects? 来解决和 How to determine if an annotation is inside of MKPolygonView (iOS) 。在 viewWillAppear 中添加以下内容:
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:
的成本并不便宜,添加这些内容的本地缓存会有所帮助。
关于mkmapview - MKMapView 覆盖层上的触摸事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6436479/