且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MKAnnotationView图像更改问题

更新时间:2022-11-02 09:46:08

viewForAnnotation中无法正确处理出队,因为当dequeueReusableAnnotationViewWithIdentifier返回以前使用的视图时(当annotationView不是nil时) ,代码仅更新该视图的annotation属性:

The dequeue is not being handled properly in viewForAnnotation because when dequeueReusableAnnotationViewWithIdentifier returns a previously-used view (when annotationView is not nil), the code is only updating that view's annotation property:

if (annotationView == nil) {
    annotationView = [location getAnnotationView];
} else {
    annotationView.annotation = annotation;
}

但是注释视图的image不会更新-在出队视图中,image将设置为与为该视图最初创建的注释相关联的注释(当getAnnotationView被调用).

But the annotation view's image is not updated -- in a dequeued view, the image will be set to the one associated with the annotation the view was originally created for (when getAnnotationView was called).

因此,现在视图出现在新注释的坐标处,但是图像仍来自该视图用于的先前注释.

So now the view appears at the new annotation's coordinates but the image is still from the previous annotation the view was used for.


有多种解决方法,例如创建MKAnnotationView的适当子类,以监视其annotation属性的更改并自动更新与注释关联的所有其他属性.


There are various ways to fix this such as creating a proper subclass of MKAnnotationView that monitors changes to its annotation property and automatically updates all other properties associated with an annotation.

使用现有代码,一种简单的解决方法是将特定于注释的属性更改分离为一个单独的方法,该方法在创建视图和更新其annotation属性时都可以调用.

With the existing code, a simple way to fix it is to separate out the annotation-specific property changes into a separate method that can be called both when the view is created and when its annotation property is updated.

例如,在CompanyAnnotation中,创建如下所示的方法:

For example, in CompanyAnnotation, create a method like this:

-(void)configureView:(MKAnnotationView *)av
{
    av.image = self.pinImage;
}

然后将getAnnotationView更改为:

- (MKAnnotationView *)getAnnotationView {
    MKAnnotationView * annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:@"companyAnnotation"];

    //set properties here that are not specific to an annotation...
    [annotationView setEnabled:YES];
    [annotationView setCanShowCallout:YES];
    [annotationView setContentMode:UIViewContentModeScaleAspectFit];
    //[annotationView setImage:self.pinImage];
    [annotationView setFrame:CGRectMake(0, 0, 28, 44)];
    [annotationView setRightCalloutAccessoryView:[UIButton buttonWithType:UIButtonTypeDetailDisclosure]];

    //set properties that are specific to an annotation...
    [self configureView:annotationView];

    return annotationView;
}

最后在viewForAnnotation:

if (annotationView == nil) {
    annotationView = [location getAnnotationView];
} else {
    annotationView.annotation = annotation;

    if ([annotation isKindOfClass:[CompanyAnnotation class]]) {
        //update image, etc...
        [annotation configureView:annotationView];
    }
}

请注意,MapAnnotationpinImageName属性将具有相同的问题.

Note that MapAnnotation will have the same issue with the pinImageName property.