Анимация спрайта в MKMapView

Не залезая в OpenGL (Quartz 2D в порядке):

  1. Допустим, у меня есть изображение, которое я хочу плавно перемещать по карте. Например, изображение самолета, «летающего» по карте. Я смог сделать это, используя MKAnnotation, NSTimer и играя со скоростью изменения широты/долготы и скоростью таймера. Однако я предполагаю, что это не идеально, хотя результаты выглядят довольно прилично. Можете ли вы придумать лучший способ?

  2. Теперь предположим, что я хочу, чтобы это изображение было анимировано (например, анимированный gif). Я не могу сделать обычный UIImageView с серией animationFrames, потому что все, к чему у меня есть доступ в MKAnnotationView, это UIImage. Как бы вы, ребята, справились с этим?

Я понимаю, что № 2 можно обрабатывать с помощью UIImageView поверх карты, содержащей анимационные изображения. Однако тогда мне пришлось бы вручную переводить движение самолета, ракеты или чего-то еще, поскольку область просмотра карты менялась в зависимости от движений пользователя в реальном мире или масштабирования пользователя (прокрутка не разрешена в моем приложении).

Что вы думаете?


person Ron Gejman    schedule 20.08.2009    source источник


Ответы (1)


Я думаю, что нашел решение № 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
person Ron Gejman    schedule 20.08.2009