gpt4 book ai didi

ios - 带有自定义标注和按钮的 MKAnnotationView 子类

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:52 24 4
gpt4 key购买 nike

我目前有一个 StationCalloutView,它是 UIView 的子类(带有 xib)。我还有一个 Station,它是一个 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。

有人知道如何实现吗?

最佳答案

实际的解决方案是将此方法(如此处提到的 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;
}

关于ios - 带有自定义标注和按钮的 MKAnnotationView 子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19938205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com