如何使整个 UIView 淡入白色并返回?
How do I make the entire UIView fade to white and back?
这个问题已经解决了。
我试图让屏幕在按下按钮时淡入白色然后再淡出。我找到了一些代码,可以将视图的 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()
})
}
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)
这个问题已经解决了。
我试图让屏幕在按下按钮时淡入白色然后再淡出。我找到了一些代码,可以将视图的 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()
})
}
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)