更新时间:2020-06-23
从天而降动画
iOS地图SDK提供从天而降动画,仅BMKPinAnnotationView及继承BMKPinAnnotationView的子类支持此动画。
显示动画
需要显示此动画效果,只需将BMKPinAnnotationView的animatesDrop属性设置为YES,代码如下:
Objective-C
Swift
//设置从天而降的动画效果 annotationView.animatesDrop = YES;
//设置从天而降的动画效果 annotationView?.animatesDrop = true
显示动画
效果如下
http://mapopen-pub-iossdk.cdn.bcebos.com/newIos/PinAnnotation.mp4
帧动画
利用iOS系统UIImageView提供的animationImages来实现帧动画,同时需要自定义BMKAnnotationView来实现点标记的帧动画。
1自定义AnimatedAnnotationView
Objective-C
Swift
#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
import UIKit class BMKAnimationAnnotationView: BMKAnnotationView { override init!(annotation: BMKAnnotation!, reuseIdentifier: String!) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) frame = CGRect(x: 0, y: 0, width: 32, height: 32) let annotationImage = UIImageView(frame: self.frame) annotationImage.animationImages = [UIImage(named: "greenAnimationIcon")!, UIImage(named: "blackAnimationIcon")!, UIImage(named: "redAnimationIcon")!] annotationImage.animationDuration = 0.5 * 3 annotationImage.animationRepeatCount = 0 annotationImage.startAnimating() addSubview(annotationImage) } required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } }
2添加点标记
Objective-C
Swift
//初始化标注类BMKPointAnnotation的实例 BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc] init]; //设置标注的经纬度坐标 annotation.coordinate = CLLocationCoordinate2DMake(39.915, 116.404); /** 向地图窗口添加标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:方法 来生成标注对应的View @param annotation 要添加的标注 */ [_mapView addAnnotation:annotation];
//初始化标注类BMKPointAnnotation的实例 let annotation = BMKPointAnnotation.init() //设置标注的经纬度坐标 annotation.coordinate = CLLocationCoordinate2DMake(39.915, 116.404) /** 向地图窗口添加标注,需要实现BMKMapViewDelegate的-mapView:viewForAnnotation:方法 来生成标注对应的View @param annotation 要添加的标注 */ mapView.addAnnotation(annotation)
3实现代理方法返回AnimatedAnnotationView
Objective-C
Swift
#pragma mark - BMKMapViewDelegate /** 根据anntation生成对应的annotationView @param mapView 地图View @param annotation 指定的标注 @return 生成的标注View */ - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation { //动画annotation NSString *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; }
//MARK:BMKMapViewDelegate /** 根据anntation生成对应的annotationView @param mapView 地图View @param annotation 指定的标注 @return 生成的标注View */ func mapView(_ mapView: BMKMapView!, viewFor annotation: BMKAnnotation!) -> BMKAnnotationView! { /** 根据指定标识查找一个可被复用的标注,用此方法来代替新创建一个标注,返回可被复用的标注 */ var annotationView: BMKAnimationAnnotationView? = mapView.dequeueReusableAnnotationView(withIdentifier: annotationViewIdentifier) as? BMKAnimationAnnotationView if annotationView == nil { /** 初始化并返回一个annotationView @param annotation 关联的annotation对象 @param reuseIdentifier 如果要重用view,传入一个字符串,否则设为nil,建议重用view @return 初始化成功则返回annotationView,否则返回nil */ annotationView = BMKAnimationAnnotationView.init(annotation: annotation, reuseIdentifier: annotationViewIdentifier) //自定义标注的图片,默认图片是大头针 return annotationView } return nil }
4运行程序
效果如下:
http://mapopen-pub-iossdk.cdn.bcebos.com/newIos/annotationView.mp4