2016-02-13 48 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() 
     })