Вы можете использовать UIGestureRecognizer
для обнаружения касаний в представлении карты.
Вместо одного нажатия, однако, я бы посоветовал искать двойное нажатие (UITapGestureRecognizer
) или долгое нажатие (UILongPressGestureRecognizer
). Одно касание может помешать пользователю, пытающемуся нажать одно касание на булавку или на выноску.
В месте, где вы настраиваете вид карты (например, в viewDidLoad
), прикрепите распознаватель жестов к виду карты:
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];
или использовать долгое нажатие:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0; //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
В методе handleGesture:
:
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
return;
CGPoint touchPoint = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D touchMapCoordinate =
[mapView convertPoint:touchPoint toCoordinateFromView:mapView];
MKPointAnnotation *pa = [[MKPointAnnotation alloc] init];
pa.coordinate = touchMapCoordinate;
pa.title = @"Hello";
[mapView addAnnotation:pa];
[pa release];
}