objective c - 隐藏自定义注释视图

时间:2012-01-13 12:24:16

标签: objective-c

我想要一个自定义Annotation视图,其行为与标准视图完全相同,但我需要在其中包含图像和几个文本,这就是我在http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html上实现教程的原因

但我的问题是我希望注释视图隐藏并显示一个引脚,与默认注释视图的行为相同,但所有注释都显示出来,我无法找到隐藏它们的方法。

有什么想法吗?

感谢。

[编辑] 我目前对viewForAnnotation的实现是:

- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(@"Item añadido");

static NSString *AnnotationViewID = @"annotationViewID";



CustomMKAnnotationView *annotationView =

(CustomMKAnnotationView *)[mapa dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];

if (annotationView == nil)

{

    annotationView = [[[CustomMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];

}



annotationView.annotation = annotation;



return annotationView;


}

因为我需要标准的泡泡,但有一个图像和几个UILabel。但是我想保持标准行为,也就是说,当气泡没有显示时有一个引脚,当你点击它时会显示气泡。我的自定义气泡的内容在“CustomMKAnnotationView”中实现。 具体如下:

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

if (self != nil)

{

    CGRect frame = self.frame;

    frame.size = CGSizeMake(10.0, 10.0);

    self.frame = frame;

    // self. = [super pincolor];
    self.backgroundColor = [UIColor clearColor];

    self.centerOffset = CGPointMake(10.0, 10.0);

}

return self;

}
- (void)drawRect:(CGRect)rect{

CustomMKAnnotation *custom = (CustomMKAnnotation *)self.annotation;

if (custom != nil)

{   
    NSLog(@"El nombre es: %@", [custom nombre]);

    UILabel *nombre = [[UILabel alloc]init];
    UILabel *media = [[UILabel alloc]init];

    PDColoredProgressView *barrita = [[PDColoredProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    [barrita setTintColor:[UIColor colorWithRed:0.6 green:0.83 blue:0.91 alpha:1.0f]];


    nombre.textColor = [UIColor whiteColor];
    media.textColor = [UIColor whiteColor];
    nombre.font = [UIFont fontWithName:@"DIN-Bold" size:14];
    media.font = [UIFont fontWithName:@"DIN-Medium" size:12];
    CGSize size = [[custom nombre] sizeWithFont:nombre.font constrainedToSize:CGSizeMake(300, 20)                                                                             lineBreakMode:nombre.lineBreakMode];

    NSLog(@"el ancho es: %f y alto %f", size.width, size.height);

    nombre.backgroundColor = [UIColor clearColor];
    media.backgroundColor = [UIColor clearColor];
    nombre.text = [custom nombre];
    barrita.progress = [custom gente];

    media.text = [NSString stringWithFormat:@"Media %@ años", [custom media]];

    UIImageView *fondo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bubble_map.png"]];


    nombre.frame = CGRectMake(10, 10, size.width, size.height);
    media.frame = CGRectMake(10, size.height + 10, size.width, size.height);
    barrita.frame = CGRectMake(10, media.frame.origin.y + 20, size.width, 10); 
    fondo.frame =  CGRectMake(-((size.width+ 20.0f)/2), -((size.height +10)*2 + 20)-10, size.width+ 20.0f, (size.height +10)*2 + 20);

    fondo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    nombre.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;


    [fondo addSubview:nombre];
    [fondo addSubview:media];
    [fondo addSubview:barrita];
    [self addSubview:fondo];

    [fondo release];
    [nombre release];
    [media release];

}
}

1 个答案:

答案 0 :(得分:1)

如果您的意思是隐藏pin的详细信息,是否尝试创建自定义MKPinAnnotationView并将其属性设置为canShowCallout=NO;

在mapview委托方法中:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id    <MKAnnotation>)annotation
{

 MKPinAnnotationView*pinView;

if([annotation isKindOfClass:[<yourannotationclass> class]])
{

    static NSString*annoIdentifier=@"AnnotationIdentifier";

    pinView=(MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annoIdentifier];

    if(pinView==nil)
    {
        pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoIdentifier]autorelease ];
    }

    pinView.animatesDrop=NO;
    pinView.canShowCallout=NO;
    pinView.pinColor=MKPinAnnotationColorRed;


}

   return pinView;
}