ios - 给mkmap添加注解,想去掉部分注解却不会去掉

标签 ios objective-c mkmapview mkannotation

我创建了一个注释,其中包含几个元素 textbubble 和 pin。我在显示注释时打开气泡,但稍后我想关闭气泡并留下注释。

这是我的两种方法。添加 subview 有效,但删除 subview 无效。

-(void)hideETACountdown {
self.etaView.hidden = YES;
[self.etaView removeFromSuperview];
}

-(void)showETACountdown {

self.etaView = [[UIView alloc] initWithFrame:CGRectMake(-34, -97, 89, 59)];
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"WaitBubble_backgroundandshadow.png"]];
[self.etaView addSubview:bg];
UILabel *minLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 24, 42, 21)];
minLabel.text = @"min";
minLabel.textAlignment = UITextAlignmentCenter;
minLabel.font = [UIFont systemFontOfSize:10];

self.etaLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 4, 30, 27)];
self.etaLabel.font = [UIFont boldSystemFontOfSize:22];
self.etaLabel.textAlignment = UITextAlignmentCenter;
self.etaLabel.text = @"";

[self.etaView addSubview:minLabel];
[self.etaView addSubview:self.etaLabel];

[self addSubview:self.etaView];

self.etaView.hidden = NO;
}

- (id) initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {


    self.canShowCallout = YES;
    self.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    self.innerImage = [[UIImageView alloc] initWithImage:nil];
    self.innerImage.frame = CGRectMake(-15, -38, 32, 39);

    [self addSubview:self.innerImage];

    if(self.showETA) {

        [NSNotificationCenter addUniqueObserver:self
                                       selector:@selector(handleEtaTimeUpdate:)
                                           name:kEtaUpdate
                                         object:nil];
        [self showETACountdown];

    }

}
return self;
}

//更新/////

似乎有些困惑。上面的代码不在保存我的 mkmap 的 viewController 中,而是在我的自定义注释中的代码。此外,我不想根据选择或取消选择来隐藏或显示整个注释。 self.etaView 是自定义 View ,它只是注释的一部分。我的注释由一个自定义 map 图钉和一个 eta 气泡组成。一旦 ETA 倒计时为 0,我想删除气泡(又名 self.etaView),但注释( map 图钉)需要始终留在 map 上。我只想隐藏 ETA 气泡。

我在保存我的 mkmap 的 viewController 中以正确的方式使用正确的 addAnnotation 方法。同样,上面的代码在我的自定义注释中,我希望我的自定义注释负责删除它自己的元素,而不是从 map 中删除它自己。

最佳答案

拜托,为什么要在 addSubView 和 removeFromSuperView 中使用这种奇怪的逻辑。 MKMapView 的构建是为了支持引脚的“数据源”。我不知道您想要实现什么样的 View ,但是这个 CGRectMake(-34, -97, 89, 59) 看起来很糟糕。所以请使用方法:

-(MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id<MKAnnotation>)annotation

这样你就可以毫无困难地使用方法管理注解

- (void)deselectAnnotation:(id < MKAnnotation >)annotation animated:(BOOL)animated

例如:

[mapView deselectAnnotation:[mapView.selectedAnnotations objectAtIndex:0] animated:YES];

关于ios - 给mkmap添加注解,想去掉部分注解却不会去掉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17507789/

相关文章:

ios - 如何在 UITableView 中将 UITableViewCell Image 更改为 Circle

ios - 选择注释时如何将 View 设置为仅垂直居中

ios - 如何使用 GDataXml 创建不带标签的文本子项

ios - 导航 Controller 之间的segue

iphone - 双击图像无响应

ios - 如何使用 CoreGraphics 重新着色图像?

objective-c - 是否 setNeedsDisplay :NO have any use at all?

ios - 如何在 block 外部保持引用有效(启用ARC)?

objective-c - iOS 6.0 map 中的最大缩放比例?

ios - 显示图像和标题的 MapView 注释