How to fade a UIVisualEffectView and/or UIBlurEffect in and out?

后端 未结 15 1067
故里飘歌
故里飘歌 2020-12-04 08:38

I want to fade a UIVisualEffectsView with a UIBlurEffect in and out:

var blurEffectView = UIVisualEffectView()
blurEffectView = UIVisualEffectView(effect: UI         


        
15条回答
  •  生来不讨喜
    2020-12-04 09:31

    based on @cc's answer i modified his extension to blur a view

    extension UIView {
    
        func blur() {
            //   Blur out the current view
            let blurView = UIVisualEffectView(frame: self.bounds)
            self.addSubview(blurView)
            UIView.animate(withDuration:0.25) {
                blurView.effect = UIBlurEffect(style: .dark)
            }
        }
    
        func unblur() {
            for childView in subviews {
                guard let effectView = childView as? UIVisualEffectView else { continue }
                UIView.animate(withDuration: 2.5, animations: {
                    effectView.effect = nil
                }) {
                    didFinish in
                    effectView.removeFromSuperview()
                }
            }
        }
    }
    

提交回复
热议问题