Bruno Recillas
Bruno Recillas

Reputation: 971

Blur behind UIImage not going away after image is dismissed

I've been trying to add a blur effect behind an image after I tap on it, however, it is not going away after I dismiss the image. The blur doesn't disappear.

func didTapImageView(_ sender: UITapGestureRecognizer) {

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = self.view.frame

    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = self.view.frame
    newImageView.layer.masksToBounds = true

    //newImageView.backgroundColor = UIColor.black.withAlphaComponent(1)
    newImageView.contentMode = UIViewContentMode.scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    //blurEffectView.addGestureRecognizer(tap)
    self.view.addSubview(blurEffectView)
    self.view.addSubview(newImageView)
}

func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
}

Image with blur in backgroud

How can I remove the blur after the tap? and also, how do I make it full screen? it doesn't seem to be working that either.

Upvotes: 1

Views: 366

Answers (1)

Pierce
Pierce

Reputation: 3158

It looks to me like when you call dismissFullScreenImage you are only removing the UIImageView from the superview. The blurEffectView is a separate subview entirely. You'll have to set a property for the effect view and then remove it when you call for the image to be removed. At the top of your class say something like like:

var blurEffectView:UIVisualEffectView!

Then in your didTapImageView:

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.frame

Then when you call dismissFullScreenImage, you need to say something like :

func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    sender.view?.removeFromSuperview()
    blurEffectView.removeFromSuperview()
}

I don't know what you mean "how do I make it fullscreen?" .. The visual effect already is fullscreen. At least it looks that way on your screenshot.

Upvotes: 3

Related Questions