MKPinAnnotationView:是否有超过三种颜色可用?

发布于 2024-07-27 20:51:11 字数 88 浏览 4 评论 0 原文

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

According to the Apple docs, MKPinAnnotationView's pin color is available in red, green and purple. Is there any way to get other colors also? I've found nothing in the docs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

山有枢 2024-08-03 20:51:11

更多 ;)

“替代文本”
在此处输入图像描述在此处输入图像描述


在此处输入图像描述在此处输入图像描述

以及原始图像:

替代文字
alt 文本 输入图像此处描述

“替代文本”
替代文本 输入图像此处描述

“替代文本”
alt 文本

和代码:

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

some more ;)

alt text
enter image description hereenter image description here

alt text
enter image description hereenter image description here

And the original ones :

alt text
alt text enter image description here

alt text
alt text enter image description here

alt text
alt text enter image description here

And the code:

- (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;
}
短暂陪伴 2024-08-03 20:51:11

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

alt text
替代文本
替代文本
alt text

以及在 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];
    }
    // ...

You might find the following images useful:

alt text
alt text
alt text
alt text

and the code to use them in 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];
    }
    // ...
蹲在坟头点根烟 2024-08-03 20:51:11

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

You could use ZSPinAnnotation to create annotation pins on the fly with a specified UIColor: https://github.com/nnhubbard/ZSPinAnnotation

恰似旧人归 2024-08-03 20:51:11

我喜欢 Yonel 的答案,但只是一个头向上,当您创建自定义 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;
}

I like Yonel's Answer but just a heads up, when you create a custom MKAnnotationView, you'll have to manually assign the offset. For the images Yonel provided: (you can leave out the calloutButton stuff if you don't need one of those)

#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;
}
仲春光 2024-08-03 20:51:11

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

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

使用此 PSD 来呈现您想要的任何颜色:)

我不将此 PSD 归功于我。 我刚刚从 http://www.teehanlax.com 获取它/downloads/iphone-4-guid-psd-retina-display/ 他们做得非常出色!

And here is the PSD for the pin with shadow and its in @2x size.

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

Use this PSD for any color you want :)

I take no credit for this PSD. I just grabbed it from http://www.teehanlax.com/downloads/iphone-4-guid-psd-retina-display/ They have done a wonderful job!

烙印 2024-08-03 20:51:11

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

With iOS 9, pinTintColor has been added to MKPinAnnotationView, allowing you to supply a UIColor for the pin color.

说好的呢 2024-08-03 20:51:11

如果您使用引脚掉落动画,则所发布的解决方案都不能 100% 工作。 Cannonade 的解决方案非常巧妙,因为它允许别针仍然具有两种末端(掉落时的尖点和带有圆形纸波纹的一端),但不幸的是,当别针弹起时,可以看到原始别针头颜色它击中了地图。 约内尔的解决方案是替换整个图钉图像,这意味着图钉在击中地图之前就随着圆形纸波纹落下!

Neither of the posted solutions work 100% if you are using the pin drop animation. Cannonade's solution is very neat because it allows the pin to still have both kinds of ends (the sharp point when falling and the one with the circular paper ripple) but unfortunately a glimpse of the original pin head colour can be seen when the pin bounces as it hits the map. yonel's solution of replacing the whole pin image means the pin falls with the circular paper ripple before it's even hit the map!

你与昨日 2024-08-03 20:51:11

我尝试了这种方式,似乎没问题...

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

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

I tried this way and it seems to be ok...

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

using the complete pin image... as the yonel example

地狱即天堂 2024-08-03 20:51:11

如果它不在文档中,那么很可能不会,如果您愿意,您可以使用 mkannotationview 并拥有自己的图像

If it's not in the docs then most probably not, you cAn use mkannotationview and have ur own image if u wish though

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文