简体   繁体   中英

Removing from superview with gestureRecognizer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM