Ios UIgesture点击以关闭视图

Ios UIgesture点击以关闭视图,ios,swift,uigesturerecognizer,Ios,Swift,Uigesturerecognizer,我正在尝试在自定义视图上启用UIGestureTap。我有一个视图控制器,在该视图控制器中,当我按下按钮时,会弹出一个自定义视图 var transparentBackground = UIView() @IBAction func UserViewImage(_ sender: UIButton) -> Void { self.transparentBackground = UIView(frame: UIScreen.main.bounds)

我正在尝试在自定义视图上启用UIGestureTap。我有一个视图控制器,在该视图控制器中,当我按下按钮时,会弹出一个自定义视图

var transparentBackground = UIView()
       @IBAction func UserViewImage(_ sender: UIButton) -> Void {
     self.transparentBackground = UIView(frame: UIScreen.main.bounds)
            self.transparentBackground.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
            UIApplication.shared.keyWindow!.addSubview(self.transparentBackground)
            self.opaqueView = self.setupOpaqueView()
            self.transparentBackground.addSubview(opaqueView)
            UIApplication.shared.keyWindow!.bringSubview(toFront: self.transparentBackground)
            self.view.bringSubview(toFront: transparentBackground)
       }
我希望能够点击透明背景视图并将其关闭。因此,我有一个名为removeAnimate()的Dismission函数

因此,在viewdidload中,我启用了UITap手势:

let gestureRecognizer = UITapGestureRecognizer(target: self, action:  #selector(removeAnimate))
            self.transparentBackground.addGestureRecognizer(gestureRecognizer)
            self.transparentBackground.isUserInteractionEnabled = true
我知道removeAnimate函数可以工作,因为我在transparentBackground视图中的一个按钮上使用了它,它工作得非常好。但当我点击透明背景视图时,它并没有消失,我也不确定我做错了什么

  func setupOpaqueView() -> UIView{


        let mainView = UIView(frame: CGRect(x: 16, y: 132, width: Int(UIScreen.main.bounds.width-32), height: 403))
        mainView.backgroundColor = UIColor.clear
        mainView.layer.cornerRadius = 6

        self.imageView = UIImageView(frame: CGRect(x: 29, y: 18, width: 274, height: 350))

        mainView.addSubview(OKbutton)
        mainView.addSubview(self.imageView)
        OKbutton.addTarget(self, action:  #selector(ThirdWheelViewController.handleOKButtonTapped(_:)), for: .touchUpInside)

        return mainView

    }

这是一个例子,希望它能帮助您:

首先,创建一个变量:

var customView:UIView!
这将是我们添加自定义视图的功能:

@IBAction func customAction(_ sender: AnyObject) {

    self.customView = UIView.init(frame: CGRect.init(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2, width: 100, height: 100))
    self.customView.backgroundColor = UIColor.red
    self.view.addSubview(self.customView)

    let tap = UITapGestureRecognizer.init(target: self, action: #selector(self.removeFromSuperView))
    tap.numberOfTapsRequired = 1
    self.customView.addGestureRecognizer(tap)
}
最后:

 func removeFromSuperView() {

    self.customView.alpha = 1.0
    self.customView.transform = .identity

    UIView.animate(withDuration: 0.3, animations: {

        self.customView.alpha = 0.0
        self.customView.transform = .init(scaleX: 1.5, y: 1.5)

        }) { (finished) in

            if !finished {

            } else {
              self.customView.removeFromSuperview()
            }

    }

}

尝试将alpha设置为0.05。我想我记得如果alpha为零,它会尝试变得聪明。为了清楚起见,请在设置动画之前将alpha从0.4更改为0.05@I_am_jorfSet初始值!例如:self.transparentBackground.transform=.identity,alpha值必须等于1。0@Mannopson我这样做是因为viewdidload
self.transparentBackground.transform=.identity self.transparentBackground.backgroundColor=UIColor.black.withAlphaComponent(1)
这就是你说的吗?实际上我还有一个问题。我有另一个弹出视图的视图,因此以您的示例为例,我在customView中有一个视图,但我不希望当我在customView/@ju中查看该视图时,customView被取消。您可以使用yourView作为customView的子视图。例如:self.customView.addSubview(yourView)是的,我这样做了。我想说的是,我有一个视图(transparentBackground),当我单击按钮时会弹出,在transparentBackground中,我有opaqueView
self.transparentBackground.addSubview(opaqueView)
,当我使用上述代码时,如果单击不透明视图,透明背景将消失,但我只希望在单击透明背景时透明背景将消失,而不是opaqueView@juelizabeth你用这个代码做了什么?你的opaqueView大小如何?@juelizabeth,你已经使用了这行代码。该代码将自定义视图置于前面,而不是不透明视图
 func removeFromSuperView() {

    self.customView.alpha = 1.0
    self.customView.transform = .identity

    UIView.animate(withDuration: 0.3, animations: {

        self.customView.alpha = 0.0
        self.customView.transform = .init(scaleX: 1.5, y: 1.5)

        }) { (finished) in

            if !finished {

            } else {
              self.customView.removeFromSuperview()
            }

    }

}