3
votes


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.

2
Don't know I accept alway, but only if the answer is right ;-)Andrea

2 Answers

4
votes

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   
{

}
0
votes

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.