在MKMapView上设置精灵动画

时间:2009-08-20 03:06:07

标签: iphone animation mapkit

没有进入OpenGL(Quartz 2D就可以了):

  1. 假设我有一张图像,我希望以某种流畅的方式在地图上移动。例如,飞机在地图上“飞行”的图像。我已经能够使用MKAnnotation,NSTimer和摆弄纬度/经度变化率和定时器速率来做到这一点。但是,我认为这并不理想,虽然结果看起来相当不错。你能想到一个更好的方法吗?

  2. 现在让我们说我希望这个图像是动画的(想想:动画gif)。我无法使用一系列UIImageView执行常规animationFrames,因为我在MKAnnotationView中访问的所有内容都是UIImage。你们怎么解决这个问题?

  3. 我意识到可以使用包含animationImages的地图顶部的UIImageView来处理#2。然而,根据用户在现实世界中的移动或用户缩放(我的应用程序中不允许滚动),我将不得不手动处理平面或火箭的移动或者地图视图区域的任何变化。

    您怎么看?

1 个答案:

答案 0 :(得分: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