Ios addTarget和AddGestureRecognitor不工作,没有崩溃/错误

Ios addTarget和AddGestureRecognitor不工作,没有崩溃/错误,ios,swift3,Ios,Swift3,我有一个覆盖图,其中有一个表,我想在背景中添加一个点击手势识别器,以消除视图,并将目标添加到覆盖图中的一个按钮中,该按钮也会执行相同的操作 覆盖显示良好,如预期,但每当我点击黑色背景或取消按钮,什么也没有发生。我在这里寻找了一个答案,但什么都没有找到。我的代码如下,后面是覆盖图的屏幕截图: class importedFileView: NSObject { let blackView = UIView() let importedFileContainerView: UIView = {

我有一个覆盖图,其中有一个表,我想在背景中添加一个点击手势识别器,以消除视图,并将目标添加到覆盖图中的一个按钮中,该按钮也会执行相同的操作

覆盖显示良好,如预期,但每当我点击黑色背景或取消按钮,什么也没有发生。我在这里寻找了一个答案,但什么都没有找到。我的代码如下,后面是覆盖图的屏幕截图:

class importedFileView: NSObject {

let blackView = UIView()

let importedFileContainerView: UIView = {
    let importedFileContainerView = UIView(frame: .zero)
    importedFileContainerView.backgroundColor = .white
    importedFileContainerView.layer.cornerRadius = 10
    importedFileContainerView.layer.masksToBounds = true
    return importedFileContainerView
}()

let headerLabel: UILabel = {
    let headerLabel = UILabel()
    headerLabel.translatesAutoresizingMaskIntoConstraints = false
    headerLabel.font = UIFont(name: "HelveticaNeue-Thin" , size: 24)
    headerLabel.text = "Attach file"
    headerLabel.textColor = .darkGray
    headerLabel.adjustsFontSizeToFitWidth = true
    return headerLabel
}()

let fileTableView: UITableView = {
    let fileTableView = UITableView()
    return fileTableView
}()


let updateDetailsButton: UIButton = {
    let updateDetailsButton = UIButton()
    updateDetailsButton.translatesAutoresizingMaskIntoConstraints = false
    updateDetailsButton.backgroundColor = UIColor(r:40, g:86, b:131)
    updateDetailsButton.setTitleColor(UIColor.white, for: .normal)
    updateDetailsButton.setTitle("Attach selected files", for: .normal)
    updateDetailsButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light" , size: 18)
    updateDetailsButton.layer.cornerRadius = 2
    return updateDetailsButton
}()

let cancelButton: UIButton = {
    let cancelButton = UIButton()
    cancelButton.translatesAutoresizingMaskIntoConstraints = false
    cancelButton.backgroundColor = UIColor.white
    cancelButton.setTitleColor(UIColor(r:40, g:86, b:131), for: .normal)
    cancelButton.setTitle("Cancel", for: .normal)
    cancelButton.titleLabel!.font = UIFont(name: "HelveticaNeue-Light" , size: 18)
    cancelButton.layer.cornerRadius = 2
    return cancelButton
}()

let frameHeight: CGFloat = 450

func showFormView(){

    if let window = UIApplication.shared.keyWindow {
        blackView.backgroundColor = UIColor(white: 0, alpha: 0.5)


        window.addSubview(blackView)
        window.addSubview(importedFileContainerView)
        importedFileContainerView.addSubview(headerLabel)
        importedFileContainerView.addSubview(fileTableView)
        importedFileContainerView.addSubview(updateDetailsButton)
        importedFileContainerView.addSubview(cancelButton)

        cancelButton.addTarget(self, action: #selector(handleDismiss), for: .touchUpInside)
        blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleDismiss)))

        layoutViews()

        fileTableView.frame = CGRect(x: 30, y: window.frame.height, width: window.frame.width - 60, height: 230)
        let frameY = (window.frame.height - frameHeight) / 2

        importedFileContainerView.frame = CGRect(x: 20, y: window.frame.height, width: window.frame.width - 40, height: self.frameHeight)

        blackView.frame = window.frame
        blackView.alpha = 0

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.blackView.alpha = 1
            self.importedFileContainerView.frame = CGRect(x: 20, y: frameY, width: window.frame.width - 40, height: self.frameHeight)
        }, completion: nil
        )
    }
}


