質問

I have custom view after select MKAnnotationView and with that i try to add a gesture recognizer like this:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)selectedAnnotationView {
    if([selectedAnnotationView.annotation isKindOfClass:[CustomPinAnnotation class]]) {
        CustomPinAnnotation *annotation = selectedAnnotationView.annotation;
        [selectedAnnotationView setCalloutOffset:CGPointMake(0,selectedAnnotationView.frame.size.height)];

        CalloutView *calloutView = [[CalloutView alloc] initWithFrame:CGRectMake(0, 0, selectedAnnotationView.frame.size.width, selectedAnnotationView.frame.size.height*2)];

    [selectedAnnotationView addSubview:calloutView];
    [UIView animateWithDuration:annotationAnimationTime animations:^{
        [calloutView setFrame:CGRectMake(-expandingAnnotationWidth/8, 0, [calloutView calculateWurstLenghtFromText:selectedAnnotationView.annotation.title], selectedAnnotationView.frame.size.height*2)];
    } completion:^(BOOL finished) {
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(calloutTapped:)];
        [calloutView addGestureRecognizer:tapGesture];
        [calloutView setUserInteractionEnabled:YES];
    }];
}

In the screen below you can see how it looks like expanded and not: enter image description here

The problem is that the gesture recognizer works only on marked area and not on entire callout:

enter image description here

I probably know what's happen - i add subview to MKAnnotation which is to small. But how to fix this? There is another way to draw callout or maybe i can expand tappable area.

役に立ちましたか?

解決 2

Your annotation's frame is probably too small. Even if you set the callout to be big enough, its parent, the annotation, itself is small. Had the same problem and it's really messy to solve it.

If you don't want to mess with frames and selected states and also because you already are using a custom view which is added, why don't you do the following :

  • add the custom callout view as a completely new/different annotation/overlay when an annotation is selected ( not as a subview like now )
  • remove it when your annotation is deselected

You only need to position it correctly and won't even need a gesture recognizer.

他のヒント

1Maybe you could try to change this:

CalloutView *calloutView = [[CalloutView alloc] initWithFrame:CGRectMake(0, 0, selectedAnnotationView.frame.size.width, selectedAnnotationView.frame.size.height*2)];

To this:

 CalloutView *calloutView = [[CalloutView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];

And see if it works, because I have a feeling that annotationView.frame is too small and sets your calloutView just to this small area. If it's hardcoded it might work.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top