大佬教程收集整理的这篇文章主要介绍了ios – 在MKPolyline上检测触摸手势,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
[_mapView addOverlay:polyline];
- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)overlay { if ([overlay class] == [MKGeodesicPolyline class]) { MKPolylineRenderer *renderer = [[[MKPolylineRenderer alloc] initWithPolyline:overlay] autorelease]; renderer.lineWidth = 4.0; renderer.strokeColor = [UIColor blackColor]; return renderer; } return nil; }
我试过WildcardGestureRecognizer应该符合我的目的.这是我正在使用的代码:
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { .... MapView init .... WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init]; tapInterceptor.touchesBeganCallBACk = ^(NSSet * touches,UIEvent * event) { UITouch *touch = [touches anyObject]; CGPoint point = [touch LOCATIOnInView:_mapView]; CLLOCATIOnCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:self.mapView]; MKMapPoint mapPoint = MKMapPointForCoordinate(coord); for (id overlay in _mapView.overlays) { if ([overlay isKindOfClass:[MKGeodesicPolyline class]]) { MKGeodesicPolyline *poly = (MKGeodesicPolyline*) overlay; id view = [_mapView viewForOverlay:poly]; NSLog(@"view class: %@",[view class]); if ([view isKindOfClass:[MKPolylineRenderer class]]) { MKPolylineRenderer *polyView = (MKPolylineRenderer*) view; CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint]; BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path,NULL,polygonViewPoint,NO); if (mapCoordinateIsInPolygon) { NSLog(@"hit!"); } else { NSLog(@"miss!"); } } } } }; [_mapView addGestureRecognizer:tapInterceptor]; } return self; }
问题是上面代码调用第一个Log的地方.该类似乎总是返回null.日志输出:
2014-01-06 13:50:41.106 App [11826:60b] view class:(null)
我希望有人能指出我正确的方向.
非常感谢提前!
工作代码:
我让它为我工作.希望它可以帮助其他人:
- (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { .... MapView init .... WildcardGestureRecognizer * tapInterceptor = [[WildcardGestureRecognizer alloc] init]; tapInterceptor.touchesBeganCallBACk = ^(NSSet * touches,UIEvent * event) { CGPoint point = [tapInterceptor LOCATIOnInView:_mapView]; CLLOCATIOnCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:self.mapView]; MKMapPoint mapPoint = MKMapPointForCoordinate(coord); for (id overlay in _mapView.overlays) { if ([overlay isKindOfClass:[MKGeodesicPolyline class]]) { MKGeodesicPolyline *poly = (MKGeodesicPolyline*) overlay; id view = [_mapView viewForOverlay:poly]; NSLog(@"view class: %@",[view class]); if ([view isKindOfClass:[MKPolylineRenderer class]]) { MKPolylineRenderer *polyView = (MKPolylineRenderer*) view; [polyView invalidatePath]; CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint]; BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path,NO); if (mapCoordinateIsInPolygon) { NSLog(@"hit!"); } else { NSLog(@"miss!"); } } } } }; [_mapView addGestureRecognizer:tapInterceptor]; } return self; }
替代方案:
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@SELEctor(handleGesture:)]; [_mapView addGestureRecognizer:recognizer]; [recognizer release];
处理手势:
- (void)handleGesture:(UIGestureRecognizer *)recognizer { if (recognizer.state == UIGestureRecognizerStateEnded) { for (int i=0; i<recognizer.numberOfTouches; i++) { //CGPoint point = [recognizer LOCATIOnInView:_mapView]; CGPoint point = [recognizer LOCATIOnOfTouch:i inView:_mapView]; CLLOCATIOnCoordinate2D coord = [_mapView convertPoint:point toCoordinateFromView:self.mapView]; MKMapPoint mapPoint = MKMapPointForCoordinate(coord); for (id overlay in _mapView.overlays) { if ([overlay isKindOfClass:[MKGeodesicPolyline class]]) { MKGeodesicPolyline *poly = (MKGeodesicPolyline*) overlay; id view = [_mapView rendererForOverlay:poly]; if ([view isKindOfClass:[MKPolylineRenderer class]] && [[poly title] isEqualToString:@"fullRouteAbove"]) { MKPolylineRenderer *polyView = (MKPolylineRenderer*) view; [polyView invalidatePath]; CGPoint polygonViewPoint = [polyView pointForMapPoint:mapPoint]; NSLog(@"polyView: %@",polyView); BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path,NO); if (mapCoordinateIsInPolygon) { NSLog(@"hit!"); }else{ NSLog(@"miss!"); ) } } } } } }
在可触摸区域之间切换:
更换
BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polyView.path,NO);
同
BOOL mapCoordinateIsInPolygon = CGRectContainsPoint(CGPathGetBoundingBox(polyView.path),polygonViewPoint);
要么
BOOL mapCoordinateIsInPolygon = CGRectContainsPoint(CGPathGetPathBoundingBox(polyView.path),polygonViewPoint);
将一个名为arrPolylineViews的NSMutableArray添加到您的类中.
然后将tapGestureRecognizer添加到mapView:
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] init]; gr.numberOfTapsrequired = 1; gr.numberOfTouchesrequired = 1; gr.delegate = self; [gr addTarget:self action:@SELEctor(didTap:)]; [mymapView addGestureRecognizer:gr];
在didTap中:
- (void)didTap:(UITapGestureRecognizer *)gr { if (gr.state == UIGestureRecognizerStateEnded) { // convert the touch point to a CLLOCATIOnCoordinate & geocode CGPoint touchPoint = [gr LOCATIOnInView:mymapView]; MKPolylineView *touchedPolyLineView = [self polylineTapped:touchPoint]; if (touchedPolyLineView) { //touched a polyLineView } } }
然后:
- (MKOverlayView*)mapView:(MKMapView*)theMapView viewForOverlay:(id <MKOverlay>)overlay { if([overlay isKindOfClass:[MKPolyline class]]){ //create your polyLineView [arrPolylineViews addObject:polyLineView]; } }
- (MKPolylineView *)polylineTapped:(CGPoint)point { // check if the overlay got tapped for (MKPolylineView *polyView in arrPolylineViews) { // Get view frame rect in the mapView's coordinate system CGRect viewFrameInMapView = [polyView.superview convertRect:polyView.frame toView:mymapView]; // check if the touch is within the view bounds if (CGRectContainsPoint(viewFrameInMapView,point)) { return polyView; } } return nil; }
别忘了 –
[arrPolylineViews removeAllObjects];
在地图上添加新的点列表之前.
以上是大佬教程为你收集整理的ios – 在MKPolyline上检测触摸手势全部内容,希望文章能够帮你解决ios – 在MKPolyline上检测触摸手势所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。