iOS地图相关

iOS 地图轨迹回放之播放、暂停、加速、减速(高德地图)

2019-01-11  本文已影响5人  望月Jarvis

最近项目上需要做轨迹回放的功能,所以就着手去研究了,高德地图SDK是有点的移动的接口的,但是没有对播放暂停,加速减速进行封装,需要我们自己去自定义;

IMG_0679.jpg

直接上代码:
JVMoveAnimation.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface JVMoveAnimation : NSObject

@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, copy) NSArray *tracks;  //坐标集数据源
/*
 减速滑动
 */
- (void)slowdown;
/*
 开始滑动或者暂停滑动
 */
- (void)play:(BOOL)play cmpleteCallback:(void(^)(BOOL isFinished))completeCallback;
/*
 加速滑动
 */
- (void)speedUp;
@end

NS_ASSUME_NONNULL_END

JVMoveAnimation.m 文件

#import "JVMoveAnimation.h"


@interface JVMoveAnimation ()
///暂停时所滑动的点
@property (nonatomic, assign) NSInteger suspendedCount;

///动画时长
@property (nonatomic, assign) CGFloat duration;
///速度 加减速
@property (nonatomic, assign) NSInteger speed;
///滑动的点数据集
@property (nonatomic, strong) NSMutableArray *animatedTracks;

@property (nonatomic, strong) MAAnimatedAnnotation* annotation;

@property (nonatomic, copy) void (^completeCallback)(BOOL isFinished);
@end
@implementation JVMoveAnimation


- (void)slowdown
{
    
    if (self.annotation) {
        NSArray *annos = [self.annotation allMoveAnimations];
        for (MAAnnotationMoveAnimation *anno in annos) {
            self.speed-=1;
            self.suspendedCount = [anno passedPointCount]+1;
        }
       // [HUBManager showItoastMsg:@" 速度-5 " hide:YES afterDelay:1];
    }
}


- (void)play:(BOOL)play cmpleteCallback:(void(^)(BOOL isFinished))completeCallback
{
    
    self.completeCallback = completeCallback;
    if (play) {
        [self palyAnimated];
    }
    else  //暂停
    {
        NSArray *annos = [self.annotation allMoveAnimations];
        for (MAAnnotationMoveAnimation *anno in annos) {
            //当前这个点要移动完成才暂停,所以移动的点数需要+1
            self.suspendedCount = [anno passedPointCount]+1;
        }
        
        return;
    }
}

- (void)speedUp
{
    
    if (self.annotation) {
        NSArray *annos = [self.annotation allMoveAnimations];
        for (MAAnnotationMoveAnimation *anno in annos) {
            self.speed+=1;
            self.suspendedCount = [anno passedPointCount]+1;
        }
        
       // [HUBManager showItoastMsg:@" 速度+5 " hide:YES afterDelay:1];
    }
    
}

#pragma mark ----------- 播放动画 -------------
- (void)palyAnimated
{
    if (self.annotation) {
        [self.mapView removeAnnotation:self.annotation];
        self.annotation = nil;
    }
    
    NSInteger count;
    
    if (self.suspendedCount) { ///暂停再播放
        
        for (int i = 0; i < self.suspendedCount-1; i++) {
            [self.animatedTracks removeObjectAtIndex:0]; //移除数据集中已经滑动的点
        }
        count = self.animatedTracks.count;
        self.duration = self.duration - self.duration * (self.speed * 0.05);
        self.suspendedCount = 0;
        self.speed = 0;
    }
    else
    {
        self.animatedTracks = [NSMutableArray arrayWithArray:self.tracks];
        count = self.animatedTracks.count;
        self.duration = count*2;
    }
    
    
    CLLocationCoordinate2D coords[count];
    for (int i = 0; i < count; i++) {
        Track *track = self.animatedTracks[I];
        coords[i].latitude = track.lat;
        coords[i].longitude = track.lon;
    }
    
    
    MAAnimatedAnnotation *anno = [[MAAnimatedAnnotation alloc] init];
    anno.coordinate = coords[0];
    self.annotation = anno;
    
    [self.mapView addAnnotation:self.annotation];
    WEAKSELF(weakSelf);
    [anno addMoveAnimationWithKeyCoordinates:coords count:count withDuration:self.duration withName:nil completeCallback:^(BOOL isFinished) {
        if (isFinished) {
            if (weakSelf.completeCallback) {
                weakSelf.completeCallback(isFinished);
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.mapView removeAnnotation:weakSelf.annotation];
                weakSelf.annotation = nil;
            });
        }
    } stepCallback:^(MAAnnotationMoveAnimation *currentAni) {
        if (currentAni.passedPointCount == weakSelf.suspendedCount) {
            [currentAni cancel];  //取消动画
            
            if (weakSelf.speed != 0) { ///速度改变后,重新添加滑动动画
                weakSelf.duration = currentAni.duration - currentAni.elapsedTime;  //计算剩余的时间
                [weakSelf palyAnimated];  //重新开启动画
            }
        }
        
    }];
}

@end
加减速的值可以根据自己的需求改;
可能有考虑不全面的地方,但是整个实现的思路方向应该是没有问题的
上一篇下一篇

猜你喜欢

热点阅读