在 MKMapView 上对精灵进行动画处理

发布于 2024-08-02 04:40:54 字数 516 浏览 3 评论 0原文

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

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

  2. 现在假设我希望该图像具有动画效果(例如:动画 gif)。我无法使用一系列 animationFrames 执行通常的 UIImageView 操作,因为我在 MKAnnotationView 中可以访问的只是一个 UIImage。你们会如何解决这个问题?

我意识到#2 可以通过包含动画图像的地图顶部的 UIImageView 来处理。但是,当地图视图区域发生变化时,我必须手动处理翻译飞机或火箭或其他任何内容的运动,具体取决于现实世界中的用户运动或用户缩放(我的应用程序中不允许滚动)。

你怎么认为?

Without getting into OpenGL (Quartz 2D is OK):

  1. Let's say I have an image which I want to move across a map in some fluid way. For instance, an image of a plane "flying" across the map. I've been able to do this using an MKAnnotation, an NSTimer and fiddling with the rate of lat/lon change and timer rate. However, I assume this isn't ideal, although the results look pretty decent. Can you think of a better way?

  2. Now let's say that I want this image to be animated (think: animated gif). I can't do the usual UIImageView with a series of animationFrames because all I have access to in MKAnnotationView is a UIImage. How would you guys tackle this?

I realize that #2 could be handled with a UIImageView on top of the map containing the animationImages. However, then I would have to manually handle translating the movement of the plane or rocket or whatever as the region of the mapview changed, depending on user movements in the real-world, or user zooming (scrolling is not allowed in my app).

What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小沫ゞ 2024-08-09 04:40:54

我想我已经找到了#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

//动画注释.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

I think I've figured out a solution to #2. I subclassed MKAnnotationView and wrote some code to add a UIImageView (with animation images) 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文