2009-08-20 8 views
2

全体でスプライトをアニメーション:は、OpenGL(クォーツ2DはOKです)に取得せずにMKMapView

  1. のは、私は私はいくつかの流体の方法でマップ上を移動したい画像があるとしましょう。例えば、地図を横切って飛行する飛行機の画像。私はこれを、MKAnnotation、NSTimer、そして緯度/経度の変化率とタイマーレートを使って行うことができました。しかし、私はこれが理想的ではないと考えていますが、結果はかなり上品です。あなたはより良い方法を考えることができますか?

  2. ここで、この画像をアニメーション化したいとしましょう(アニメーションgifと考える)。私は通常、UIImageViewanimationFramesのシリーズで実行することはできません。なぜなら、私がMKAnnotationViewにアクセスできるのはUIImageであるからです。あなたはどうやってこれに取り組んでいますか?

アニメーションイメージを含むマップの上に#2をUIImageViewで処理できることがわかりました。しかし、実際のユーザーの動きやユーザーのズーム(私のアプリではスクロールが許可されていません)に応じて、飛行機やロケットの動きやマップビューの領域が変更されたときのように、手動で処理する必要があります。

あなたはどう思いますか?

答えて

5

私は#2の解決策を考え出したと思います。私はMKAnnotationViewをサブクラス化し、UIImageView(アニメーションイメージ付き)をサブビューとして追加するコードを書きました。

//AnimatedAnnotation.h 

#import <Foundation/Foundation.h> 
#import <MapKit/MapKit.h> 

@interface AnimatedAnnotation : MKAnnotationView 
{ 
    UIImageView* _imageView; 
    NSString *imageName; 
    NSString *imageExtension; 
    int imageCount; 
    float animationDuration; 
} 

@property (nonatomic, retain) UIImageView* imageView; 
@property (nonatomic, retain) NSString* imageName; 
@property (nonatomic, retain) NSString* imageExtension; 
@property (nonatomic) int imageCount; 
@property (nonatomic) float animationDuration; 


- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageName:(NSString *)name imageExtension:(NSString *)extension imageCount:(int)count animationDuration:(float)duration 
; 

@end 

//AnimatedAnnotation.m

#import "AnimatedAnnotation.h" 

@implementation AnimatedAnnotation 
@synthesize imageView = _imageView; 
@synthesize imageName, imageCount, imageExtension,animationDuration; 

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier imageName:(NSString *)name imageExtension:(NSString *)extension imageCount:(int)count animationDuration:(float)duration 
{ 
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; 
    self.imageCount = count; 
    self.imageName = name; 
    self.imageExtension = extension; 
    self.animationDuration = duration; 
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@0.%@",name,extension]]; 
    self.frame = CGRectMake(0, 0, image.size.width, image.size.height); 
    self.backgroundColor = [UIColor clearColor]; 


    _imageView = [[UIImageView alloc] initWithFrame:self.frame]; 
    NSMutableArray *images = [[NSMutableArray alloc] init]; 
    for(int i = 0; i < count; i++){ 
     [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@%d.%@", name, i, extension]]]; 
    } 


    _imageView.animationDuration = duration; 
    _imageView.animationImages = images; 
    _imageView.animationRepeatCount = 0; 
    [_imageView startAnimating]; 

    [self addSubview:_imageView]; 

    return self; 
} 

-(void) dealloc 
{ 
    [_imageView release]; 
    [super dealloc]; 
} 


@end 
関連する問題