视频层上的CALayer位置动画

CALayer position animation on video layer

在本教程中,我找到了如何在视频中制作动画 CALayer: http://www.raywenderlich.com/30200/avfoundation-tutorial-adding-overlays-and-animations-to-videos

CABasicAnimation *animation
=[CABasicAnimation animationWithKeyPath:@"opacity"];
animation.duration=3.0;
animation.repeatCount=5;
animation.autoreverses=YES;
// animate from fully visible to invisible
animation.fromValue=[NSNumber numberWithFloat:1.0];
animation.toValue=[NSNumber numberWithFloat:0.0];
animation.beginTime = AVCoreAnimationBeginTimeAtZero;
[overlayLayer1 addAnimation:animation forKey:@"animateOpacity"];

但是当我想为CALayer的移动设置动画时,它不起作用:

animation.keyPath = @"position.x";

animation.keyPath = @"position.y";

是否可以为 CALayer 的移动设置动画?

尝试使用这个键路径:

animation.keyPath = @"transform.translation.x";

任何动画都将在视频的时间轴上进行解释,而不是实时的,因此您应该:

  1. 将动画的 beginTime 属性 设置为 AVCoreAnimationBeginTimeAtZero 而不是 0(CoreAnimation 将其替换为 CACurrentMediaTime);

  2. 将动画的 removedOnCompletion 设置为 NO,这样它们就不会被自动删除;