iOS MapView:点击添加注释,但如果现有注释点击则不添加

时间:2014-02-05 22:09:55

标签: ios mkmapview mapkit mkannotation

我有一个UITapGestureRecognizer设置,可以在用户点击的地图上添加注释。我遇到的问题是当用户点击现有注释以查看工具提示时,工具提示会弹出,但另一个注释会添加到点击注释后面的地图中。有没有办法检测是否在添加注释之前点击并返回注释?

这是我的viewDidLoad:

UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(foundTap:)];
singleTapRecognizer.numberOfTapsRequired = 1;
[self.mapView addGestureRecognizer:singleTapRecognizer];

我的触摸功能:

-(IBAction)foundTap:(UITapGestureRecognizer *)recognizer
{
    CGPoint point = [recognizer locationInView:self.mapView];

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

    AGIAnnotation * annotation = [[AGIAnnotation alloc] initWithCoordinate:tapPoint];
    // Some other stuff

    [self.mapView addAnnotation:annotation];
}

感谢任何帮助。

2 个答案:

答案 0 :(得分:6)

有一个UIGestureRecognizerDelegate Protocol

如果触摸位于现有工具提示中,请实施gestureRecognizer:shouldReceiveTouch:并返回NO。像这样:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([touch.view isKindOfClass:[MKPinAnnotationView class]])
    {
        return NO;
    }
    return YES;
}

答案 1 :(得分:3)

Swift 3 +中的

@mbehan 回答:

ViewDidLoad中的

let singleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourClass.foundTap(_:)))
singleTapRecognizer.delegate = self
mapView.addGestureRecognizer(singleTapRecognizer)

和UIGestureRecognizerDelegate扩展名:

extension YourClass: UIGestureRecognizerDelegate {
  func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return !(touch.view is MKPinAnnotationView)
  }
}