【发布时间】:2020-01-02 15:11:38
【问题描述】:
我想为UILabel 添加淡入动画。我通过在 UIView 中为淡入和淡出动画添加扩展来进行尝试。淡出动画效果很好,但我遇到了淡入动画的问题。
这是我的代码,
extension UIView{
func fadeIn() {
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: nil)
}
func fadeOut() {
UIView.animate(withDuration: 8.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
self.alpha = 0.0
}, completion: nil)
}
}
class myScene: UIViewController {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
override func viewDidLoad(){
super.viewDidLoad()
self.label.center = CGPoint(x: self.view.frame.width / 2, y: self.view.frame.height / 2 )
self.label.font = UIFont.systemFont(ofSize: 100)
self.label.textAlignment = .center
self.label.text = "Hello"
//self.view.addSubview(label) (tried adding this after and before the fadein() statement)
label.fadeIn()
}
}
我相信我犯了一个愚蠢的错误,但不知何故无法弄清楚是什么。请帮我弄清楚。
【问题讨论】: