iOS 高德地图点平滑移动-暂停-再移动

2018-04-24  本文已影响0人  追梦小怪兽

废话不多说,直接贴代码:
暂停后继续移动的重要代码

- (void)button2 {    // 点击stop 按钮
    //结束未完成动画
    for (MAAnnotationMoveAnimation *animation in [self.annotation allMoveAnimations]) {
        [animation cancel];
        self.animationDurationTime += animation.elapsedTime; // 这里为啥要用 += ,是因为他每次开启动画都是一个新的 动画。
        self.passedPointCount += (int)animation.passedPointCount; // 将动画的时间和动画经过的点记录一下。
    }
}

下面是继续滑动

- (void)button3{
    // 获取走过的点的个数,再进行动画
    int passCount = self.passedPointCount;
    CLLocationCoordinate2D commonPolyLineCoords[5- passCount]; // 这里的5 是你之前线路的点的个数
    for (int i = 0; i < 5 - passCount; i++) {
        commonPolyLineCoords[i].latitude = coords1[i + passCount].latitude;
        commonPolyLineCoords[i].longitude = coords1[i + passCount].longitude;
    }
    // 这样新的移动的点集合就拿到了。
    self.annotation.coordinate = CLLocationCoordinate2DMake(coords1[passCount].latitude, coords1[passCount].longitude);

    [self.annotation addMoveAnimationWithKeyCoordinates:commonPolyLineCoords count:5 - passCount withDuration:10 - self.animationDurationTime withName:nil completeCallback:^(BOOL isFinished) {
       // 注意这里面是需要使用weak self  的 ---完成回调
       
    } stepCallback:^(MAAnnotationMoveAnimation *currentAni) {
       // 每一个点的回调
      
    }];
}

下面是整个控制器的代码,有点多,有不懂的,下面mark 一下。希望能帮助大家。

#import "MovingAnnotationViewController.h"
#import <MAMapKit/MAMapKit.h>

@interface MovingAnnotationViewController () <MAMapViewDelegate>
{
    CLLocationCoordinate2D coords1[5];
    CLLocationCoordinate2D coords2[6];
    CLLocationCoordinate2D coords3[10];//五角星
}

@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) MAAnimatedAnnotation* annotation;
@property (nonatomic,assign) int passedPointCount;  // 移动过的点count
@property (nonatomic,assign) CGFloat animationDurationTime;  // 动画开始了的时间

@end

@implementation MovingAnnotationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initCoordinates];
    
    self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
    
    //add overlay
    MAPolyline *polyline1 = [MAPolyline polylineWithCoordinates:coords1 count:sizeof(coords1) / sizeof(coords1[0])];
    MAPolyline *polyline2 = [MAPolyline polylineWithCoordinates:coords2 count:sizeof(coords2) / sizeof(coords2[0])];
    MAPolyline *polyline3 = [MAPolyline polylineWithCoordinates:coords3 count:sizeof(coords3) / sizeof(coords3[0])];
    [self.mapView addOverlays:@[polyline1, polyline2, polyline3]];
    
    MAAnimatedAnnotation *anno = [[MAAnimatedAnnotation alloc] init];
    anno.coordinate = coords1[0];
    self.annotation = anno;
    
    [self.mapView addAnnotation:self.annotation];
    
    [self initButton];
}

- (void)initCoordinates {
    ///1
    coords1[0].latitude = 39.852136;
    coords1[0].longitude = 116.30095;
    
    coords1[1].latitude = 39.852136;
    coords1[1].longitude = 116.40095;
    
    coords1[2].latitude = 39.932136;
    coords1[2].longitude = 116.40095;
    
    coords1[3].latitude = 39.932136;
    coords1[3].longitude = 116.40095;
    
    coords1[4].latitude = 39.982136;
    coords1[4].longitude = 116.48095;
    
    ///2
    coords2[0].latitude = 39.982136;
    coords2[0].longitude = 116.48095;
    
    coords2[1].latitude = 39.832136;
    coords2[1].longitude = 116.42095;
    
    coords2[2].latitude = 39.902136;
    coords2[2].longitude = 116.42095;
    
    coords2[3].latitude = 39.902136;
    coords2[3].longitude = 116.44095;
    
    coords2[4].latitude = 39.932136;
    coords2[4].longitude = 116.44095;
    
    
    ///3
    [self generateStarPoints:coords3 pointsCount:10 atCenter:CLLocationCoordinate2DMake(39.800892, 116.293413)];//生成多角星的坐标
    
    coords2[5] = coords3[0];
}

/*!
 @brief  生成多角星坐标
 @param coordinates 输出的多角星坐标数组指针。内存需在外申请,方法内不释放,多角星坐标结果输出。
 @param pointsCount 输出的多角星坐标数组元素个数。
 @param starCenter  多角星的中心点位置。
 */
