Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>the idea is that you want to make all of the "accessory view" tappable <strong>without</strong> interfering with the original tap of the actual annotation.. this is how i do it:</p> <p><strong>first</strong> I create and assign a tap gesture to the annotation view <em>after</em> it has been selected like so (I use obj-c runtime object association here.. see <a href="https://gist.github.com/abbood/7039483" rel="nofollow">this</a> gist):</p> <pre><code>// set up vars static NSString *const kExtraTapGestureRecognizer = @"extraGesture"; UIControl *currentAnnotationControl; - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleAnnotationViewTap:)]; tap.numberOfTapsRequired = 1; [tap setInfo:kExtraTapGestureRecognizer]; [view addGestureRecognizer:tap]; } </code></pre> <p>to handle the tap, I call <code>MKMapViewDelegate</code> Protocol's <a href="https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html#//apple_ref/occ/intfm/MKMapViewDelegate/mapView%3aannotationView%3acalloutAccessoryControlTapped%3a" rel="nofollow">mapView:annotationView:calloutAccessoryControlTapped:</a>, which basically means that if the view in between the accessory views gets tapped, it's as if one of the accessory views just got tapped:</p> <pre><code>- (void)handleAnnotationViewTap:(UITapGestureRecognizer *)gestureRecognizer { MKAnnotationView *annotationView = (MKAnnotationView *)gestureRecognizer.view; // currentAnnotationControl is just a reference to one of the // accessory views of the annotation that has just been selected.. // see comment below [self mapView:self.mapView annotationView:annotationView calloutAccessoryControlTapped:currentAnnotationControl]; } </code></pre> <p>when the annotationView is returned, I save a reference to one of its (left or right) accessoryViews like so:</p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation { if ([annotation isKindOfClass:[Random class]]) { static NSString *annotationIdentifier = @"annotation"; MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView annotationIdentifier]; if (annotationView == nil) { annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:RiderAnnotationIdentifier]; annotationView.canShowCallout = YES; UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom]; leftButton.frame = CGRectMake(0, 0,21, 21); [leftButton setImage:[UIImage imageNamed:@"smallInfo_rider_left.png"] forState:UIControlStateNormal]; [leftButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside]; annotationView.leftCalloutAccessoryView = leftButton; // this is where i store a reference to one of the accessory views currentAnnotationControl = leftButton; return annotationView; } else { annotationView.annotation = annotation; } return annotationView; </code></pre> <p>keep in mind that we created an extra tap gesture, we must get rid of it as soon as one of the accessory views has been tapped like so:</p> <pre><code>- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control { // we remove the extra tap gesture so that it doesn't interfere // with normal app flow in the future for (UIGestureRecognizer *gesture in [view gestureRecognizers]) { if ([[gesture info] isEqualToString:kExtraTapGestureRecognizer]) { [gesture removeTarget:nil action:NULL]; } } // more stuff </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload