首页 > 解决方案 > removeFromSuperview() 几个子视图

问题描述

我在ViewHelperClass 有一些功能:

class func showFrontPopOver(popOver:UIView,view:UIView) {
        let animation = AnimationType.zoom(scale: 1.5)
        popOver.animate(animations: [animation])
        popOver.layer.cornerRadius = 10
        popOver.center = view.center
        view.addSubview(popOver)
    }
class func hidePopOver(popOver:UIView, view:UIView) {
        UIView.transition(with: view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
            popOver.removeFromSuperview()
        }, completion: nil)
    }

而不是我打电话并关闭我的弹出窗口(它工作得很好): 在此处输入图像描述

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        ViewHelper.showFrontPopOver(popOver: popOver, view: self.view)
        return true
    }



func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
            ViewHelper.hidePopOver(popOver: self.popOver, view:self.view)
        }

但是我想在我的showFrontPopOver方法中添加一个 blurView:

class func showFrontPopOver(popOver:UIView,view:UIView) {
    let animation = AnimationType.zoom(scale: 1.5)
    popOver.animate(animations: [animation])
    popOver.layer.cornerRadius = 10
    popOver.center = view.center
    let blurEffect = UIBlurEffect(style: .light)
    let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurVisualEffectView.frame = view.bounds
    blurVisualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(blurVisualEffectView)
    view.addSubview(popOver)
}

事实证明,这种方法不起作用:

 func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
                ViewHelper.hidePopOver(popOver: self.popOver, view:self.view)
            }

因为他不再适用于这一层和视图。我将 2 个子视图强加给另一个可能是不正确的。但是我不知道如何实现我的表单下的模糊效果 在此处输入图像描述

我该如何解决?关闭所有东西,我的表单和模糊视图,地图除外。

标签: iosswift

解决方案


你需要给每个子视图一个标签

op1

class func showFrontPopOver(popOver:UIView,view:UIView) {
    let animation = AnimationType.zoom(scale: 1.5)
    popOver.animate(animations: [animation])
    popOver.layer.cornerRadius = 10
    popOver.center = view.center
    let blurEffect = UIBlurEffect(style: .light)
    let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
    blurVisualEffectView.frame = view.bounds
    blurVisualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    popOver.tag = 11
    blurVisualEffectView.tag = 11
    view.addSubview(blurVisualEffectView)
    view.addSubview(popOver)
}

然后

class func hidePopOver(popOver:UIView, view:UIView) {
        UIView.transition(with: view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
            view.subviews.forEach {
              if $0.tag == 11 {
                $0.removeFromSuperview()
              }
        }, completion: nil)
 }

op2

class func hidePopOver(view:UIView) {
        UIView.transition(with: view, duration: 0.25, options: [.transitionCrossDissolve], animations: {
            view.subviews.last?.removeFromSuperview()
            view.subviews.last?.removeFromSuperview()

        }, completion: nil)
 }

推荐阅读