在将它与按钮一起使用之前删除 BlurEffect

Remove BlurEffect before to use it with a button

我有一个按钮,当我点击它时,我的 background_image 变成了模糊效果。

 @IBAction func button_MainClicked2(_ sender: UIButton) {
        if button_MainCenter == button_viajes.center{
            UIView.animate(withDuration: 0.6, animations:

         {

            let blur = UIBlurEffect(style: UIBlurEffectStyle.light)
            let blurView = UIVisualEffectView(effect: blur)
            blurView.frame = self.image_Background.bounds
            self.image_Background.addSubview(blurView)    

        })
        } else {
            UIView.animate(withDuration: 0.6, animations: {
                // I want to remove blur effect as soon as i press the button again.
            })
        }
    }

如何修复它以在单击返回按钮后立即删除效果?

您可以添加 tag 来模糊视图,例如 blurView.tag = 1000,然后再将其添加为子视图,然后在动画块中

for view in button_MainCenter.subviews {
    if view.tag == 1000 {
        view.removeFromSuperview()
    }
}

编辑: 不确定它是否会被动画化。如果不是,您可以将此视图的 alpha 从 1 变为 0,然后将其删除。

编辑:

 let blur = UIBlurEffect(style: UIBlurEffectStyle.light)
 let blurView = UIVisualEffectView(effect: blur)
 blurView.tag = 1000
 blurView.frame = self.image_Background.bounds
 self.image_Background.addSubview(blurView)