我需要一个CALayer在指定的时间出现在屏幕上与一些CAAnimation (例如。fadeIn)。我希望它停留在屏幕上几秒钟,然后消失与一个fadeOut动画。
示例:如果我有一个timeRange as:CMTimeRangeMake(start: 3 , end : 5),我需要在3秒开始时使用CAAnimation,在5秒结束时需要一个CAAnimation,.The CALayer必须只存在于timeRange期间。
我已经找到了一项显示CALayer的工作,以便它在指定的时间出现,但是我不知道如何使它在给定的时间内保持不变。
// Call this method in viewDidLoad for quick demo
func layerAnimation(){
let box = CALayer()
box.frame = CGRect(x:100, y: 100, width: 100, height: 100)
box.backgroundColor = UIColor.orange.cgColor
self.view.layer.addSublayer(box)
// Animation
CATransaction.begin()
let hide = CABasicAnimation(keyPath: "opacity")
hide.duration = 3 // The Start time for the box to appear in seconds
hide.fromValue = 0
hide.toValue = 0
hide.isRemovedOnCompletion = false
hide.fillMode = kCAFillModeBoth
CATransaction.setCompletionBlock({() -> Void in
let fadeInFadeOut = CABasicAnimation(keyPath: "opacity")
fadeInFadeOut.duration = 0
fadeInFadeOut.fromValue = 0
fadeInFadeOut.toValue = 1
fadeInFadeOut.isRemovedOnCompletion = false
fadeInFadeOut.fillMode = kCAFillModeBoth
fadeInFadeOut.autoreverses = true
box.add(fadeInFadeOut, forKey: "fadeInFadeOut")
})
box.add(hide, forKey: "hide")
CATransaction.commit()
}我最终希望能够在视频中添加标题来制作像这一个这样的抒情视频。
发布于 2017-08-10 14:37:02
如果我理解你的更正,你希望动画延迟3秒,淡入,然后淡出后,它已经显示了2秒,在完全不透明。如果应该立即淡出,只需删除完成块中的“开始时间”设置。试试看。我正在使用beginTime属性,还删除了isRemovedOnCompletion,因为您通常应该尽量避免使用该属性,因为您不想污染presentationLayer。我将实际动画持续时间更改为0.5秒。这就是我们需要多长时间才能完全消失。如果你想把它变成更长的东西,你可以。
// Call this method in viewDidAppear <-//Please for quick demo
func layerAnimation(){
let box = CALayer()
box.frame = CGRect(x:100, y: 100, width: 100, height: 100)
box.backgroundColor = UIColor.orange.cgColor
self.view.layer.addSublayer(box)
// Animation
CATransaction.begin()
let fadeIn = CABasicAnimation(keyPath: "opacity")
fadeIn.duration = 0.5
fadeIn.beginTime = CACurrentMediaTime() + 3.0 // The Start time for the box to appear in seconds
fadeIn.fromValue = 0
fadeIn.toValue = 1
//makes the animation start from the from value when there is a delay
fadeIn.fillMode = kCAFillModeBackwards
CATransaction.setCompletionBlock({() -> Void in
let fadeOut = CABasicAnimation(keyPath: "opacity")
fadeOut.duration = 0.5
fadeOut.beginTime = CACurrentMediaTime() + 2.0 // The Start time for the box to appear in seconds
fadeOut.fromValue = 1
fadeOut.toValue = 0
//makes the animation start from the from value when there is a delay
fadeOut.fillMode = kCAFillModeBackwards
box.add(fadeOut, forKey: "hide")
//update the model so that it is really hidden. best not to use isRemovedOnCompletion
box.opacity = 0
})
box.add(fadeIn, forKey: "show")
CATransaction.commit()
}https://stackoverflow.com/questions/45615355
复制相似问题