如何在MKMapView上捕捉Tap手势

sbk*_*bkb 31 mkpinannotationview mkmapview ios4 mkoverlay uitapgesturerecognizer

我正在尝试捕捉我的点击事件MKMapView,这样我就可以MKPinAnnotation在用户点击的点上删除.基本上我有一个覆盖的地图MKOverlayViews(显示建筑物的叠加层),我想通过删除MKPinAnnotaion并在标注中显示更多信息来为用户提供有关该叠加的更多信息.谢谢.

小智 59

您可以使用a UIGestureRecognizer来检测地图视图上的触摸.

然而,我建议寻找双击(UITapGestureRecognizer)或长按(UILongPressGestureRecognizer),而不是单击.单击可能会干扰用户尝试单击引脚或标注本身.

在您设置地图视图的位置(viewDidLoad例如),将手势识别器附加到地图视图:

UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)];
tgr.numberOfTapsRequired = 2;
tgr.numberOfTouchesRequired = 1;
[mapView addGestureRecognizer:tgr];
[tgr release];
Run Code Online (Sandbox Code Playgroud)

或使用长按:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
    initWithTarget:self action:@selector(handleGesture:)];
lpgr.minimumPressDuration = 2.0;  //user must press for 2 seconds
[mapView addGestureRecognizer:lpgr];
[lpgr release];
Run Code Online (Sandbox Code Playgroud)


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];
}
Run Code Online (Sandbox Code Playgroud)

  • @ phix23,尝试实现`shouldRecognizeSimultaneouslyWithGestureRecognizer`并从那里返回`YES`.在为要调用的shoudRecognize委托方法添加GR之前,需要执行`tgr.delegate = self;`. (6认同)
  • iOS 6中的UITapGestureRecognizer在MKMapView上无法识别.它在iOS 5中有效.有关此问题的任何想法? (4认同)

o0o*_*o0o 5

我设置了长按(UILongPressGestureRecognizer),viewDidLoad:但它只检测到第一次触摸的唯一一次.

我在哪里可以设置长按以检测所有触摸?(这意味着每次等待用户触摸屏幕推针时都准备好了地图)

viewDidLoad:方法!

- (void)viewDidLoad {
    [super viewDidLoad];mapView.mapType = MKMapTypeStandard;

    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)];
    [self.mapView addGestureRecognizer:longPressGesture];
    [longPressGesture release];

    mapAnnotations = [[NSMutableArray alloc] init];
    MyLocation *location = [[MyLocation alloc] init];
    [mapAnnotations addObject:location];

    [self gotoLocation];
    [self.mapView addAnnotations:self.mapAnnotations];
}
Run Code Online (Sandbox Code Playgroud)

handleLongPressGesture方法:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
    // This is important if you only want to receive one tap and hold event
    if (sender.state == UIGestureRecognizerStateEnded)
    {NSLog(@"Released!");
        [self.mapView removeGestureRecognizer:sender];
    }
    else
    {
        // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
        CGPoint point = [sender locationInView:self.mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        // Then all you have to do is create the annotation and add it to the map
        MyLocation *dropPin = [[MyLocation alloc] init];
        dropPin.latitude = [NSNumber numberWithDouble:locCoord.latitude];
        dropPin.longitude = [NSNumber numberWithDouble:locCoord.longitude];
//        [self.mapView addAnnotation:dropPin];
        [mapAnnotations addObject:dropPin];
        [dropPin release];
        NSLog(@"Hold!!");
        NSLog(@"Count: %d", [mapAnnotations count]);
    }   
}
Run Code Online (Sandbox Code Playgroud)