iphone - 带有框架、图标和图像的自定义 MKAnnotationView

标签 iphone objective-c ios cocoa-touch ios5

我一直在寻找类似的解决方案来设计像这样的自定义 MKAnnotationView。

enter image description here

我已经对 MKAnnotation 进行了子类化,并且我能够添加一个名为 F.png 的图像 F.png 是如图所示的帧图像。 我想要的是添加一个内部图像。 (我画的图中蓝色)

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{    
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;
static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
if(annotationView)
    return annotationView;
else
{
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annotationView.canShowCallout = YES;
    annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]];        
    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    annotationView.rightCalloutAccessoryView = rightButton;
    annotationView.canShowCallout = YES;
    annotationView.draggable = NO;
    return annotationView;
}
return nil;
}

最佳答案

在你的代码中

else
{
    MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annotationView.canShowCallout = YES;

    //change here
    annotationView.image = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"]];  

    UIImage *frame = [UIImage imageNamed:[NSString stringWithFormat:@"F.png"];
    UIImage *image = theImageInFrameInner;

    UIGraphicsBeginImageContext(CGSizeMake(pin.size.width, pin.size.height));

    [frame drawInRect:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [image drawInRect:CGRectMake(2, 2, 60, 60)]; // the frame your inner image
    //maybe you should draw the left bottom icon here, 

    //then set back the new image, done
    annotationView.image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [rightButton addTarget:self action:@selector(writeSomething:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:annotation.title forState:UIControlStateNormal];

    annotationView.rightCalloutAccessoryView = rightButton;
    annotationView.canShowCallout = YES;
    annotationView.draggable = NO;
    return annotationView;
}

关于iphone - 带有框架、图标和图像的自定义 MKAnnotationView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10520274/

相关文章:

ios - iTunes Search Lookup for all a Developers Apps

iPhone UIView动画最佳实践

ios - 如何在 JSONRequestOperation 上向 AFNetworking 添加自定义 header

ios - 如何让 UIView 检测到父尺度的变化?

ios - 在iOS上下载Dailymotion视频

iphone - tableView 单元格仅在模拟器中填充

ios - 文件下载器使用 NSURLSessionTask downloadTask

ios - 检索框架外的 Storyboard 元素

iphone - 我该怎么做才能调整 UIView 的大小以适合其 subview ?

ios - NSCaseInsensitiveSearch RangeOfString 搜索 Swift