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

直接上代码:
JVMoveAnimation.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #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 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| #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; } } }
- (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) { 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; } } }
#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
|
加减速的值可以根据自己的需求改;
可能有考虑不全面的地方,但是整个实现的思路方向应该是没有问题的