我了解如何基于此SO问题在圆形路径中正常制作其他CALayers的动画:iPhone-如何为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);
由于关键帧动画将使用“位置”关键路径,因此如何将其转换为2个单独的关键路径(纬度和经度),以便可以在地图上的圆圈中为标记设置动画?
任何帮助是极大的赞赏。
由于当前我无法使用“位置”键路径进行动画处理,因此我最终分别使用“纬度”和“经度”键路径对其进行了动画处理。
首先计算点并将其添加到2个单独的数组中,一个用于纬度值(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]];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句