- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个使用自定义 AnnotationView 的应用程序。它有一个自定义图像和一个带有标签的背景。背景和标签是annotationView的子View。
if ([annotation isKindOfClass:[VehicleAnnotation class]]) {
VehicleAnnotation *vehicleAnnotation = annotation;
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier];
if(!pinView)
{
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier];
}
pinView.annotation = annotation;
pinView.canShowCallout = NO;
pinView.image = [ImageHelper imageForStatus:vehicleAnnotation.vehicle.driver.driverStatus];
// Add a label with the name of the driver
CGRect pinFrame = pinView.frame;
UILabel *label = [[UILabel alloc] init];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setNumberOfLines:2];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setFont:[UIFont systemFontOfSize:12]];
label.text = vehicleAnnotation.vehicle.driver.description;
CGSize maximumSize = CGSizeMake(100, 50);
CGSize myStringSize = [label.text sizeWithFont:label.font
constrainedToSize:maximumSize
lineBreakMode:label.lineBreakMode];
CGRect labelFrame = CGRectMake(pinFrame.origin.x, pinFrame.origin.y + pinFrame.size.height * 2 + 2, myStringSize.width, myStringSize.height);
[label setFrame:labelFrame];
// Add a background to the label.
CGFloat offset = 5;
CGRect backgroundFrame = CGRectMake(labelFrame.origin.x - offset, labelFrame.origin.y - offset , labelFrame.size.width + 2 * offset, labelFrame.size.height + 2 * offset);
UIView *backgroundView = [[UIView alloc] initWithFrame:backgroundFrame];
backgroundView.layer.cornerRadius = 5;
backgroundView.layer.masksToBounds = YES;
[backgroundView setBackgroundColor:[UIColor darkGrayColor]];
[backgroundView setAlpha:0.8];
[pinView addSubview:backgroundView];
[label setUserInteractionEnabled:YES];
[label setMultipleTouchEnabled:YES];
[pinView addSubview:label];
return pinView;
}
每次收到新位置时都会执行此代码。问题是旧标签和旧背景仍然存在。所以我想我把这段代码:
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier];
if(!pinView)
{
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier];
}
for(UIView *view in [pinView subviews])
{
[view removeFromSuperview];
}
pinView.annotation = annotation;
但是没有显示标签或背景。有人可以帮我以适当的方式替换或删除背景和标签吗?
谢谢!
最佳答案
我建议制作您自己的自定义 MKAnnotatinView。否则使用上面的代码,尝试在分配初始化 MKAnnotationView 的 block 中添加标签和背景 View 。设置框架和文本以及其他属性,这些属性会随着 block 外的不同注释而变化,如下所示。
if ([annotation isKindOfClass:[VehicleAnnotation class]]) {
VehicleAnnotation *vehicleAnnotation = annotation;
MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier];
if(!pinView)
{
pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier];
UILabel *label = [[UILabel alloc] init];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setNumberOfLines:2];
[label setLineBreakMode:NSLineBreakByWordWrapping];
[label setFont:[UIFont systemFontOfSize:12]];
[label setUserInteractionEnabled:YES];
[label setMultipleTouchEnabled:YES];
[label setTag:101];
UIView *backgroundView = [[UIView alloc]init];
[backgroundView setTag:102];
backgroundView.layer.cornerRadius = 5;
backgroundView.layer.masksToBounds = YES;
[backgroundView setBackgroundColor:[UIColor darkGrayColor]];
[backgroundView setAlpha:0.8];
[pinView addSubView:backgroundView];
[pinView addSubview:label];
}
pinView.annotation = annotation;
pinView.canShowCallout = NO;
pinView.image = [ImageHelper imageForStatus:vehicleAnnotation.vehicle.driver.driverStatus];
// set frame of Label
CGRect pinFrame = pinView.frame;
UILabel *label = (UILabel *)[pinView viewWithTag:101];
label.text = vehicleAnnotation.vehicle.driver.description;
CGSize maximumSize = CGSizeMake(100, 50);
CGSize myStringSize = [label.text sizeWithFont:label.font
constrainedToSize:maximumSize
lineBreakMode:label.lineBreakMode];
CGRect labelFrame = CGRectMake(pinFrame.origin.x, pinFrame.origin.y + pinFrame.size.height * 2 + 2, myStringSize.width, myStringSize.height);
[label setFrame:labelFrame];
// set backgroundView frame.
CGFloat offset = 5;
CGRect backgroundFrame = CGRectMake(labelFrame.origin.x - offset, labelFrame.origin.y - offset , labelFrame.size.width + 2 * offset, labelFrame.size.height + 2 * offset);
UIView *backgroundView = [pinView viewWithTag:102];
[backgroundView setFrame:backgroundFrame];
return pinView;
}
关于ios - MKPinAnnotationView subview 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574829/
我正在开发一个 OSX 应用程序。 我对 MKAnnotation 进行了子类化: import Foundation import MapKit class LocationAnnotation:
我想设置自定义图像而不是 map 上的普通图钉。我还希望它能够打开并为每个添加放置动画。 有什么建议吗? 编辑:这是我所拥有的一切 - (MKAnnotationView *)mapView:(MKM
根据 Apple 文档,MKPinAnnotationView 的图钉颜色有红色、绿色和紫色。还有什么办法可以得到其他颜色吗?我在文档中没有找到任何内容。 最佳答案 更多;) 还有原来的: 代码: -
我不明白为什么(理论上)与 MKPointAnnotation 关联的 MKPinAnnotationView 没有出现在 map 上。事实上,图钉出现了,但它不是应有的紫色... 这是代码: MKP
我正在使用 iOS Mapkit 来放置代表包的图钉。初始负载时一切正常。 我需要做的是,当用户位置在最近包裹的100m半径内时,我需要将引脚更改为“解锁”状态(目前可能只是更改颜色)。我已经检测到最
我有以下代码可以在我的 map 中删除并添加新图钉: - (IBAction)setLocation:(id)sender{ NSMutableArray *toRemove = [NSMut
我过去有过使用 MKMapView 和 MKPointAnnotation 的经验,我用它们在 map 上放置了一些图钉。这次我尝试更进一步,使用 MKPinAnnotationView 来编写标签和
编辑:将标题从:“Double tap ..”更改为“Double selection touch..” 我需要在我的应用程序中至少检测到对 MKPinAnnotationView 的第二次触摸。目前
我必须为不同的引脚设置自定义图像。使用我的代码,我可以为它们中的每一个设置自定义颜色,但是当我尝试设置自定义图像而不是颜色时,它不起作用。 - (MKAnnotationView *)mapView:
我想要一个 MKMapView 显示带有公开按钮的注释,这会导致像 the Golden Gate Bridge annotation in this Apple sample app 这样的 Vie
为 MKPinAnnotationView 上传自定义图像后,我注意到图钉偏离了中心。引脚应该位于路线折线上的一个点上,并且位于 mkcircle 的中心;但是,图钉似乎位于多段线的右侧和中心偏北的位
有谁知道为什么用于 MKPinAnnotationView 的图像应该稍微偏移(向上和向右),我使用的代码是: pinView = [[MKPinAnnotationView alloc] initW
MKPinAnnotationView pin 的宽度和高度是多少像素? 编辑:更具体地说,是 iPhone 屏幕上包含图钉的假想矩形的宽度和高度。 最佳答案 根据下面的代码,图钉图像和 MKPinA
我继承了一个引发此警告的项目 Incompatible pointer types assigning to 'MKPinAnnotationView *' from 'MKAnnotationVie
我正在尝试在 MKPinAnnotationView 后面添加图像。看起来在这里执行此操作应该相当容易: - (void)mapView:(MKMapView *)mapView didAddAnno
我添加了 MKPinAnnotationView 和 setDragAble。我的代码在这里 - (MKAnnotationView *)mapView:(MKMapView *)mapView vi
我正在使用 MKPinAnnotationView在我的应用程序中。 我正在设置 MapView对象作为委托(delegate),并使用此代码自定义我的 AnnotationView func
我遇到了一个奇怪的问题,即初始的一组图钉未显示自定义图像。 在我的 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotati
我正在尝试根据某些条件为 MKPinAnnotationView 创建三个自定义图钉图像注释。在这种情况下,我将遍历自行车站对象的 for 循环,检查可用自行车的数量是否为 0,如果是,则设置一个 p
我过去有一些使用 MKMapView 和 MKPointAnnotation 的经验,我曾用它们在 map 上放置一些图钉。这次我想更进一步,使用 MKPinAnnotationView 来写一个标签
我是一名优秀的程序员,十分优秀!