iOS地图SDK提供从天而降动画,仅BMKPinAnnotationView及继承BMKPinAnnotationView的子类支持此动画。
显示动画
需要显示此动画效果,只需将BMKPinAnnotationView的animatesDrop属性设置为YES,代码如下:
//设置从天而降的动画效果annotationView.animatesDrop = YES;
运行程序
效果如下:
利用iOS系统UIImageView提供的animationImages来实现帧动画,同时需要自定义BMKAnnotationView来实现点标记的帧动画。
#import <UIKit/UIKit.h>@interface AnimationAnnotationView: BMKAnnotationView//存储每一帧图片@property (nonatomic, strong) NSMutableArray *annotationImages;//展示帧动画的UIImageView@property (nonatomic, strong) UIImageView *annotationImageView;@end#import "AnimationAnnotationView.h"@implementation AnimationAnnotationView- (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];if (self) {[self setBounds:CGRectMake(0.f, 0.f, 32.f, 32.f)];[self setBackgroundColor:[UIColor clearColor]];_annotationImageView = [[UIImageView alloc] initWithFrame:self.bounds];_annotationImageView.contentMode = UIViewContentModeCenter;[self addSubview:_annotationImageView];}return self;}- (void)setAnnotationImages:(NSMutableArray *)images {_annotationImages = images;[self updateImageView];}- (void)updateImageView {if ([_annotationImageView isAnimating]) {[_annotationImageView stopAnimating];}_annotationImageView.animationImages = _annotationImages;_annotationImageView.animationDuration = 0.5 * [_annotationImages count];_annotationImageView.animationRepeatCount = 0;[_annotationImageView startAnimating];}@end
//初始化标注类BMKPointAnnotation的实例BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init];//设置标注的经纬度坐标annotation.coordinate = CLLocationCoordinate2DMake(39.915, 116.404);/**向地图窗口添加标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:方法来生成标注对应的View@param annotation 要添加的标注*/[_mapView addAnnotation:annotation];
#pragma mark - BMKMapViewDelegate/**根据anntation生成对应的annotationView@param mapView 地图View@param annotation 指定的标注@return 生成的标注View*/- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation {//动画annotationNSString *AnnotationViewID = @"AnimatedAnnotation";AnimationAnnotationView *annotationView = nil;if (annotationView == nil) {annotationView = [[AnimationAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];}NSMutableArray *images = [NSMutableArray array];for (int i = 1; i < 4; i++) {UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"poi-%d.png", i]];[images addObject:image];}annotationView.annotationImages = images;return annotationView;}
效果如下:
上一篇
下一篇
本篇文章对您是否有帮助?