ios - 通过 AVExportSession 导出带有隐式动画的 CALayer

标签 ios animation calayer avmutablecomposition

我正在尝试通过 AVExportSession 为我的自定义 CALayer 导出带有自定义属性的动画,请在下面找到设置:

class CustomAnimatable: CALayer
{
    @NSManaged var brightness: CGFloat

    override init(layer: Any) {
        super.init(layer: layer);

        if let l = layer as? CustomAnimatable {
            self.brightness = l.brightness;
        }
    }

    override func action(forKey event: String) -> CAAction?
    {
        if event == "brightness" {
            let animation = CABasicAnimation(keyPath: event);
            animation.fromValue = presentation()?.brightness ?? self.brightness;
            return animation;
        }

        return super.action(forKey: event);
    }

    override class func needsDisplay(forKey key: String) -> Bool
    {
        if key == "brightness" {
            return true;
        }

        return super.needsDisplay(forKey: key);
    }

    override func display()
    {
        print("\(self) \(presentation()?.brightness) \(self.brightness)")
    }
}

这是导出 session 的预设置:

func render()
{
     ......

    let parentLayer = CALayer();
    let videoLayer = CALayer();
    let animationLayer = CustomAnimatable()

    parentLayer.frame = frame;
    videoLayer.frame = frame;
    animationLayer.frame = frame;

    parentLayer.addSublayer(videoLayer);
    parentLayer.addSublayer(animationLayer);

    CATransaction.begin()
    CATransaction.setAnimationDuration(2.2);
    CATransaction.setDisableActions(true);
    CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear))

    let anim = CABasicAnimation(keyPath: "brightness");

    anim.fromValue = 1.0;
    anim.fillMode = kCAFillModeBoth;
    anim.beginTime = AVCoreAnimationBeginTimeAtZero;
    anim.repeatCount = 1;
    anim.toValue = 0.0;
    anim.isRemovedOnCompletion = false;

    animationLayer.add(anim, forKey: "anim")

    CATransaction.commit()


    let videoComposition = AVMutableVideoComposition();

    videoComposition.renderSize = CGSize(width: width, height: height);
    videoComposition.instructions = [mainInstruction];
    videoComposition.frameDuration = CMTimeMake(1, 30);
    videoComposition.animationTool = AVVideoCompositionCoreAnimationTool(postProcessingAsVideoLayer: videoLayer, in: parentLayer);

     ....
}

问题是生成的视频中的 brightness 值在没有动画的情况下从 1 变为 0。如果我尝试为 CALayer 的原生属性设置动画,例如opacity - 导出的动画视频完全没问题,不透明度从 1 平滑地淡化到 0。

我对自定义属性做错了吗?

我考虑过的事情:

  • 将显式动画包装到 CATransaction 中以禁用隐式 Action
  • 根据“Editing Media with AV Foundation” session (核心动画陷阱部分)将动画开始时间设置为 AVCoreAnimationBeginTimeAtZero 并将 isRemovedOnCompletion 设置为 false

我对 native CALayer 属性的动画效果很好这一事实感到有点困惑,因此导出 session 设置似乎是正确的。

除此之外,如果我将自定义层添加到 View 并为 brightness 属性设置动画 - 它也可以很好地设置动画。所以这个问题似乎特定于使用 AVExportSession 渲染自定义属性动画。

最佳答案

我不完全确定为什么您在 parentLayer? 中添加了类 videoLayeranimationLayer

parentLayer.addSublayer(videoLayer);
parentLayer.addSublayer(animationLayer);

使用下面的代码希望对你有所帮助!

parentLayer.addSublayer(videoLayer);
videoLayer.addSublayer(animationLayer);

关于ios - 通过 AVExportSession 导出带有隐式动画的 CALayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47181365/

相关文章:

swift - 设备方向更改后使 collectionViewCell 中的 CAShapeLayer 无效并重绘

ios - Tumblr 的 UIDocumentInteractionController

ios - 最小正浮点值是多少?

javascript - 点击时 react 模态动画

iphone - 根据状态修改 UIButtons 层

ios - CATransform3DMakeRotation和阴影

ios - NSData 到 NSArray;数组为空

ios - ld : warning: directory not found for option -- deleting derived data and cleaning project don't work

html - 使用 css 动画在 10 秒后显示一个 div

ios - 未调用 UIView 动画完成 block