iPhone: Detecting Tap in MKMapView

后端 未结 8 1395
天命终不由人
天命终不由人 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:43

    If you're just looking to get notified of tap gestures without affecting any of the other touch behavior of the map, you'll want to use a UITapGestureRecognizer. It's super simple, just put in some code like this.

    UITapGestureRecognizer* tapRec = [[UITapGestureRecognizer alloc] 
       initWithTarget:self action:@selector(didTapMap:)];
    [theMKMapView addGestureRecognizer:tapRec];
    [tapRec release];
    

    That will call the didTapMap whenever theMKMapView receives a tap gesture and all the pinching, and dragging gestures will still work as they did before.

    0 讨论(0)
  • 2020-12-24 07:48

    Hope this will help : How to intercept touches events on a MKMapView or UIWebView objects?

    0 讨论(0)
  • 2020-12-24 07:52

    Or depending on what you are trying to do, add an MKAnnotation (push pin, with a callout), so you have something to tap on - and then your map delegate will receive an event eg.

    mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

    0 讨论(0)
  • 2020-12-24 07:52

    You cant at this time intercept touches on a map view, you can try layering an opaque view on there and see if it picks up touches...

    0 讨论(0)
  • 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 ...
         }
    
    0 讨论(0)
  • 2020-12-24 07:55

    Nothing I ever found worked, but I came up with this unperfect solution : In viewDidLoad

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

    In delegate :

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
        return touch.view!.frame.equalTo(mapView.frame)
    }
    
    0 讨论(0)
提交回复
热议问题