【问题标题】:Custom MKAnnotationView with frame,icon and image带有框架、图标和图像的自定义 MKAnnotationView
【发布时间】:2012-05-18 05:10:05
【问题描述】:

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

我对 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;
}

【问题讨论】:

  • 我唯一能做的就是更改注释的图像,但我无法管理如何添加图像、顶部的框架和左下角的图标.我可以用 4 个不同的图标制作 4 个框架,因为我有 4 个不同的注释。位我如何将图标嵌入框架中。
  • 您是否尝试子类化注释视图?
  • 我已经用更多信息更新了这个问题。

标签: iphone objective-c ios cocoa-touch ios5


【解决方案1】:

在你的代码中

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

【讨论】:

    【解决方案2】:
    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    
    
        static NSString *AnnotationViewID = @"annotationViewID";
    
        MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    
        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        }
    
        UIImage *imgPinBorder = [UIImage imageNamed:@"pinBorder.png"];
        UIImageView *imageViewPinBorder = [[UIImageView alloc] initWithImage:imgPinBorder];
        imageViewPinBorder.center = annotationView.center;
        [annotationView addSubview:imageViewPinBorder];
    
        UIImage *img = [UIImage imageNamed:@"myIcon.png"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
        imageView.center = annotationView.center;
        [annotationView addSubview:imageView];
    
        annotationView.annotation = annotation;
    
    
        UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [rightButton addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:annotation.title forState:UIControlStateNormal];
    
        annotationView.rightCalloutAccessoryView = rightButton;
        annotationView.canShowCallout = YES;
        annotationView.draggable = NO;
    
    
    
        return annotationView;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      相关资源
      最近更新 更多