iPhone: Detecting Tap in MKMapView

后端 未结 8 1434
天命终不由人
天命终不由人 2020-12-24 07:35

How do I detect a single tap on an instance of MKMapView? Do I have to subclass MKMapView and then override the touchesEnded method?

8条回答
  •  旧巷少年郎
    2020-12-24 07:54

    Working Perfectly on iOS 8

    - (void)viewDidLoad 
        {
            [super viewDidLoad];
    
            UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
            doubleTap.numberOfTapsRequired = 2;
            doubleTap.numberOfTouchesRequired = 1;
            [self.mapView addGestureRecognizer:doubleTap];
    
            UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
            singleTap.numberOfTapsRequired = 1;
            singleTap.numberOfTouchesRequired = 1;
            [singleTap requireGestureRecognizerToFail: doubleTap];
            [self.mapView addGestureRecognizer:singleTap];
         }
    
       - (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
         {
                if (gestureRecognizer.state != UIGestureRecognizerStateEnded)
                    return;
                //Do your work ...
         }
    

提交回复
热议问题