我在xCode中有一個標籤,我希望無限地淡入一個詞,然後當前時間。褪色和褪色標籤不定式用單獨的`文本'
我已經看過網上,可以找到一個淡入淡出,但對斯威夫特新來的,所以我努力適應這個我的需要。
任何幫助將是驚人的,謝謝!
我在xCode中有一個標籤,我希望無限地淡入一個詞,然後當前時間。褪色和褪色標籤不定式用單獨的`文本'
我已經看過網上,可以找到一個淡入淡出,但對斯威夫特新來的,所以我努力適應這個我的需要。
任何幫助將是驚人的,謝謝!
這裏是如何做到這一點的例子:
@IBOutlet weak var label: UILabel!
@IBAction func fade_out(sender: AnyObject) {
fade(label)
}
@IBAction func reset(sender: AnyObject) {
label.text = "label"
}
func fade(label : UILabel) {
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
label.alpha = 0.0
}, completion: nil)
UIView.animateWithDuration(2.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
label.alpha = 1.0
label.text = String(NSDate())
}, completion: nil)
}
我要做的就是,我第一次淡出標籤與UIViewAnimationOptions.CurveEaseOut
,然後用今天的日期與UIViewAnimationOptions.CurveEaseIn
再次消失進去。
Here是我爲您創建的測試項目,以便您可以看到我是如何完成的。
編輯
要做到這一點無窮大,你可以像下面這樣做並沒有一個按鈕
var timer = NSTimer()
var b = false
override func viewDidLoad() {
super.viewDidLoad()
self.timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(ViewController.fade), userInfo: nil, repeats: true)
}
func fade() {
if b{
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.label.alpha = 0.0
self.label.text = "Label"
}, completion: nil)
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.label.alpha = 1.0
self.label.text = String(NSDate())
}, completion: nil)
b = false
}
else{
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.label.alpha = 0.0
self.label.text = String(NSDate())
}, completion: nil)
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.label.alpha = 1.0
self.label.text = "Label"
}, completion: nil)
b = true
}
}
我不認爲這是什麼OP可能意味着無限... – Dravidian
淡出的動畫和時間是我所需要的,但而不是按鈕有兩種方式之間無限淡出? –
「兩者之間無限淡入淡出」是否意味着你想淡入淡出? –
一個更優雅的方式將是使用CAAnimation - >
class YourClass: UIViewController , ..{
@IBOutlet weak var fadingLabel: UILabel!
var a = true
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.swapText), userInfo: nil, repeats: true)
let anim : CABasicAnimation = CABasicAnimation(keyPath: "opacity")
anim.fromValue = 1
anim.toValue = 0
anim.duration = 0.5
anim.autoreverses = true
anim.repeatCount = Float.infinity
fadingLabel.layer.addAnimation(anim, forKey: "flashOpacity")
}
func swapText(){
if a == true{
fadingLabel.text = String(NSDate())
a = false
}else{
fadingLabel.text = String("My Text")
a = true
}
}
}
你現在要做的就是相應地操縱anim.duration
和timeInterval在NSTimer()中......
謝謝,這是一個很好的做法 –
您是否找到解決方案? – Dravidian