【问题标题】:MKPinAnnotationView: Are there more than three colors available?MKPinAnnotationView:是否有超过三种颜色可用?
【发布时间】:2010-11-14 05:03:44
【问题描述】:

根据 Apple 文档,MKPinAnnotationView 的引脚颜色有红色、绿色和紫色。有什么办法也可以得到其他颜色吗?我在文档中没有找到任何内容。

【问题讨论】:

标签: iphone cocoa-touch mapkit mkpinannotationview


【解决方案1】:

还有一些;)

还有原来的:

还有代码:

- (MKAnnotationView*)mapView:(MKMapView*)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKPinAnnotationView* anView =[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"test"];
anView.pinColor=MKPinAnnotationColorPurple;
UIImage* image = nil;
// 2.0 is for retina. Use 3.0 for iPhone6+, 1.0 for "classic" res.
UIGraphicsBeginImageContextWithOptions(anView.frame.size, NO, 2.0);
[anView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData* imgData = UIImagePNGRepresentation(image);
NSString* targetPath = [NSString stringWithFormat:@"%@/%@", [self writablePath], @"thisismypin.png" ];
[imgData writeToFile:targetPath atomically:YES]; 
return anView;
}

-(NSString*) writablePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}

【讨论】:

  • 您好,您是如何获得原始图钉的?谢谢。有可用的@2x 版本吗?
  • 我在模拟器中运行了一段代码,它使用 UIImagePNGRepresentation(yourImage) 将 UIImage 写入文件系统。图片 -> NSData -> 文件系统。我没有可用的@2x 版本:/
  • matt,我为针脚添加了视网膜显示。以及用于渲染它们的代码。
  • 图片链接已损坏。你能修好它们吗?丹克
  • 奇怪,因为你是对的,前段时间链接坏了,但现在我可以看到它们回来了....:/你没看到吗?
【解决方案2】:

您可能会发现以下图片很有用:

以及在viewForAnnotation中使用它们的代码:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{   
    // ... get the annotation delegate and allocate the MKAnnotationView (annView)
    if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame)
    {
        UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
        [annView addSubview:imageView];
    }
    // ...

【讨论】:

  • 没问题。很高兴您发现它们很有用。
【解决方案3】:

您可以使用ZSPinAnnotation 使用指定的UIColor 动态创建注释图钉:https://github.com/nnhubbard/ZSPinAnnotation

【讨论】:

  • 嘿@Nic-Hubbard - 感谢分享代码。是您不将其添加为 MKAnnotation 上的类别以让您设置 pinView.image = [MKAnnotation pinAnnotationWithColor:a.color] 的原因
  • @earnshavian 真的没有理由,我想我当时没有考虑过!
【解决方案4】:

我喜欢Yonel's Answer,但请注意,当您创建自定义 MKAnnotationView 时,您必须手动分配偏移量。对于 Yonel 提供的图片:(如果您不需要其中之一,可以省略 calloutButton 的内容)

#pragma mark MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)aMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if(![annotation isKindOfClass:[MyAnnotation class]]) // Don't mess user location
        return nil;

    MKAnnotationView *annotationView = [aMapView dequeueReusableAnnotationViewWithIdentifier:@"spot"];
    if(!annotationView)
    {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"spot"];
        annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [(UIButton *)annotationView.rightCalloutAccessoryView addTarget:self action:@selector(openSpot:) forControlEvents:UIControlEventTouchUpInside];
        annotationView.enabled = YES;
        annotationView.canShowCallout = YES;
        annotationView.centerOffset = CGPointMake(7,-15);
        annotationView.calloutOffset = CGPointMake(-8,0);
    }

    // Setup annotation view
    annotationView.image = [UIImage imageNamed:@"pinYellow.png"]; // Or whatever

    return annotationView;
}

【讨论】:

  • 顺便说一下,如果annotationView成功地使注解视图出列,则需要更新其annotation属性。例如,您通常会有一个if (!annotationView) { ... } else { annotationView.annotation = annotation; }
【解决方案5】:

这里是带有阴影的引脚的 PSD,其大小为 @2x。

http://dl.dropbox.com/u/5622711/ios-pin.psd

将此 PSD 用于您想要的任何颜色 :)

我不相信这个 PSD。我刚刚从http://www.teehanlax.com/downloads/iphone-4-guid-psd-retina-display/ 那里拿到了它,他们做得很好!

【讨论】:

    【解决方案6】:

    在 iOS 9 中,pinTintColor 已添加到 MKPinAnnotationView,允许您为引脚颜色提供 UIColor

    【讨论】:

      【解决方案7】:

      如果您使用的是图钉放置动画,则发布的解决方案都不能 100% 工作。 Cannonade 的解决方案非常简洁,因为它允许大头针仍然有两种末端(落下时的尖点和带有圆形纸波纹的末端),但不幸的是,当大头针弹跳时可以看到原始针头颜色的一瞥它击中了地图。 yonel 的替换整个 pin 图像的解决方案意味着 pin 甚至在击中地图之前就随着圆形纸波纹掉落!

      【讨论】:

        【解决方案8】:

        我试过这个方法,好像还可以……

        UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
                UIImageView *imageView = [[[UIImageView alloc] initWithImage:image]
                                         autorelease];
                [annotationView addSubview:imageView];
                annotationView = nil;
        

        使用完整的 pin 图像...作为 yonel 示例

        【讨论】:

        • 仅适用于 iOS 8 的代码(其他答案无济于事)。谢谢!
        【解决方案9】:

        如果文档中没有,那么很可能没有,您可以使用 mkannotationview 并拥有自己的图像(如果您愿意)

        【讨论】:

          猜你喜欢
          • 2012-11-07
          • 2015-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-30
          • 1970-01-01
          相关资源
          最近更新 更多