MKAnnotationView子类具有自定义标注和按钮

时间:2013-11-12 19:30:50

标签: ios iphone objective-c mkmapview mkannotationview

我目前有一个StationCalloutView,它是UIView的子类(带有xib)。此外,我还有一个MKAnnotation和一个StationAnnotationView的站点,它是MKAnnotationView的子类。

在StationAnnotationView中打开StationCalloutView我这样做:

- (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];
    }
}

它完美无缺,但现在我想在StationCalloutView上添加一个按钮,但是当我点击我的AnnotationView时,它会自动取消选择,并且没有任何触摸传递给StationCalloutView。

有人知道如何做到这一点吗?

2 个答案:

答案 0 :(得分:1)

实际的解决方案是将这些方法(如How To add custom View in map's Annotation's Callout's所述)添加到MKAnnotationView子类中:

- (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;
}

答案 1 :(得分:0)

这是一个猜测,但它可能是你对超类方法的调用。只是查看参考文档(https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html#//apple_ref/occ/instm/MKAnnotationView/setSelected:animated :),我不确定您是否需要在覆盖中调用super。