我有一个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];
}
感谢任何帮助。
答案 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)
@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)
}
}