0

MKAnnotationView サブクラス内で画像ビューを変更する際に問題が発生しています。ユーザーが特定の時点にいた場所に関するデータに基づいて、注釈の位置を更新するタイマーがあります。これは正常に機能し、注釈の位置が正しく更新されます。私は次のコードでこれを行います:

[_myAnnotation setCoordinate:point.position];

この下に、ユーザーが東または西に向かっていたかどうかも計算します。setIsMovingRight:YESユーザーが東に向かっている場合はクラスのメソッドを呼び出し、MyAnnotationユーザーが西に向かっている場合はその逆を行います。

メソッド内で NSLog を使用して値が変更されるたびに出力することにより、メソッドが正しい時間に正しい値で呼び出されていることを確認しました。setIsMovingRightただし、メソッドが注釈の視覚的な外観に影響を与えているようには見えません。メソッドが呼び出されるたびにイメージビューの背景色を赤に設定しようとしましたが、これでも効果はありません。さまざまな場所で 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];
}

誰でもできる助けやアドバイスをいただければ幸いです。ありがとう!

4

1 に答える 1

1

注釈ビューはKVOを使用して変更をリッスンしていると思います。imageプロパティを変更してみましたか?何かのようなもの[self setImage:myNewImage]

于 2012-07-29T13:38:24.530 に答える