2013-09-02 10 views
0

사용자 지정 AnnotationViews를 사용하는 응용 프로그램에서 작업하고 있습니다. 그것에는 레이블이있는 사용자 정의 이미지와 배경이 있습니다. 배경 및 레이블은 annotationView의 하위 뷰입니다.MKPinAnnotationView subview doft't update

if ([annotation isKindOfClass:[VehicleAnnotation class]]) { 
    VehicleAnnotation *vehicleAnnotation = annotation; 

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier]; 
    if(!pinView) 
    { 
     pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier]; 
    } 

    pinView.annotation = annotation; 

    pinView.canShowCallout = NO; 
    pinView.image = [ImageHelper imageForStatus:vehicleAnnotation.vehicle.driver.driverStatus]; 

    // Add a label with the name of the driver 
    CGRect pinFrame = pinView.frame; 
    UILabel *label = [[UILabel alloc] init]; 
    [label setBackgroundColor:[UIColor clearColor]]; 
    [label setTextColor:[UIColor whiteColor]]; 
    [label setNumberOfLines:2]; 
    [label setLineBreakMode:NSLineBreakByWordWrapping]; 
    [label setFont:[UIFont systemFontOfSize:12]]; 
    label.text = vehicleAnnotation.vehicle.driver.description; 

    CGSize maximumSize = CGSizeMake(100, 50); 
    CGSize myStringSize = [label.text sizeWithFont:label.font 
           constrainedToSize:maximumSize 
            lineBreakMode:label.lineBreakMode]; 

    CGRect labelFrame = CGRectMake(pinFrame.origin.x, pinFrame.origin.y + pinFrame.size.height * 2 + 2, myStringSize.width, myStringSize.height); 
    [label setFrame:labelFrame]; 

    // Add a background to the label. 
    CGFloat offset = 5; 
    CGRect backgroundFrame = CGRectMake(labelFrame.origin.x - offset, labelFrame.origin.y - offset , labelFrame.size.width + 2 * offset, labelFrame.size.height + 2 * offset); 
    UIView *backgroundView = [[UIView alloc] initWithFrame:backgroundFrame]; 
    backgroundView.layer.cornerRadius = 5; 
    backgroundView.layer.masksToBounds = YES; 
    [backgroundView setBackgroundColor:[UIColor darkGrayColor]]; 
    [backgroundView setAlpha:0.8]; 
    [pinView addSubview:backgroundView]; 

    [label setUserInteractionEnabled:YES]; 
    [label setMultipleTouchEnabled:YES]; 
    [pinView addSubview:label]; 

    return pinView; 
} 

이 코드는 새로운 위치가 수신 될 때마다 수행된다. 문제는 오래된 레이블과 오래된 배경이 여전히 존재한다는 것입니다.

MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier]; 
if(!pinView) 
{ 
    pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier]; 
} 
for(UIView *view in [pinView subviews]) 
{ 
    [view removeFromSuperview]; 
} 
pinView.annotation = annotation; 

그러나 더 라벨 나 배경이 표시되지 않습니다 : 그래서 내가이 코드를 넣어 문질러서. 누군가 제가 적절한 방법으로 배경과 라벨을 교체하거나 제거하도록 도와 줄 수 있습니까?

감사합니다.

답변

2

사용자 지정 MKAnnotatinView를 만드는 것이 좋습니다. 그렇지 않으면 위의 코드를 사용하여 MKAnnotationView를 초기화 할 Bock 내부에 Label 및 백그라운드 뷰를 추가하십시오. 아래 그림과 같이 블록 외부의 다른 주석에 대해 변경되는 프레임 및 텍스트 및 기타 속성을 설정합니다. MKPinAnnotationView의 image을 설정하면 효과가

