从任何地方删除任何视图,例如 window

Remove any view from any where e.g from window

我在屏幕上有 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,我不知道其他 class 的操作如何同时影响 overlayView 并隐藏该视图。有什么想法吗?

您有两种不同的类观点。扩展您的 window 以删除您的特定视图,就像我所做的 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
            }
        }
    }
}