简体   繁体   English

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

[英]MKAnnotationView subclass with custom callout and button

I currently have a StationCalloutView that's a subclass of UIView (with a xib). 我目前有一个StationCalloutView,它是UIView的子类(带有xib)。 Also I've got a Station thats a MKAnnotation and an StationAnnotationView that's a subclass of MKAnnotationView. 另外,我有一个Station,它是MKAnnotation,而StationAnnotationView是MKAnnotationView的子类。

In StationAnnotationView to open StationCalloutView I'm doing this: 在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];
    }
}

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. 它工作正常,但是现在我想向StationCalloutView添加一个按钮,但是当我单击AnnotationView时,它会自动取消选择,并且没有任何触摸传递给StationCalloutView。

Does anybody has an idea on how to accomplish this? 有人对如何做到这一点有想法吗?

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: 实际的解决方案是只向MKAnnotationView子类添加此方法(如此处所述, 如何在地图的Annotation的Callout中添加自定义View ):

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

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. 仅查看参考文档( https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKAnnotationView_Class/Reference/Reference.html#//apple_ref/occ/instm/MKAnnotationView/setSelected:animated :) ,我不确定您是否需要在替代中调用超级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM