【发布时间】:2017-08-10 13:37:22
【问题描述】:
我需要一个CALayer 与一些CAAnimation 一起在指定时间出现在屏幕上(例如fadeIn)。我希望它在屏幕上停留几秒钟,然后以 fadeOut 动画消失。
示例:如果我有一个 timeRange 为:
CMTimeRangeMake(start: 3 , end : 5) 我在 3 秒开始时需要一个 CAAnimation,在 5 秒结束时需要一个。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()
}
我终于希望能够为视频添加标题以制作像 this one 这样的歌词视频。
【问题讨论】:
标签: ios core-animation