简体   繁体   中英

MKannotationView with UIButton as subview, button don't respond

I have add a view with button as a subview to MKAnnotationView

 CCBigBubleViewController* buble = [[CCBigBubleViewController alloc] init];
 [annotationView addSubView:buble.view];

It is shown perfectly, but the button does't respond to tapping.

In your implementation of MKAnnotationView, override the hitTest method:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (CGRectContainsPoint(_button.frame, point)) {
        return _button;
    }
    return [super hitTest:point withEvent:event];
}

Your button will then receive touch events.

your annotationView has probably not the right size. Out of it's frame subviews dont respond to touches. For testing this you can clip to bounds.

So make sure your button is inside the frame of your AnnotationView, perhaps sizeToFit will help here.

您将需要像这样在编码中创建按钮动作

 [buttonInstance addTarget:self action:@selector(youraction:) forControlEvents:UIControlEventTouchUpInside];

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