如何使整个UIView逐渐变白并变回白色?

时间:2019-03-04 02:29:44

标签: ios swift uiview fadein fadeout

这个问题已经解决了。

我试图使屏幕淡入白色并在按下按钮时退回。我发现一些代码可以将视图的Alpha渐变为0,然后变为1,从而将整个屏幕的颜色更改为黑色。我想要的是一种使其褪色为白色而不是黑色的方法。我是编码的初学者,非常感谢您的帮助。谢谢!

这是我的淡入UIView的扩展代码:

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: UIView.AnimationOptions.curveEaseIn, animations: {
        self.alpha = 1.0
    }, completion: completion)  }

func fadeOut(_ duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
    UIView.animate(withDuration: duration, delay: delay, options: UIView.AnimationOptions.curveEaseIn, animations: {
        self.alpha = 0.0
    }, completion: completion)
}

这是我实现扩展的代码:

@IBAction func playButtonPressed(_ sender: Any) {
    self.view.fadeOut(completion: {
        (finished: Bool) -> Void in
        self.view.fadeIn()
    })
}

1 个答案:

答案 0 :(得分:0)

let whiteView = UIView(frame: view.frame)
whiteView.backgroundColor = .white
whiteView.alpha = 0
view.addSubview(whiteView)

UIViewPropertyAnimator.runningPropertyAnimator(withDuration: 1.0, delay: 0, options: .curveEaseIn, animations: {
  whiteView.alpha = 1.0
}, completion: nil)