2

I have a collection view cell that has a UIVisualEffectView as a subview. This view initially is at alpha = 0.0 and when the user tap in the cell it becomes alpha = 1.0.

let blurEffect = UIBlurEffect(style: .light)
blurEffectView.effect = blurEffect
blurEffectView.frame = cardView.bounds
blurEffectView.alpha = 0.0
cardView.addSubview(blurEffectView)
blurEffectView.snp.makeConstraints { (make) in
    make.edges.equalToSuperview()
}

I am doing this animating it like the following:

DispatchQueue.main.async {
    UIView.animate(withDuration: 0.5) {
           cell.blurEffectView.alpha = 1.0
     }
}

The issue is that the blur view becomes visible but without any animation. Any idea what I'm doing wrong?

EDIT: Thanks to @TylerTheCompiler answer I have now changed the animation of the alpha for the Blur Effect itself. The issue is that the animation keeps not happening.

I use cell.blurEffectView.fadeInEffect() in func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

func fadeInEffect(_ style:UIBlurEffect.Style = .light, withDuration duration: TimeInterval = 1.0) {
        if #available(iOS 10.0, *) {
            let animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn) {
                self.effect = UIBlurEffect(style: style)
            }
            animator.startAnimation()
        }else {
            // Fallback on earlier versions
            UIView.animate(withDuration: duration) {
                self.effect = UIBlurEffect(style: style)
            }
        }
    }

Thank you!

Joan Cardona
  • 3,463
  • 2
  • 25
  • 43
  • Possible duplicate of: [How to fade a UIVisualEffectView and/or UIBlurEffect in and out?](https://stackoverflow.com/questions/29307827/how-to-fade-a-uivisualeffectview-and-or-uiblureffect-in-and-out) – TylerP Mar 30 '20 at 19:31
  • @TylerTheCompiler I thought this would be the answer, as it specifically says to avoid the alpha effect. I have changed to what things to be their solution and there's still no animation. – Joan Cardona Mar 30 '20 at 19:37

1 Answers1

0

To animate blur effect you should set effect to nil:

blurEffectView.effect = nil

and then perform the animation:

UIView.animate(withDuration: 0.5) {
    blurEffectView.effect = UIBlurEffect(style: .light)
}
hell0friend
  • 561
  • 1
  • 3
  • 4