我正在进行willChangeValueForKey
和didChangeValueForKey
调用,但即使使用它们,mapView委托也不会通过mapView: viewForAnnotation:
来更新事物。如何强制MKAnnotation
更新?
图像更改时annotationView.image
未更新,annotationView.image
也不会更新。据我所知,我的NSLog行可以在更新mapView: viewForAnnotation:
时再次调用MKAnnotation
。
·H
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@interface MapAnnotation : NSObject <MKAnnotation>
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, retain) NSString *currentTitle;
@property (nonatomic, retain) NSString *currentSubTitle;
@property (nonatomic, retain) NSString *category;
@property (nonatomic, retain) NSString *pin;
- (NSString*) title;
- (NSString*) subtitle;
- (id) initWithCoordinate:(CLLocationCoordinate2D) c;
- (CLLocationCoordinate2D) getCoordinate;
- (void) addPlace:(MapAnnotation*) place;
- (int) placesCount;
- (void) cleanPlaces;
- (UIImage *) getPin;
@end
的.m
#import "MapAnnotation.h"
@interface MapAnnotation()
@property (strong) NSMutableArray *places;
@end
@implementation MapAnnotation
@synthesize coordinate = _coordinate;
@synthesize currentTitle = _currentTitle;
@synthesize currentSubTitle = _currentSubTitle;
@synthesize places = _places;
@synthesize category = _category;
@synthesize pin = _pin;
- (NSString*) subtitle {
if ([self placesCount] == 1) {
return self.currentSubTitle;
}
else{
return @"";
}
}
- (NSString*) title {
if ([self placesCount] == 1) {
return self.currentTitle;
}
else{
return [NSString stringWithFormat:@"%d places", [self placesCount]];
}
}
- (void)addPlace:(MapAnnotation *)place {
[self willChangeValueForKey:@"title"];
[self willChangeValueForKey:@"subtitle"];
[self.places addObject:place];
[self didChangeValueForKey:@"title"];
[self didChangeValueForKey:@"subtitle"];
}
- (CLLocationCoordinate2D)getCoordinate {
return self.coordinate;
}
- (void)cleanPlaces {
[self willChangeValueForKey:@"title"];
[self willChangeValueForKey:@"subtitle"];
[self.places removeAllObjects];
[self.places addObject:self];
[self didChangeValueForKey:@"title"];
[self didChangeValueForKey:@"subtitle"];
}
- (id)initWithCoordinate:(CLLocationCoordinate2D) c {
self.coordinate=c;
self.places=[[NSMutableArray alloc] initWithCapacity:0];
return self;
}
- (int)placesCount {
return [self.places count];
}
- (UIImage *)getPin {
// begin a graphics context of sufficient size
CGSize size = CGSizeMake(26, 26);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
// get the context for CoreGraphics
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect imageRect = CGRectMake(0, 0, size.width, size.height);
// Draw Dot
CGRect circleRect = CGRectInset(imageRect, 4, 4);
[[UIColor blackColor] setStroke];
[[UIColor yellowColor] setFill];
CGContextFillEllipseInRect(ctx, circleRect);
CGContextStrokeEllipseInRect(ctx, circleRect);
// Dot Content
[[UIColor blackColor] setStroke];
[[UIColor blackColor] setFill];
CGAffineTransform transform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(ctx, transform);
CGContextSetLineWidth(ctx, 2.0);
CGContextSetCharacterSpacing(ctx, 1.7);
CGContextSetTextDrawingMode(ctx, kCGTextFill);
UIFont *font = [UIFont fontWithName:@"Arial" size:11.0];
if ([self placesCount] != 1) {
NSString *label = [NSString stringWithFormat:@"%d", [self placesCount]];
CGSize stringSize = [label sizeWithAttributes:@{NSFontAttributeName:font}];
[label drawAtPoint:CGPointMake(size.width / 2 - stringSize.width / 2, size.height / 2 - stringSize.height / 2)
withAttributes:@{NSFontAttributeName:font}];
} else {
NSString *label = [NSString stringWithFormat:@"%@", self.pin];
CGSize stringSize = [label sizeWithAttributes:@{NSFontAttributeName:font}];
[label drawAtPoint:CGPointMake(size.width / 2 - stringSize.width / 2, size.height / 2 - stringSize.height / 2)
withAttributes:@{NSFontAttributeName:font}];
}
// make image out of bitmap context
UIImage *retImage = UIGraphicsGetImageFromCurrentImageContext();
// free the context
UIGraphicsEndImageContext();
return retImage;
}
@end
mapView:viewForAnnotation:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnoation"];
if(!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"MyAnnoation"];
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
}
annotationView.enabled = YES;
if ([(MapAnnotation*)annotation placesCount] == 1) {
annotationView.canShowCallout = YES;
}
annotationView.image = [(MapAnnotation*)annotation getPin];
NSLog(@"%@", [annotation title]);
return annotationView;
}
答案 0 :(得分:1)
如评论中所述,除title
和subtitle
外,对注记属性的更改不会自动导致视图更新。
更改title
和subtitle
会自动更新标注,只要更改会导致KVO通知(例如,如果使用默认设置器ann.title = xxx
或手动发出KVO通知,因为问题中的代码正在执行,因为您有自定义的getter。)
要自动更新图像,您可能需要使用自定义注释视图来观察对基础注释属性的更改并刷新自身(在init
调用addObserver
中{{1}在observeValueForKeyPath
调用dealloc
)中更新图片。您也可以使用非KVO方法(例如自定义通知或委托)(但仍使用自定义注释视图)来执行此操作。
使用现有代码,快速手动更新注释视图而不创建自定义注释视图和使用KVO等是在注释更新后立即获取注释的当前视图并更新{{1}属性。只要removeObserver
中的代码具有设置image
的相同逻辑,这应该有效。
我假设应用程序中某处(有时在注释已添加之后),它通过调用viewForAnnotation
来更新注释中的位置。之后,您可以尝试以下方法:
image
不相关但当前addPlace
无法正确处理除自定义注释之外的可能的视图重用和/或注释类型(例如会导致当前代码崩溃的用户位置蓝点)。我建议将其更改为:
[someExistingAnnotation addPlace:anotherPlace];
//obtain the current view of someExistingAnnotation...
MKAnnotationView *av = [mapView viewForAnnotation:someExistingAnnotation];
//update its image property to force refresh...
av.image = [someExistingAnnotation getPin];
//also set canShowCallout (it might have been NO)