ios – 在MKPolyline上检测触摸手势

ios – 在MKPolyline上检测触摸手势,第1张

概述在我的应用程序中,我有一个带有几个MKGeodesicPolylines的MapView.我希望能够识别这些线上的触摸手势. 使用以下内容添加叠加层: [_mapView addOverlay:polyline]; - (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id < MKOverlay >)ove 在我的应用程序中,我有一个带有几个MKGeodesicpolylines的MapVIEw.我希望能够识别这些线上的触摸手势.
使用以下内容添加叠加层:

[_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:

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上检测触摸手势所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1040160.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存