目前我这样做是为了倒计时。是否有更多聪明的方法来做到这一点? 是否聪明使用协议通知代表动画已完成?
您可以下载我目前正在处理的代码:
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")
})
})
})
})
})
}
答案 0 :(得分:1)
您可以像这样重构代码。
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
。