addTarget and addGestureRecognizer not working, no crash/error

余生长醉 提交于 2019-12-13 07:11:13

问题


I a have an overlay with a table in and I'd like to add a Tap gesture recogniser to the background to dismiss the view and also addTarget to a button within the overlay which does the same thing.

The overlay displays fine as expected, however whenever I tap the black background or the cancel button, nothing happens. I've searched for an answer here but nothing found has worked. My code is as follows, followed by a screenshot of the overlay:

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()
}
} 


回答1:


Are you keeping a strong reference to the instance of your importedFileView while your overlays are visible? As far as I tested, all actions are silently ignored when the target is lost.

For example, this does not work:

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

    //`ifv` is released at the end of this method, then your overlays are shown...
}

This works:

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

Programmatically generated UIViews isUserInteractionEnabled is default to true. You have no need to explicitly set it to true.

By the way, you'd better not name your non-UIView class as ...View, and better make your type names start with capital letter, which makes your code more readable to experienced Swift programmers.




回答2:


self.blackView.isUserInteractionEnabled = true;

is all you need to add to the blackView (UIView).

Without that, the view doesn't have any interactions enabled and so the gesture recognizer's target/action isn't triggered.

Events are ignored.

https://developer.apple.com/reference/uikit/uiview/1622577-isuserinteractionenabled

You might also want to disable it during animations.



来源:https://stackoverflow.com/questions/40976432/addtarget-and-addgesturerecognizer-not-working-no-crash-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!