iPhone: Detecting Tap in MKMapView

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

    Just add some code snippet as illustration of @tt-kilew answer. In my case, I want to point the user to itself on the map but do not want to interrupt his drag touch.

    @interface PrettyViewController () 
    
    @property (weak, nonatomic) IBOutlet MKMapView *mapView;
    @property (assign, nonatomic) BOOL userTouchTheMap;
    
    @end
    
    @implementation PrettyViewController
    
    #pragma mark - UIResponder
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        self.userTouchTheMap = [[touches anyObject].view isEqual:self.mapView];
    }
    
    
    #pragma mark - MKMapViewDelegate
    
    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
        //We just positioning to user
        if (!self.userTouchTheMap) {
            CLLocationDistance radius = 5000;
            [self.mapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate, 2*radius, 2*radius) animated:YES];
        }
    }
    
    @end
    

提交回复
热议问题