Removing from superview with gestureRecognizer - ios

I create a blurEffectView as such:
var blurEffectView: UIVisualEffectView{
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return blurEffectView
}
And then a tapGestureRecognizer to add to the effect view.
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissBlurView))
self.blurEffectView.addGestureRecognizer(tapGestureRecognizer)
self.mapView.addSubview(self.blurEffectView)
And here is the function to dismiss it:
func dismissBlurView(){
blurEffectView.removeFromSuperview()
}
I am obviously missing something here, as dismissBlurView is not called when I tap on the blur view.

Try this
class YourViewController: UIViewController, UIGestureRecognizerDelegate{
tapGestureRecognizer.delegate = self
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}

Try to use this code because it's working for me. I,ve changed block declaration and set the frame size outside of block in viewDidLoad.
var blurEffectView: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
return blurEffectView
}()
// Set UIVisualEffectView frame in viewDidLoad
self.blurEffectView.frame = self.view.bounds
self.blurEffectView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(self.dismissBlurView(gesture:))))
self.view.addSubview(self.blurEffectView)
func dismissBlurView(gesture: UITapGestureRecognizer){
blurEffectView.removeFromSuperview()
}

Related

Blur effect on view background

I have this extension, and I was hoping for a blur background, where I can see blur through the view:
import Foundation
import UIKit
extension UIView
{
func addBlurEffect()
{
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
} else {
self.backgroundColor = UIColor.white
}
}
}
Then I use it like this:
let v = UIView(frame: self.view.frame)
v.addBlurEffect()
self.view.addSubview(v)
But I can not see anything under the view?
Add this framework to your project.
Add transparent view over your background and setup blur (view Readme on github)
Preview

Add uiview to be the background view

I have the following blur view, that I would like to be the background view. The problem is that it becomes the first view, hiding underneath labels/cells etc. I tought that self.sendSubview(toBack: blurEffectView) would do just that - do you know why? :
Update: also tried: self.insertSubview(blurEffectView, belowSubview: self)
import Foundation
import UIKit
extension UIView
{
func addBlurEffect()
{
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(blurEffectView)
self.sendSubview(toBack: blurEffectView)
} else {
self.backgroundColor = UIColor.white
}
}
}
It turns out that it is only when using UIViewController or UItableView.
import Foundation
import UIKit
extension UIView
{
func addBlurEffect()
{
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
if (self is UICollectionView){
(self as! UICollectionView).backgroundView = blurEffectView
}else{
self.addSubview(blurEffectView)
self.insertSubview(blurEffectView, belowSubview: self)
}
} else {
self.backgroundColor = UIColor.white
}
}
}

Blur effect not appearing in UITableViewCell

I'm trying to add a blur effect to my view. I created the blur effect like so
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
return blurEffectView
}()
Then I add it to the subview like so, and set up the constraints with it as well
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
let view = tableViewCell
addSubview(view)
view.addSubview(blurLabel)
blurLabel.leftAnchor.constraint(equalTo: self.leftAnchor,constant:0).isActive = true
//blurLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 178).isActive = true
blurLabel.widthAnchor.constraint(equalToConstant: 375).isActive = true
blurLabel.heightAnchor.constraint(equalToConstant: 180).isActive = true
But when I run the application it doesn't appear at all
Edit: As suggested by #HAS & #Fogmeister, you can also use translatesAutoResizingMaskIntoConstraints = false as it is better solution to use Auto Layout without specifying explicit frames.
You will have to assign frame to UIVisualEffectView like this:
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = CGRect(x: 0, y: 0, width: 60, height: 40) //give desirable frame
return blurEffectView
}()
If you would like, instead of giving explicit frames you can also provide your blurView's to any of your view or, subviews.
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = myMainview.bounds //Blur effect's frame
return blurEffectView
}()
override func viewDidLoad() {
super.viewDidLoad()
// Use it somewhere using
self.view.insertSubview(blurEffectView, belowSubview: myAnotherView)
// You may also use any of the subviews too, instead of the self.view
/// self.myView.insertSubview(blurEffectView, belowSubview: myAnotherView)
}
To fix this you just have to stop it automatically creating constraints. Like this...
let blurLabel: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
// this line will stop constraints being added for a zero frame
blurEffectView.translatesAutoResizingMaskIntoConstraints = false
return blurEffectView
}()

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()
}
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.
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.

How to remove blur subview from super view

I need to blur screen when alert is shown, so I googled the function, which blurs the screen
it looks like
var effectView: UIVisualEffectView!
func addBlur() {
var effect = UIBlurEffect(style: UIBlurEffectStyle.Light)
effectView = UIVisualEffectView(effect: effect)
effectView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
view.addSubview(effectView)
}
I want to remove the blur after user dismissed the alert and I come up with the such function
func removeBlur() {
effectView.view.removeFromSuperview()
}
but it doesn't work, says UIVisualEffectView does not have a member named "view"
How to fix it?
func removeBlur() {
effectView.removeFromSuperview()
}
You can also try this way:
func blureffect() {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
}

Resources