fdiaz
fdiaz

Reputation: 2600

MKAnnotationView subclass with custom callout and button

I currently have a StationCalloutView that's a subclass of UIView (with a xib). Also I've got a Station thats a MKAnnotation and an StationAnnotationView that's a subclass of MKAnnotationView.

In StationAnnotationView to open StationCalloutView I'm doing this:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
    if (selected) {
        self.calloutView = [[StationCalloutView alloc] initWithStation:self.annotation];
        if (currentLocation) [self.calloutView calculateDistanceFromLocation:currentLocation];
        [self.calloutView setOrigin:CGPointMake(-self.calloutView.frame.size.width/5.5, -self.calloutView.frame.size.height)];
        [self animateCalloutAppearance];
        [self addSubview:self.calloutView];
    } else {
        [self.calloutView removeFromSuperview];
    }
}

It works perfectly but now I want to add a button to StationCalloutView but I when I click on my AnnotationView it get's deselected automatically and no touches are passed to StationCalloutView.

Does anybody has an idea on how to accomplish this?

Upvotes: 2

Views: 3998

Answers (2)

fdiaz
fdiaz

Reputation: 2600

The actual solution was to just add this methods (as mentioned here How To add custom View in map's Annotation's Callout's) to the MKAnnotationView subclass:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
    UIView* hitView = [super hitTest:point withEvent:event];
    if (hitView != nil)
    {
        [self.superview bringSubviewToFront:self];
    }
    return hitView;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
    CGRect rect = self.bounds;
    BOOL isInside = CGRectContainsPoint(rect, point);
    if(!isInside)
    {
        for (UIView *view in self.subviews)
        {
            isInside = CGRectContainsPoint(view.frame, point);
            if(isInside)
                break;
        }
    }
    return isInside;
}

Upvotes: 1

codebaboon
codebaboon

Reputation: 67

This is a bit of a guess but it could be your call to the superclass method. Just looking at the reference docs (https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html#//apple_ref/occ/instm/MKAnnotationView/setSelected:animated:), I'm not sure you need to call the super in your override.

Upvotes: 0

Related Questions