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

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

    I ended up with the following solution, using separate animations for the UIVisualEffectView and the contents. I used the viewWithTag() method to get a reference to the UIView inside the UIVisualEffectView.

    let blurEffectView = UIVisualEffectView()
    // Fade in
    UIView.animateWithDuration(1) { self.blurEffectView.effect = UIBlurEffect(style: .Light) }    
    UIView.animateWithDuration(1) { self.blurEffectView.viewWithTag(1)?.alpha = 1 }
    // Fade out
    UIView.animateWithDuration(1) { self.blurEffectView.effect = nil }    
    UIView.animateWithDuration(1) { self.blurEffectView.viewWithTag(1)?.alpha = 0 }
    

    I would prefer the single animation changing the alpha, but this avoids the error and seems to work just as well.

提交回复
热议问题