2016-02-13 84 views

回答

6

即使視圖已經加載,當調用viewDidLoad時,用戶可能看不到該視圖。這意味着你可能會發現你的動畫看起來已經開始了。爲了解決這個問題,你需要改用viewDidAppear開始動畫。

至於fadeInfadeOut功能 - 它們不存在。你需要自己寫。謝天謝地,這很容易做到。像下面的東西可能會足夠好。

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) 
    } 
} 
14

我的建議是利用Swift擴展使代碼更加模塊化,易於使用。例如,如果您想讓多個標籤淡入淡出或一個標籤在多個視圖中淡入/淡出,則必須在各處傳遞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的任何孩子爲好。我希望這有幫助。

+0

這應該是正確答案。非常乾淨和抽象。謝謝。 – user3286381

+0

這是一個很棒的解決這個問題的方法。 –

+0

這看起來不錯!謝謝。僅供參考「完成」參數需要一個@escaping關閉。 – adamcee

4

更新雨燕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) 
     } 
    } 
+0

編輯按鈕出於某種原因。複製粘貼答案只是稍微修改語法並不好。 – Fogmeister

4

回答更新的斯威夫特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() 
     })