弹出窗口关闭后删除模糊背景

时间:2019-03-13 17:40:13

标签: ios swift

我有两个视图控制器:# LOGGING logging.level.org.springframework.web=${LOG_LEVEL_SPRING:info} logging.level.org.hibernate=${LOG_LEVEL_SPRING:info} logging.level.web=${LOG_LEVEL_SPRING:info} logging.config=classpath:log4j2.properties MainViewController。在主控制器上,我有一个带有长按手势识别器的imageview,在长按时,我调用了弹出视图控制器,并在主控制器上使用了模糊处理:

PopupViewController

然后我在弹出式控制器中执行瘦操作并致电

var blurEffectView: UIVisualEffectView!

@IBAction func addGamePopup(_ sender: UILongPressGestureRecognizer) {

    if (sender.state == UIGestureRecognizer.State.began){

        self.view.addSubview(blurEffectView)
        UIView.animate(withDuration: 0.5, animations: {
            self.blurEffectView.alpha = 1;
        });

        let popupView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AddGamePopup")
        self.present(popupView, animated: true, completion: nil)


    }


}
override func viewDidLoad() {
    super.viewDidLoad()
    self.blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.dark))
    self.blurEffectView.frame = self.view.bounds
    self.blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.blurEffectView.alpha = 0;

    // Do any additional setup after loading the view, typically from a nib.
}

但是现在我需要从主视图中删除我的self.dismiss(animated: true, completion: nil) 。这样的事情,但是我应该在哪里做呢?我无法从弹出式控制器访问此视图,并且我不知道在关闭弹出式窗口时如何触发某些事件

blurEffectView

2 个答案:

答案 0 :(得分:1)

我与您的情况相同,我使用协议将数据传递回第一个ViewController(在您的情况下为MainViewController),告诉它隐藏模糊。

这是我的做法:

这是第一个ViewController(在您的情况下为MainViewController)。您创建并实现该协议并遵循其方法。

import UIKit

//here you can name your protocol and the function whatever you want and set the values you want to pass back, in our case a boolean
protocol esconderBlurProtocol {
    func isEsconder(value: Bool)
}

//you have to extend the controller to your protocol and conform its methods, in this case the function isEsconder, and inside it you do whatever you want.
//In our case hide the blur if the value we are receiving is true
class PalestrantesVC: UIViewController,esconderBlurProtocol {

    func isEsconder(value: Bool) {
        if(value){
            blur.isHidden = true
        }
    }
}

现在是您的第二个ViewController,它非常简单。只需创建一个协议类型的变量(在我的情况下为esconderBlurProtocol)即可。

class DetalhePalestranteVC: UIViewController {

     var delegate: esconderBlurProtocol?

并在需要时使用它(在我们的情况下,当我们关闭此ViewController时)调用其函数,将值True发送回先前的ViewController:

override func viewWillDisappear(_ animated: Bool) {
     delegate?.isEsconder(value: true)
}

最后,您必须在打开第二个ViewController时将协议实例设置为第一个ViewController,

let viewController = (self.storyboard?.instantiateViewController(withIdentifier: "DetalhePalestranteVC")) as! DetalhePalestranteVC
viewController.modalPresentationStyle = .overFullScreen
viewController.delegate = self
self.present(viewController, animated: true, completion: nil)

就这样。很抱歉,如果它的英语混乱不是我的主要语言。

答案 1 :(得分:0)

弹出窗口

viewController 调用blurEffectView?.removeFromSuperviewviewWillDisappear()内部的viewDidDisappear