简体   繁体   中英

How to create a callout bubble MKAnnotationView that respond to touches


I'm trying to make a custom annotation view able to respond to touches without success.
Thanks to this question I was able to made an annotation view close to what I want customize callout bubble for annotationview? also seen this How to capture touches and NOT dismiss the callout? but the problem is quite different The first thing I've done so far is subclassing an MKAnnotationView and override the -setSelected:animated: method

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  [super setSelected:selected animated:animated];
  if(selected)
  {
    MyCallOut * callOut=[MyCallOut createMyCallOut];
    callOut.tag=555;
    [self.superview addSubview:callOut];

  }
  else
  {
    // [[self viewWithTag:555] removeFromSuperview];
    //Remove my custom CallOut
  }
}

The problem is that the map view is eating all the touches event, my custom callout has two buttons but no action is triggered pressing them.

In one of my experiment I've tried to add the callout view to the MKAnnotationView superview (the mapView) everything seems to be fine while I scroll, but If I zoom the callout moves around.

you have to add call out accessory view in map annotation

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil; 
if(annotation != mapView.userLocation) 
{
    static NSString *defaultPinID = @"com.invasivecode.pin";
    pinView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
    if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
                                      initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
    pinView.pinColor = MKPinAnnotationColorRed; 
    pinView.canShowCallout = YES;
    pinView.animatesDrop = YES;
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    return pinView;



} 
else {
    [mapView.userLocation setTitle:@"I am here"];
}
return pinView;
}

and for click event of your call out

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

}

Solved looking to this link Custom annotation and callouts , the other tricky part is implement a correct way to press button on the fake callout. I managed this part creating a boolean value that understands the number of annotation/parentAnnotation on screen and choose when one could disappear or not.Just need to make the CalloutAnnotation available to selection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM