即使将isUserInteraction设置为true,UITapGesture也无法工作

时间:2020-09-02 21:38:27

标签: ios swift uikit

好,这是我的情况:

  • 具有自定义inputAccessoryView的聊天应用(当然是Overrode)
  • 需要实现对聊天功能的回复(就像whatsapp一样,用户可以在其中回复个别聊天)
  • 我正在显示新视图keyBoardReplyView,这是我在inputAccessoryView上方回复的聊天的一小段内容(通过自动布局以编程方式添加)
  • 此新视图keyBoardReplyView包含带有关闭图标的图像视图
  • 当我点击此关闭图标时,我想删除KeyboardReplyView,但不幸的是,点击手势似乎没有触发

注意: 我将isUserInteractionEnabled设置为true

我尝试过的事情: 我在一个堆栈帖子中读到,我必须将父视图用户交互设置为true,我也尝试这样做,但仍然无法正常工作

如何在inputAccessoryView顶部添加我的KeyboardReplyView的代码:

let keyboardReplyView = KeyboardSupplementaryReplyView()
    
    fileprivate func setupReplyViewForKeyboard(withTitleColor: UIColor){
        keyboardReplyView.titleLabel.textColor = withTitleColor
        keyboardReplyView.isUserInteractionEnabled = true
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleReplyMessageClose))
        keyboardReplyView.closeImageView.addGestureRecognizer(tapGesture)
        inputAccessoryView?.addSubview(keyboardReplyView)
        keyboardReplyView.anchor(top: nil, leading: inputAccessoryView?.leadingAnchor, bottom: inputAccessoryView?.topAnchor, trailing: inputAccessoryView?.trailingAnchor, size: .init(width: 0, height: 60))
    }

以下是视图的代码:

class KeyboardSupplementaryReplyView: UIView {
    let closeImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.isUserInteractionEnabled = true
        iv.tintColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        let boldConfig = UIImage.SymbolConfiguration(weight: .bold)
        let boldBell = UIImage(systemName: "xmark.circle", withConfiguration: boldConfig)
        iv.image = boldBell
        return iv
    }()
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 1
        label.font = .systemFont(ofSize: 14, weight: .bold)
        return label
    }()
    
    let messageLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 2
        label.font = .systemFont(ofSize: 12)
        return label
    }()
        
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }
    
    private func setupViews(){
        let visualEffectView = UIVisualEffectView()
        visualEffectView.effect = UIBlurEffect(style: .systemMaterialDark)
        addSubview(visualEffectView)
        visualEffectView.anchor(top: topAnchor, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor)
                
        addSubview(closeImageView)
        closeImageView.anchor(top: nil, leading: nil, bottom: nil, trailing: trailingAnchor, padding: .init(top: 0, left: 0, bottom: 0, right: 4), size: .init(width: 30, height: 30))
        closeImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        
        //38
        addSubview(titleLabel)
        titleLabel.text = "Name of user replying to"
        titleLabel.anchor(top: topAnchor, leading: leadingAnchor, bottom: nil, trailing: closeImageView.leadingAnchor, padding: .init(top: 4, left: 4, bottom: 0, right: 4))
    
        addSubview(messageLabel)
        messageLabel.textColor = UIColor.white.withAlphaComponent(0.7)
        messageLabel.text = "This is a long text that is used to test of the view can hold this in the accurate and the right format let us try it out as much as we can"
        messageLabel.anchor(top: titleLabel.bottomAnchor, leading: leadingAnchor, bottom: nil, trailing: closeImageView.leadingAnchor, padding: .init(top: 2, left: 4, bottom: 0, right: 4))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

0 个答案:

没有答案