- (void)generateStarPoints:(CLLocationCoordinate2D *)coordinates pointsCount:(NSUInteger)pointsCount atCenter:(CLLocationCoordinate2D)starCenter
{
#define STAR_RADIUS 0.05
#define PI 3.1415926
    NSUInteger starRaysCount = pointsCount / 2;
    for (int i =0; i<starRaysCount; I++)
    {
        float angle = 2.f*i/starRaysCount*PI;
        int index = 2 * I;
        coordinates[index].latitude = STAR_RADIUS* sin(angle) + starCenter.latitude;
        coordinates[index].longitude = STAR_RADIUS* cos(angle) + starCenter.longitude;
        
        index++;
        angle = angle + (float)1.f/starRaysCount*PI;
        coordinates[index].latitude = STAR_RADIUS/2.f* sin(angle) + starCenter.latitude;
        coordinates[index].longitude = STAR_RADIUS/2.f* cos(angle) + starCenter.longitude;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initButton
{
    UIButton *button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.frame = CGRectMake(10, 80, 70,25);
    button1.backgroundColor = [UIColor redColor];
    [button1 setTitle:@"Go" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(button1) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.frame = CGRectMake(10, 120,70,25);
    button2.backgroundColor = [UIColor redColor];
    [button2 setTitle:@"Stop" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(button2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    button3.frame = CGRectMake(10, 160,70,25);
    button3.backgroundColor = [UIColor redColor];
    [button3 setTitle:@"Continue" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(button3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
}

- (void)button1 {
    self.annotation.coordinate = coords1[0];
    
//    MAAnimatedAnnotation *anno = self.annotation;
//    [anno addMoveAnimationWithKeyCoordinates:coords1 count:sizeof(coords1) / sizeof(coords1[0]) withDuration:5 withName:nil completeCallback:^(BOOL isFinished) {
//    }];
//
//    [anno addMoveAnimationWithKeyCoordinates:coords2 count:sizeof(coords2) / sizeof(coords2[0]) withDuration:5 withName:nil completeCallback:^(BOOL isFinished) {
//    }];
//
//
//    [anno addMoveAnimationWithKeyCoordinates:coords3 count:sizeof(coords3) / sizeof(coords3[0]) withDuration:5 withName:nil completeCallback:^(BOOL isFinished) {
//    }];
    
    self.annotation.title = @"涛胖子带你飞";
    [self.mapView setSelectedAnnotations:@[self.annotation]];
    // 点在 coords1 的这条线上面平滑移动
    [self.annotation addMoveAnimationWithKeyCoordinates:coords1 count:sizeof(coords1) / sizeof(coords1[0]) withDuration:10 withName:nil completeCallback:^(BOOL isFinished) { // 完成回调
        
    } stepCallback:^(MAAnnotationMoveAnimation *currentAni) { // 当前点的回调
        
    }];
    
}

- (void)button2 {    
    //结束未完成动画
    for (MAAnnotationMoveAnimation *animation in [self.annotation allMoveAnimations]) {
        [animation cancel];
        self.animationDurationTime += animation.elapsedTime; // 这里为啥要用 += ,是因为他每次开启动画都是一个新的 动画。
        self.passedPointCount += (int)animation.passedPointCount;
    }
}

- (void)button3{
    // 获取走过的点的个数,再进行动画
    int passCount = self.passedPointCount;
    CLLocationCoordinate2D commonPolyLineCoords[5- passCount]; // 这里的5 是你之前线路的点的个数
    for (int i = 0; i < 5 - passCount; i++) {
        commonPolyLineCoords[i].latitude = coords1[i + passCount].latitude;
        commonPolyLineCoords[i].longitude = coords1[i + passCount].longitude;
    }
    // 这样新的移动的点集合就拿到了。
    self.annotation.coordinate = CLLocationCoordinate2DMake(coords1[passCount].latitude, coords1[passCount].longitude);
   // 这里的时间就要改成这个动画的总时间 - 用了的时间
    [self.annotation addMoveAnimationWithKeyCoordinates:commonPolyLineCoords count:5 - passCount withDuration:10 - self.animationDurationTime withName:nil completeCallback:^(BOOL isFinished) {
       
       
    } stepCallback:^(MAAnnotationMoveAnimation *currentAni) {
       
      
    }];
}
#pragma mark - mapview delegate
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        polylineRenderer.lineWidth    = 8.f;
        polylineRenderer.strokeImage = [UIImage imageNamed:@"arrowTexture"];
        return polylineRenderer;
        
    }
    
    return nil;
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        NSString *pointReuseIndetifier = @"myReuseIndetifier";
        MAAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
                                                             reuseIdentifier:pointReuseIndetifier];
            
            UIImage *imge  =  [UIImage imageNamed:@"jj_bt_praise_s"];
            annotationView.image =  imge;
        }
        
        annotationView.canShowCallout               = YES;
        annotationView.draggable                    = NO;
        annotationView.rightCalloutAccessoryView    = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [annotationView setSelected:YES animated:NO];
        
        return annotationView;
    }
    
    return nil;
}

运行:


IMG_0355.PNG

总结:因为高德大佬没有给我们动画暂停的API,那我们就先将动画取消,再找到取消动画之后的点,还有就是取消动画时用了的时间。然后再拿到剩下的点的集合。。最后进行动画。OK。。完美。
这个代码可能会有小伙伴在运行的时候发现,我擦,说好的平滑移动呢,为啥没有平滑移动,那么这里要说明一下,代码中的点距离都很大,实际开发中我们拿到的点肯定是很密集的,所以不用担心这个问题。

希望能帮助到大家。。

---来自涛胖子的工作笔记
点平滑移动的API

上一篇 下一篇

猜你喜欢

热点阅读