[英]How to make a label fade in or out in swift
我希望在viewDidLoad()
中使標簽淡入,然后在計時器淡出3之后。 我不熟悉fadein
或fadeout
功能。
我該怎么做呢?
我的建議是利用Swift擴展來使代碼更加模塊化和易於使用。 例如,如果要在多個視圖上制作多個標簽fadein / out或一個標簽淡入/淡出,則必須在任何地方傳遞animateWithDuration方法,這可能很麻煩。 更清潔的替代方法是創建一個名為UIView.swift的文件(我們的UIView擴展名)。 將以下代碼添加到此文件:
import Foundation
extension UIView {
func fadeIn(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 0.0, completion: ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion) }
func fadeOut(duration: NSTimeInterval = 1.0, delay: NSTimeInterval = 3.0, completion: (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animateWithDuration(duration, delay: delay, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}
現在,您可以為UIView的任何子項添加淡入/淡出功能(例如,UILabel,UIImage等)。 在viewDidLoad()函數內,您可以添加:
self.statusLabel.alpha = 0
self.statusLabel.text = "Sample Text Here"
self.myLabel.fadeIn(completion: {
(finished: Bool) -> Void in
self.myLabel.fadeOut()
})
現在,您可以將此示例代碼用於圖像視圖,標簽,文本視圖,滾動視圖或UIView的任何子項。 我希望這有幫助。
即使視圖已加載,當調用viewDidLoad
時,用戶可能看不到它。 這意味着當您目擊它時,您可能會發現您的動畫似乎已經開始。 要解決此問題,您需要在viewDidAppear
啟動動畫。
至於fadeIn
和fadeOut
函數 - 它們不存在。 你需要自己寫。 值得慶幸的是,這很容易做到這一點。 像下面這樣的東西可能已經足夠了。
func fadeViewInThenOut(view : UIView, delay: NSTimeInterval) {
let animationDuration = 0.25
// Fade in the view
UIView.animateWithDuration(animationDuration, animations: { () -> Void in
view.alpha = 1
}) { (Bool) -> Void in
// After the animation completes, fade out the view after a delay
UIView.animateWithDuration(animationDuration, delay: delay, options: .CurveEaseInOut, animations: { () -> Void in
view.alpha = 0
},
completion: nil)
}
}
為Swift 3更新了答案 - 使用擴展程序
extension UIView {
func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion)
}
func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 3.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}
用法:
self.statusLabel.alpha = 0
self.statusLabel.text = "Sample Text Here"
self.myLabel.fadeIn(completion: {
(finished: Bool) -> Void in
self.myLabel.fadeOut()
})
更新Swift 3.1 - 關於@Andy代碼
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 0.25
// Fade in the view
UIView.animate(withDuration: animationDuration, animations: { () -> Void in
view.alpha = 1
}) { (Bool) -> Void in
// After the animation completes, fade out the view after a delay
UIView.animate(withDuration: animationDuration, delay: delay, options: [.curveEaseOut], animations: { () -> Void in
view.alpha = 0
}, completion: nil)
}
}
斯威夫特5
這對我很有用。 我將標簽放在“cardHeaderView”中。
@IBOutlet weak var cardHeaderView: UIView!
將其放在“viewDidAppear”中。 我的延遲為零,所以動畫會立即開始。
fadeViewInThenOut(view: cardHeaderView, delay: 0)
這是功能。
func fadeViewInThenOut(view : UIView, delay: TimeInterval) {
let animationDuration = 1.5
UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: {
view.alpha = 0
}, completion: nil)
}
SWIFT 4.2
extension UIView {
func fadeIn(duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: .curveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion)
}
func fadeOut(duration: TimeInterval = 1.0, delay: TimeInterval = 3.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: .curveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.