Add a text overlay with AVMutableVideoComposition to a specific timerange

社会主义新天地 提交于 2019-11-27 14:13:59

I figured out what I needed to do. It wasn't anything special really. It just required a better understanding of what was all possible.

Basically all I had to do was add a basic opacity animation to the layer with text in it.

// My original code for creating the text layer
CATextLayer *titleLayer = [CATextLayer layer];
.
.
.
[titleLayer displayIfNeeded];

// the code for the opacity animation which then removes the text
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
[animation setDuration:0];
[animation setFromValue:[NSNumber numberWithFloat:1.0]];
[animation setToValue:[NSNumber numberWithFloat:0.0]];
[animation setBeginTime:1];
[animation setRemovedOnCompletion:NO];
[animation setFillMode:kCAFillModeForwards];
[titleLayer addAnimation:animation forKey:@"animateOpacity"];

We should add two opacity animations for Video overlay at particular time rang. First animation for show overlay at start duration and second animation for hide overlay at end duration. We should take care to set fill mode of animation.

Sample code of Video overlay for particular time Rang.

            let textLayer = CATextLayer()
            textLayer.opacity = 0.0
            .
            .
            textLayer.frame = CGRect(x: 0.0, y: 0.0, width: 150.0, height:100.0)
            textLayer.string = "Overlay Text"

            let startVisible = CABasicAnimation.init(keyPath:"opacity")
            startVisible.duration = 0.1    // for fade in duration
            startVisible.repeatCount = 1
            startVisible.fromValue = 0.0  
            startVisible.toValue = 1.0
            startVisible.beginTime = overlay.startTime.seconds // overlay time range start duration
            startVisible.isRemovedOnCompletion = false 
            startVisible.fillMode = kCAFillModeForwards 
            textLayer.add(startVisible, forKey: "startAnimation")

            let endVisible = CABasicAnimation.init(keyPath:"opacity")
            endVisible.duration = 0.1
            endVisible.repeatCount = 1
            endVisible.fromValue = 1.0
            endVisible.toValue = 0.0
            endVisible.beginTime = overlay.endTime.seconds
            endVisible.fillMode = kCAFillModeForwards
            endVisible.isRemovedOnCompletion = false
            textLayer.add(endVisible, forKey: "endAnimation")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!