弹出控制器关闭后删除UIBlurView

时间:2015-03-10 14:04:52

标签: ios swift popup uiblureffect

我有一个函数,我在viewDidLoad

MainViewcontroller中调用
func showPopUp() {    

    self.popViewController = PopUpViewController(nibName: "PopUpViewController", bundle: nil)
    //self.popViewController.title = "This is a popup view"        

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    view.addSubview(blurEffectView)       

    self.popViewController.showInView(self.view, withImage: UIImage(named: "typpzDemo"), withMessage: "You just triggered a great popup window", animated: true)  
}

这会调出PopupViewController并模糊MainViewController。在我的PopupViewController内,我有一个IBAction来关闭此视图控制器

@IBAction func closePopup(sender: AnyObject) {
    self.removeAnimate()
   // Need to Close down the blur effect here!
    }
}

当我关闭弹出窗口时,主视图控制器仍然模糊。如何从closePopup IBAction

中删除此模糊视图

2 个答案:

答案 0 :(得分:1)

完成使用后,您必须删除blurEffectView。在方法closePopUp方法内部试试这个:

blurEffectView.removeFromSuperview()

当然,以下方式保存对此变量的引用,例如:

class PopUpViewController {

   var blurEffectView : UIVisualEffectView!

    @IBAction func closePopup(sender: AnyObject) {
       self.removeAnimate()
       blurEffectView.removeFromSuperview()
    }
}

在此之前你必须这样做:

func showPopUp() {    

    self.popViewController = PopUpViewController(nibName: "PopUpViewController", bundle: nil)
    //self.popViewController.title = "This is a popup view"        

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    view.addSubview(blurEffectView)       

    // In this line you pass the reference to the Blur
    self.popViewController.blurEffectView = blurEffectView


    self.popViewController.showInView(self.view, withImage: UIImage(named: "typpzDemo"), withMessage: "You just triggered a great popup window", animated: true)  
}

答案 1 :(得分:1)

您的问题似乎是如何在两个ViewController之间传递信息。虽然您可以采取多种方法,但我将使用观察者模式与基金会NotificationCenter向您展示如何做到这一点。在PopUpViewController中,在closePopUp

中添加通知
    @IBAction func closePopup(sender: AnyObject) {
        self.removeAnimate()
        NSNotificationCenter.defaultCenter().postNotification(
            NSNotification(name: "popUpDidClose", object: self)
        )
    }

返回MainViewController,在启动viewDidLoad之前,在PopUpViewController方法中添加观察服务器。

    override func viewDidLoad() {
        //...
        NSNotificationCenter.defaultCenter().addObserver(self,
                selector: Selector("clearBlurEffect:"),
                name: "popUpDidClose",
                object: nil
        )
    }

最后,将方法clearBlurEffect添加到MainViewController,这将与selector的{​​{1}}方法中的NSNotificationCenter参数相对应。编辑:您必须从主队列中发出通知。

addObserver

在此,我们会从您的主视图中删除任何 func clearBlurEffect(sender: NSNotification) { dispatch_async(dispatch_get_main_queue()) { [unowned self] in for subview in self.view.subviews as [UIView] { if let v = subview as? UIVisualEffectView { v.removeFromSuperview() } } } }