我在改变MKAnnotationView子类中的图像视图时遇到了一些问题。我有一个计时器,根据用户在某个特定点的位置的数据更新注释位置。这工作正常,注释位置正确更新。我这样做是用下面的代码:更改MKMapKit MKAnnotation子类中的图像
[_myAnnotation setCoordinate:point.position];
低于这个,我还计算出用户是否向东或向西。如果用户向东行驶,则在类MyAnnotation
上调用setIsMovingRight:YES
方法,而在用户向西行驶时做相反的操作。
我已经验证了方法是在正确的时间使用正确的值调用,方法是在方法内部使用NSLog来打印出值的变化。但是,似乎setIsMovingRight
方法对注释的外观没有任何影响。我尝试在调用方法时将imageview的背景颜色设置为红色,但即使这样也没有效果。我曾尝试在各个地方调用setNeedsDisplay而没有成功。
我可以添加和重新创建一个注释视图,但是只要位置发生变化,注释就会闪烁,并且看起来确实不太好。
以下是我的自定义注释视图“MyAnnotation”的代码。
@interface MyAnnotation : MKAnnotationView <MKAnnotation>
{
UIImageView *_imageView;
BOOL _currentlyMovingRight;
}
@property (nonatomic) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic,retain) UIImage *image;
-(void)setIsMovingRight:(BOOL)movingRight;
@end
@implementation MyAnnotation
@synthesize coordinate, title, subtitle, image;
-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if(self)
{
_currentlyMovingRight = NO;
_imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
[self addSubview:_imageView];
}
return self;
}
-(void)setIsMovingRight:(BOOL)movingRight
{
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
[_imageView setBackgroundColor:[UIColor redColor]]; // THIS LINE NEVER DOES ANYTHING.
if(movingRight && !_currentlyMovingRight)
{
NSLog(@"right now.");
[_imageView setImage:[UIImage imageNamed:@"right.png"]];
}
else if (!movingRight && _currentlyMovingRight)
{
NSLog(@"left now.");
[_imageView setImage:[UIImage imageNamed:@"left.png"]];
}
_currentlyMovingRight = movingRight;
[self addSubview:_imageView];
[self setNeedsDisplay];
}
我会很感激任何人都可以给予的帮助或建议。谢谢!