首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关闭弹出窗口控制器后删除UIBlurView

关闭弹出窗口控制器后删除UIBlurView
EN

Stack Overflow用户
提问于 2015-03-10 22:04:52
回答 2查看 421关注 0票数 1

我在MainViewcontrollerviewDidLoad中调用了一个函数

代码语言:javascript
复制
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

代码语言:javascript
复制
@IBAction func closePopup(sender: AnyObject) {
    self.removeAnimate()
   // Need to Close down the blur effect here!
    }
}

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

EN

回答 2

Stack Overflow用户

发布于 2015-03-10 22:28:24

使用完blurEffectView后,必须将其移除。在方法closePopUp方法中,尝试如下所示:

代码语言:javascript
复制
blurEffectView.removeFromSuperview()

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

代码语言:javascript
复制
class PopUpViewController {

   var blurEffectView : UIVisualEffectView!

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

在此之前,你必须这样做:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-03-10 22:31:39

您的问题似乎是如何在两个ViewControllers之间传递信息。尽管有许多方法可以做到这一点,但我将向您展示如何使用带有基金会的NotificationCenter的观察者模式来完成此任务。在PopUpViewController中,在closePopUp中添加通知

代码语言:javascript
复制
    @IBAction func closePopup(sender: AnyObject) {
        self.removeAnimate()
        NSNotificationCenter.defaultCenter().postNotification(
            NSNotification(name: "popUpDidClose", object: self)
        )
    }

回到你的MainViewController,在触发你的PopUpViewController之前,在你的viewDidLoad方法中添加一个观察者。

代码语言:javascript
复制
    override func viewDidLoad() {
        //...
        NSNotificationCenter.defaultCenter().addObserver(self,
                selector: Selector("clearBlurEffect:"),
                name: "popUpDidClose",
                object: nil
        )
    }

最后,将方法clearBlurEffect添加到MainViewController中,这将与NSNotificationCenteraddObserver方法中的selector参数相对应。

代码语言:javascript
复制
    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()
                }
            }
        }
    }

在这里,我们从你的主视图中删除所有的UIVisualEffectView

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28965838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档