从窗口移除任何视图

时间:2018-03-14 07:54:34

标签: swift uiview uitapgesturerecognizer ibaction

我在屏幕上有2个视图,一个是底部的overlayView,另一个是overlayView顶部的introView。当我在屏幕上点击(tapToContinueAction)时,他们应该隐藏或删除自己。

extension UIView {
 ....
 func hideView(view: UIView, hidden: Bool) {
    UIView.transition(with: view, duration: 0.5, options: .transitionCrossDissolve, animations: {
        view.isHidden = hidden
    })
}
} 
class IntroScreen
 @IBAction func tapToContinueAction(_ sender: UITapGestureRecognizer) {
    self.hideView(view: self, hidden: true)
}

-

class OverlayView : UiView {
  ...
 }

在目前的情况下,我只能隐藏introScreen,我不知道其他类的动作如何同时影响overlayView并隐藏该视图。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的观点有两个不同的类。扩展您的窗口以删除您的特定视图,就像我已经removeOverlayremoveIntroView这两个计算属性将在window的子视图列表中搜索并检查每个视图一样输入并删除它们。这就是你如何从任何地方删除任何视图。

class OverLayView: UIView {}
class IntroView: UIView {
    @IBAction func didTapYourCustomButton(sender: UIButton) {
        let window = (UIApplication.shared.delegate as! AppDelegate).window!
        window.removeOverlay
        window.removeIntroView
    }
}
extension UIWindow {
    var removeOverlay: Void {
        for subview in self.subviews {
            if subview is OverLayView {
                subview.removeFromSuperview()// here you are removing the view.
                subview.hidden = true// you can hide the view using this
            }
        }
    }
    var removeIntroView: Void {
        for subview in self.subviews {
            if subview is IntroView {
                subview.removeFromSuperview()// here you are removing the view.
                subview.hidden = true// you can hide the view using this
            }
        }
    }
}