2

現在、UIView のサブクラスである StationCalloutView があります (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

実際の解決策は、このメソッドを 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 :) 、オーバーライドでスーパーを呼び出す必要があるかどうかはわかりません。

于 2013-11-12T19:37:38.447 に答える