MKPinAnnotationView:有三种以上的颜色吗?

时间:2009-07-26 21:32:26

标签: iphone cocoa-touch mapkit mkpinannotationview

根据Apple文档,MKPinAnnotationView的引脚颜色有红色,绿色和紫色可供选择。有没有办法得到其他颜色?我在文档中找不到任何内容。

9 个答案:

答案 0 :(得分:81)

更多;)

alt text http://lionel.gueganton.free.fr/pins/pinGray.png enter image description here enter image description here

alt text http://lionel.gueganton.free.fr/pins/pinOrange.png enter image description here enter image description here

原来的那些:

alt text http://lionel.gueganton.free.fr/pins/pinGreen.png alt text enter image description here

alt text http://lionel.gueganton.free.fr/pins/pinPurple.png alt text enter image description here

alt text http://lionel.gueganton.free.fr/pins/pinRed.png alt text enter image description here

代码:

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

答案 1 :(得分:40)

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

alt text alt text 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];
    }
    // ...

答案 2 :(得分:11)

您可以使用ZSPinAnnotation动态创建注释引脚,并使用指定的UIColorhttps://github.com/nnhubbard/ZSPinAnnotation

答案 3 :(得分:8)

我喜欢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;
}

答案 4 :(得分:4)

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

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

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

我不相信这个PSD。我刚刚从http://www.teehanlax.com/downloads/iphone-4-guid-psd-retina-display/抓住它,他们做得很棒!

答案 5 :(得分:4)

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

答案 6 :(得分:3)

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

答案 7 :(得分:2)

我试过这种方式似乎没问题......

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

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

答案 8 :(得分:1)

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

相关问题