2

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

在 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。

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

4

2 回答 2

1

实际的解决方案是将这些方法(如如何在地图的 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;
}
于 2013-11-13T12:24:41.410 回答
0

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

于 2013-11-12T19:37:38.447 回答