Animating a sprite via MKMapView

Without OpenGL involvement (Quartz 2D is fine):

  • Let's say I have an image that I want to move around the map in some way. For example, an image of a plane "flying" over the map. I was able to do this using MKAnnotation, NSTimer and fiddling with lat / lon rate and timer rate. However, I suppose this is not ideal, although the results look pretty decent. Can you think of a better way?

  • Now let's say that I want this image to be animated (think: an animated gif). I can't do the usual UIImageView

    with the series animationFrames

    because everything I have in the MKAnnotationView is there UIImage

    . How do you guys solve this?

I understand that # 2 could be rendered with a UIImageView on top of the map containing the animation. However, then I would have to manually handle the movement of the plane or rocket or whatever, since the viewport of the map changed, depending on the user's movements in the real world or the user's zooming (scrolling is not allowed in my application).

What do you think?

+2


source to share


1 answer


I think I figured out solution # 2. I subclassed MKAnnotationView and wrote some code to add a UIImageView (with animations) as a subview.

//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

      

+5


source







All Articles