func layoutViews(){
    let views = ["v0" : headerLabel, "v1": fileTableView, "v2": updateDetailsButton, "v3": cancelButton]

    let leftSpace = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v0]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)
    let leftSpace1 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v1]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)
    let leftSpace2 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v2]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)
    let leftSpace3 = NSLayoutConstraint.constraints(withVisualFormat: "H:|-20.0-[v3]-20.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)

    let topSpacing = NSLayoutConstraint.constraints(withVisualFormat: "V:|-20.0-[v0(40)]-20.0-[v1(230)]-20.0-[v2(50)]-10.0-[v3(50)]-10.0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)

    importedFileContainerView.addConstraints(topSpacing)
    importedFileContainerView.addConstraints(leftSpace)
    importedFileContainerView.addConstraints(leftSpace1)
    importedFileContainerView.addConstraints(leftSpace2)
    importedFileContainerView.addConstraints(leftSpace3)

}

func handleDismiss() {

    UIView.animate(withDuration: 0.5,
                   delay: 0.0,
                   options: .curveEaseInOut,
                   animations: {
                    self.blackView.alpha = 0

                    if let window = UIApplication.shared.keyWindow {
                        self.importedFileContainerView.frame = CGRect(x: 20, y: window.frame.height, width: window.frame.width - 40, height: self.frameHeight)

                    }
    },
                   completion: { [weak self] finished in
                    self?.blackView.removeFromSuperview()
                    self?.importedFileContainerView.removeFromSuperview()
    })

}

override init() {
    super.init()
}
} 

self.blackView.isUserInteractionEnabled=true

是您需要添加到
blackView
UIView
)的所有内容

否则,视图不会启用任何交互,因此不会触发手势识别器的目标/动作

事件被忽略


您可能还希望在动画期间禁用它。

self.blackView.isUserInteractionEnabled=true

是您需要添加到
blackView
UIView
)的所有内容

否则,视图不会启用任何交互,因此不会触发手势识别器的目标/动作

事件被忽略


您可能还希望在动画过程中禁用它。

当覆盖图可见时,是否保留对导入文件视图实例的强引用?据我所测试的,当目标丢失时,所有操作都会被默默忽略

例如,这不起作用:

@IBAction func someAction(_ sender: Any) {
    let ifv = importedFileView()
    ifv.showFormView()

    //`ifv` is released at the end of this method, then your overlays are shown...
}
这项工作:

let ifv = importedFileView() //keep the instance as a property of your ViewController.
@IBAction func someAction(_ sender: Any) {
    ifv.showFormView()
}

以编程方式生成的
UIView
s
isUserInteractionEnabled
默认为
true
。您无需显式将其设置为
true


顺便说一下,您最好不要将非
UIView
类命名为
…View
,最好将类型名称以大写字母开头,这使您的代码更易于有经验的Swift程序员阅读。

当覆盖图可见时,您是否保留对导入文件视图实例的强引用?据我所测试的,当目标丢失时,所有操作都会被默默忽略

例如,这不起作用:

@IBAction func someAction(_ sender: Any) {
    let ifv = importedFileView()
    ifv.showFormView()

    //`ifv` is released at the end of this method, then your overlays are shown...
}
这项工作:

let ifv = importedFileView() //keep the instance as a property of your ViewController.
@IBAction func someAction(_ sender: Any) {
    ifv.showFormView()
}

以编程方式生成的
UIView
s
isUserInteractionEnabled
默认为
true
。您无需显式将其设置为
true


顺便说一下,您最好不要将非
UIView
类命名为
…View
,最好将类型名称以大写字母开头,这会使有经验的Swift程序员更容易阅读您的代码。

我尝试过,但没有效果,因此再次删除它。我会把它扔回去,看看能不能看到什么东西把它扔回去,但还是不起作用。还尝试将addTarget和GestureRecognitor移动到动画的完成块,以确保它们未被禁用或重新启用。还打印了一行完成,以确保动画报告已完成。我尝试了,但没有效果,所以再次删除它。我会把它扔回去,看看能不能看到什么东西把它扔回去,但还是不起作用。还尝试将addTarget和GestureRecognitor移动到动画的完成块,以确保它们未被禁用或重新启用。还打印出一行完成,以确保动画报告已完成,它是。宾果!非常感谢。会给班级改名的,谢谢。答对了!非常感谢。我会把班级改名的,谢谢。