2

I am trying to display an image over top of a video for the first few seconds only. I am adding an image to a CALayer and then attempting to hide it using CABasicAnimation. I have tried a few different iterations of the code below and cannot get the image to disappear. I have also tried setting the delegate property and having the parent view extend CAAnimationDelegate with an implementation of animationDidStop. However, this seems to only fire once when the view is rendered and not at the desired time within the video. What is the proper way to perform animations when constructing videos using AVMutableComposition?

let mixComposition = AVMutableComposition.init()

let videoLayer = CALayer()
videoLayer?.frame = CGRect.init(x: 0, y: 0, width: 414, height: 414)

let parentLayer = CALayer()
parentLayer?.frame = CGRect.init(x: 0, y: 0, width: 414, height: 414)

parentLayer?.addSublayer(videoLayer!)

let imageLayer = CALayer()
imageLayer.backgroundColor = UIColor.green.cgColor
imageLayer.frame = CGRect(x: 100, y: 100, width: 50, height: 50)
imageLayer.contents = UIImage(named: "music")?.cgImage

let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.fromValue = 1.0
fadeOut.toValue = 0.0
fadeOut.duration = 2.0
fadeOut.setValue("video", forKey:"fadeOut")
fadeOut.fillMode = CAMediaTimingFillMode.forwards
imageLayer.add(fadeOut, forKey: nil);

parentLayer?.addSublayer(imageLayer)

// Main video composition instruction
mainInstruction = AVMutableVideoCompositionInstruction()
mainInstruction?.timeRange = CMTimeRangeMake(start: CMTime.zero, duration: insertTime)
mainInstruction?.layerInstructions = arrayLayerInstructions
mainInstruction?.backgroundColor = UIColor.systemPink.cgColor

// Main video composition
let mainComposition = AVMutableVideoComposition()
mainComposition.instructions = [mainInstruction!]
mainComposition.frameDuration = CMTimeMake(value: 1, timescale: 30)
mainComposition.renderSize = outputSize!
Victor Altadonna
  • 193
  • 2
  • 10

0 Answers0