UIImageView Tap 手势无法识别。用户交互已启用

UIImageView Tap Gesture not recognizing. User interaction is Enabled

我有一个容器视图作为输入附件视图,我在其中添加了一个带有上传图像的简单 UIImageView。我尝试将 Tap Gesture Recognizer 添加到图像视图,但它似乎没有触发给定的事件。启用用户交互。

代码如下:

override var inputAccessoryView: UIView? {
    get {
        containerView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 50)
        containerView.backgroundColor = UIColor.white
        containerView.isMultipleTouchEnabled = true
        containerView.isUserInteractionEnabled = true

        let chooseImage = UIImageView()
        chooseImage.image = UIImage(named: "upload_image_icon")
        chooseImage.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(chooseImage)


        let gesture = UITapGestureRecognizer(target: nil, action: #selector(handleChatImage))
        chooseImage.isMultipleTouchEnabled = true
        chooseImage.isUserInteractionEnabled = true
        chooseImage.addGestureRecognizer(gesture)

        chooseImage.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        chooseImage.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        chooseImage.widthAnchor.constraint(equalToConstant: 44).isActive = true
        chooseImage.heightAnchor.constraint(equalToConstant: 44).isActive = true

        send.setTitle("Send", for: .normal)
        send.translatesAutoresizingMaskIntoConstraints = false

        containerView.addSubview(send)
        send.rightAnchor.constraint(equalTo: containerView.rightAnchor).isActive = true
        send.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        send.widthAnchor.constraint(equalToConstant: 80).isActive = true
        send.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

        containerView.addSubview(self.inputTextField)
        self.inputTextField.leftAnchor.constraint(equalTo: chooseImage.rightAnchor, constant: 8).isActive = true
        self.inputTextField.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        self.inputTextField.rightAnchor.constraint(equalTo: send.leftAnchor).isActive = true
        self.inputTextField.heightAnchor.constraint(equalTo: containerView.heightAnchor).isActive = true

        divide.backgroundColor = UIColor.lightGray
        divide.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(divide)
        divide.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
        divide.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        divide.widthAnchor.constraint(equalTo: containerView.widthAnchor).isActive = true
        divide.heightAnchor.constraint(equalToConstant: 1).isActive = true

        send.addTarget(self, action: #selector(sendMessage), for: .touchUpInside)

        return containerView
    }
}

目标是 nil(目标是 UITapGestureRecognizer 将在其中查找选择器方法的对象)

let gesture = UITapGestureRecognizer(target: nil, action: #selector(handleChatImage))

应该是self

let gesture = UITapGestureRecognizer(target: self, action: #selector(handleChatImage))