2

无需进入 OpenGL(Quartz 2D 即可):

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

  2. 现在假设我希望这张图片是动画的(想想:动画 gif)。我不能像往常一样UIImageView使用一系列,animationFrames因为我在 MKAnnotationView 中可以访问的只是一个UIImage. 你们将如何解决这个问题?

我意识到 #2 可以使用包含动画图像的地图顶部的 UIImageView 来处理。但是,然后我将不得不手动处理飞机或火箭的运动,或者随着地图视图区域的变化,这取决于现实世界中的用户运动或用户缩放(我的应用程序中不允许滚动)。

你怎么看?

4

1 回答 1

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
于 2009-08-20T04:42:13.077 回答