FadeIn FadeOut Animation Cascade - 有更好的方法吗?
FadeIn FadeOut Animation Cascade - is there a better way?
目前我这样做是为了制作倒计时动画。有没有更聪明的方法来做到这一点?
使用协议通知代理动画已经完成是不是很聪明?
您可以在以下位置下载我目前正在处理的代码:
https://github.com/madeTK/TKAnimations.git
感谢任何评论。
override func viewDidLoad() {
super.viewDidLoad()
self.threeTwoOneCounter()
}
func threeTwoOneCounter() {
let fadeInTime:Double = 1.0
let fadeOutTime:Double = 0.5
let label = UILabel(frame: CGRectMake(self.view.bounds.width/2-50,100,100,100))
label.text = "3"
label.font = UIFont(name: "Arial", size: 50)
label.textColor = UIColor.blackColor()
label.textAlignment=NSTextAlignment.Center
label.alpha = 0.0
self.view.addSubview(label)
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha = 1.0
}) { (Bool) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
label.text = "2"
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha = 1.0
}, completion: { (Bool) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
label.text = "1"
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha=1.0
}, completion: { (BOOl) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
println("done.maybe calling delegate now")
})
})
})
})
})
}
您可以像这样重构代码。
func threeTwoOneCounter() {
let label = UILabel(frame: CGRectMake(self.view.bounds.width/2-50,100,100,100))
label.text = "3"
label.font = UIFont(name: "Arial", size: 50)
label.textColor = UIColor.blackColor()
label.textAlignment=NSTextAlignment.Center
label.alpha = 0.0
self.view.addSubview(label)
animateCountdownLabel(label, startValue: 3) { () -> () in
println("done.maybe calling delegate now")
}
}
func animateCountdownLabel(label : UILabel,startValue : Int,completed : () -> ())
{
if (startValue <= 0)
{
completed()
return
}
let fadeInTime:Double = 1.0
let fadeOutTime:Double = 0.5
label.text = "\(startValue)"
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha = 1.0
}) { (Bool) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
self.animateCountdownLabel(label, startValue: startValue - 1,completed: completed)
})
}
}
递归调用animateCountdownLabel()
函数,直到倒计时变为0
。
目前我这样做是为了制作倒计时动画。有没有更聪明的方法来做到这一点? 使用协议通知代理动画已经完成是不是很聪明?
您可以在以下位置下载我目前正在处理的代码:
https://github.com/madeTK/TKAnimations.git
感谢任何评论。
override func viewDidLoad() {
super.viewDidLoad()
self.threeTwoOneCounter()
}
func threeTwoOneCounter() {
let fadeInTime:Double = 1.0
let fadeOutTime:Double = 0.5
let label = UILabel(frame: CGRectMake(self.view.bounds.width/2-50,100,100,100))
label.text = "3"
label.font = UIFont(name: "Arial", size: 50)
label.textColor = UIColor.blackColor()
label.textAlignment=NSTextAlignment.Center
label.alpha = 0.0
self.view.addSubview(label)
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha = 1.0
}) { (Bool) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
label.text = "2"
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha = 1.0
}, completion: { (Bool) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
label.text = "1"
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha=1.0
}, completion: { (BOOl) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
println("done.maybe calling delegate now")
})
})
})
})
})
}
您可以像这样重构代码。
func threeTwoOneCounter() {
let label = UILabel(frame: CGRectMake(self.view.bounds.width/2-50,100,100,100))
label.text = "3"
label.font = UIFont(name: "Arial", size: 50)
label.textColor = UIColor.blackColor()
label.textAlignment=NSTextAlignment.Center
label.alpha = 0.0
self.view.addSubview(label)
animateCountdownLabel(label, startValue: 3) { () -> () in
println("done.maybe calling delegate now")
}
}
func animateCountdownLabel(label : UILabel,startValue : Int,completed : () -> ())
{
if (startValue <= 0)
{
completed()
return
}
let fadeInTime:Double = 1.0
let fadeOutTime:Double = 0.5
label.text = "\(startValue)"
UIView.animateWithDuration(fadeInTime, animations: { () -> Void in
label.alpha = 1.0
}) { (Bool) -> Void in
UIView.animateWithDuration(fadeOutTime, animations: { () -> Void in
label.alpha = 0.0
}, completion: { (Bool) -> Void in
self.animateCountdownLabel(label, startValue: startValue - 1,completed: completed)
})
}
}
递归调用animateCountdownLabel()
函数,直到倒计时变为0
。