我正在寻找在MKMapView上创建“长效”插脚。

当前,一切都按我想要的方式工作,当您点击并按住地图时,它会注册手势,并且代码会掉一个大头针。

-(void)viewDidLoad
{
   [self.mapView addGestureRecognizer:longPressGesture];
}

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender
{
    if(sender.state == UIGestureRecognizerStateBegan || sender == nil)
    {
        CGPoint point = [sender locationInView:self.mapView];

        CLLocationCoordinate2D locCoord;
        locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

        //Drop Pin
    }
}


也就是说,仅当您距离MKUserLocation注释(蓝色脉冲点)不太近时,该方法才起作用,否则,该手势将不会被注册,并且会调用didSelectAnnotationView:函数。

有没有办法忽略用户点击MKUserLocation批注?我一直在寻找setUserEnabled之类的东西,但对于MKAnnotations它并不存在。

谢谢你的帮助。

最佳答案

MKAnnotationView类具有enabled属性(不是id<MKAnnotation>对象)。


要在地图视图的用户位置注释视图上设置enabled,请在mapView:didAddAnnotationViews:委托方法中获取对其的引用:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    MKAnnotationView *av = [mapView viewForAnnotation:mapView.userLocation];
    av.enabled = NO;  //disable touch on user location
}



(在viewForAnnotation中,您必须返回nil来告诉地图视图创建视图本身,这样就不能在其中设置enabled -至少对于用户位置而言。)

关于ios - 忽略MKUserLocation选择以注册UILongTapGestureRecognizer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22840346/

10-08 23:10