代码之家  ›  专栏  ›  技术社区  ›  Jamornh

在圆形路径中设置GMS标记的动画

  •  3
  • Jamornh  · 技术社区  · 11 年前

    基于这个SO问题,我理解如何在圆形路径中正常动画化其他CAL层: iPhone - How to make a circle path for a CAKeyframeAnimation?

    然而,GMSMarkerLayer是CALayers的一个特殊子类,它似乎不会响应“位置”关键路径(按照该链接中的说明,我看不到任何东西),而是会响应“纬度”和“经度”关键路径。

    以下是我尝试过的代码:

    CAKeyframeAnimation *circlePathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    CGMutablePathRef circularPath = CGPathCreateMutable();
    CGRect pathRect = CGRectMake(marker.position.latitude, marker.position.longitude, 0.001, 0.001);
    CGPathAddEllipseInRect(circularPath, NULL, pathRect);
    circlePathAnimation.path = circularPath;
    circlePathAnimation.duration = 1.0f;
    circlePathAnimation.repeatCount = HUGE_VALF;
    
    [marker.layer addAnimation:circlePathAnimation forKey:[NSString stringWithFormat:@"circular-%@", marker.description]];
    CGPathRelease(circularPath);
    

    由于关键帧动画将使用“位置”关键路径,我如何将其转换为两个单独的关键路径(纬度和经度),以便在地图上的圆圈中为标记设置动画?

    非常感谢您的帮助。

    1 回复  |  直到 8 年前
        1
  •  4
  •   Jamornh    11 年前

    由于目前我无法使用“位置”关键路径来设置动画,我最终分别使用“纬度”和“经度”关键路径设置动画。

    首先计算这些点,并将它们添加到两个单独的数组中,一个用于纬度值(y),另一个用于经度(x),然后使用CAKeyFrameAnimation中的values属性设置动画。创建2个CAKeyFrameAnimation对象(每个轴1个),并使用CAAnimationGroup将它们分组在一起,然后将它们设置动画以形成一个圆。

    在我的方程中,我改变了每个轴上半径的长度,这样我也可以生成一个椭圆路径。

        NSMutableArray *latitudes = [NSMutableArray arrayWithCapacity:21];
        NSMutableArray *longitudes = [NSMutableArray arrayWithCapacity:21];
        for (int i = 0; i <= 20; i++) {
            CGFloat radians = (float)i * ((2.0f * M_PI) / 20.0f);
    
            // Calculate the x,y coordinate using the angle
            CGFloat x = hDist * cosf(radians);
            CGFloat y = vDist * sinf(radians);
    
            // Calculate the real lat and lon using the
            // current lat and lon as center points.
            y = marker.position.latitude + y;
            x = marker.position.longitude + x;
    
    
            [longitudes addObject:[NSNumber numberWithFloat:x]];
            [latitudes addObject:[NSNumber numberWithFloat:y]];
        }
    
        CAKeyframeAnimation *horizontalAnimation = [CAKeyframeAnimation animationWithKeyPath:@"longitude"];
        horizontalAnimation.values = longitudes;
        horizontalAnimation.duration = duration;
    
        CAKeyframeAnimation *verticleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"latitude"];
        verticleAnimation.values = latitudes;
        verticleAnimation.duration = duration;
    
        CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
        group.animations = @[horizontalAnimation, verticleAnimation];
        group.duration = duration;
        group.repeatCount = HUGE_VALF;
        [marker.layer addAnimation:group forKey:[NSString stringWithFormat:@"circular-%@",marker.description]];