if ([annotation isKindOfClass:[VehicleAnnotation class]]) { 
    VehicleAnnotation *vehicleAnnotation = annotation; 

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:driverAnnotationIdentifier]; 
    if(!pinView) 
    { 
     pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:driverAnnotationIdentifier]; 
    UILabel *label = [[UILabel alloc] init]; 
    [label setBackgroundColor:[UIColor clearColor]]; 
    [label setTextColor:[UIColor whiteColor]]; 
    [label setNumberOfLines:2]; 
    [label setLineBreakMode:NSLineBreakByWordWrapping]; 
    [label setFont:[UIFont systemFontOfSize:12]]; 
    [label setUserInteractionEnabled:YES]; 
    [label setMultipleTouchEnabled:YES]; 
    [label setTag:101]; 

    UIView *backgroundView = [[UIView alloc]init]; 
    [backgroundView setTag:102]; 
    backgroundView.layer.cornerRadius = 5; 
    backgroundView.layer.masksToBounds = YES; 
    [backgroundView setBackgroundColor:[UIColor darkGrayColor]]; 
    [backgroundView setAlpha:0.8]; 
    [pinView addSubView:backgroundView]; 
    [pinView addSubview:label]; 



    } 

    pinView.annotation = annotation; 

    pinView.canShowCallout = NO; 
    pinView.image = [ImageHelper imageForStatus:vehicleAnnotation.vehicle.driver.driverStatus]; 

    // set frame of Label 
    CGRect pinFrame = pinView.frame; 
    UILabel *label = (UILabel *)[pinView viewWithTag:101]; 
    label.text = vehicleAnnotation.vehicle.driver.description; 

    CGSize maximumSize = CGSizeMake(100, 50); 
    CGSize myStringSize = [label.text sizeWithFont:label.font 
           constrainedToSize:maximumSize 
            lineBreakMode:label.lineBreakMode]; 

    CGRect labelFrame = CGRectMake(pinFrame.origin.x, pinFrame.origin.y + pinFrame.size.height * 2 + 2, myStringSize.width, myStringSize.height); 
    [label setFrame:labelFrame]; 

    // set backgroundView frame. 
    CGFloat offset = 5; 
    CGRect backgroundFrame = CGRectMake(labelFrame.origin.x - offset, labelFrame.origin.y - offset , labelFrame.size.width + 2 * offset, labelFrame.size.height + 2 * offset); 
    UIView *backgroundView = [pinView viewWithTag:102]; 
    [backgroundView setFrame:backgroundFrame]; 

    return pinView; 
} 
+0

동일한 코드가이 코드에서 계속 발생합니다. 시작될 때 레이블이없는 배경을 보여주고 업데이트시 배경이 더 이상 표시되지 않습니다. 내 생각에 유일한 해결책은 사용자 정의 MKPinAnnotationView가 될 것입니다. – Michael90

+0

예, 미안합니다. 먼저 하위보기로 backgroundView보다 레이블을 추가했습니다. 나는 코드를 수정했다. 그렇지 않으면 사용자 주석이 답을 찾지 못하는지 확인한다. @ Michael90 –

+0

레이블은 정확하지만 이제는 업데이트 후에 레이블과 배경이 모두 사라진다. 맞춤 주석을 사용하는 것이 좋습니다. – Michael90

1

, 그것을 덮어 쓰게됩니다. 일반 MKAnnotationView를 사용하십시오.

Stackocerflow에서도이 질문을 던지고이 질문에 답하기 불과 6 시간 만에 답변했습니다. MapView Custom Pin Image Issue

+0

이것은 이미지에 관한 것이 아니라 하위 뷰를 추가하고 업데이트하는 것에 관한 것입니다. – Michael90

+0

코드 12 번째 줄은 MKPinAnnotationView의 이미지를 설정하려고 시도합니다. 나는 그들이 관련 있다고 생각한다. 본질적으로 MKPinAnnotationView는 Apple이 어떻게 보이길 원하는지 보입니다. 다른 것을 원하면 MKAnnotationView를 서브 클래스해야합니다. – Craig