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

后端 未结 15 1038
故里飘歌
故里飘歌 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:36

    You can take a snapshot of a static underlying view, and fade it in and out without touching the opacity of the blur view. Assuming an ivar of blurView:

    func addBlur() {
        guard let blurEffectView = blurEffectView else { return }
    
        //snapShot = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
        let snapShot = self.view.snapshotViewAfterScreenUpdates(false)
        view.addSubview(blurEffectView)
        view.addSubview(snapShot)
    
        UIView.animateWithDuration(0.25, animations: {
            snapShot.alpha = 0.0
        }, completion: { (finished: Bool) -> Void in
            snapShot.removeFromSuperview()
        } )
    }
    
    func removeBlur() {
        guard let blurEffectView = blurEffectView else { return }
    
        let snapShot = self.view.snapshotViewAfterScreenUpdates(false)
        snapShot.alpha = 0.0
        view.addSubview(snapShot)
    
        UIView.animateWithDuration(0.25, animations: {
            snapShot.alpha = 1.0
        }, completion: { (finished: Bool) -> Void in
            blurEffectView.removeFromSuperview()
            snapShot.removeFromSuperview()
        } )
    }
    

提交回复
热议问题