Custom MQAnnotationView button is not clickable

蹲街弑〆低调 提交于 2020-01-16 18:39:18

问题


I'm working with MapQuest but I think that is not the problem.

I have map (with MapQuest) and custom pins on it. I can tap the pins and my custom callout (xib-file with labels and one button) pops up and everything is working fine. The only problem is that I can't press the button on the custom callout view (UIView).

Here is my code:

-(MQAnnotationView*)mapView:(MQMapView *)aMapView viewForAnnotation:(id<MQAnnotation>)annotation {

    static NSString* identifier = @"Pins";
    MQAnnotationView * annotationView = (MQAnnotationView *)[self->mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

    annotationView = [[MQAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
    annotationView.image = [UIImage imageNamed:@"marker_schuhe"];

    annotationView.enabled = YES;
    annotationView.canShowCallout = NO;

    return annotationView;
}

And in my didSelectAnnotationView method:

- (void)mapView:(MQMapView *)mapView didSelectAnnotationView:(MQAnnotationView *)view {
        callOutView *calloutView = (callOutView *)[[[NSBundle mainBundle] loadNibNamed:@"callOutView" owner:self options:nil] objectAtIndex:0];
    CGRect calloutViewFrame = calloutView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
    calloutView.frame = calloutViewFrame;

   [calloutView.my_button addTarget:self action:@selector(button_pressed:) forControlEvents:UIControlEventTouchUpInside]; //here is something wrong, button_pressed is never called

[view addSubview:calloutView];
}

回答1:


Set userInteractionEnabled = YES;on the calloutView?

-- edit after comment from user

If @selector isn't working (because the MKAnotationView intercepts it) try adding a UIGesture to your UIButton like this:

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] 
            initWithTarget:self action:@selector(button_pressed:)];
        tap.numberOfTapsRequired = 1;
        [calloutView.my_button addGestureRecognizer:tap];


来源:https://stackoverflow.com/questions/24635311/custom-mqannotationview-button-is-not-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!