사용자 지정 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;
그러나 더 라벨 나 배경이 표시되지 않습니다 : 그래서 내가이 코드를 넣어 문질러서. 누군가 제가 적절한 방법으로 배경과 라벨을 교체하거나 제거하도록 도와 줄 수 있습니까?
감사합니다.
동일한 코드가이 코드에서 계속 발생합니다. 시작될 때 레이블이없는 배경을 보여주고 업데이트시 배경이 더 이상 표시되지 않습니다. 내 생각에 유일한 해결책은 사용자 정의 MKPinAnnotationView가 될 것입니다. – Michael90
예, 미안합니다. 먼저 하위보기로 backgroundView보다 레이블을 추가했습니다. 나는 코드를 수정했다. 그렇지 않으면 사용자 주석이 답을 찾지 못하는지 확인한다. @ Michael90 –
레이블은 정확하지만 이제는 업데이트 후에 레이블과 배경이 모두 사라진다. 맞춤 주석을 사용하는 것이 좋습니다. – Michael90