【问题标题】:how to add gesture recognizer to UIImage and UIButton of UITableViewCell?如何将手势识别器添加到 UITableViewCell 的 UIImage 和 UIButton?
【发布时间】:2019-03-22 20:49:57
【问题描述】:

我正在尝试在按钮和动态 tableView 单元格中的图像视图上附加手势识别器,但出现错误:

*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UIButton nameOfuserTappedWithGestureRecgonizer:]: 无法识别的选择器发送到 实例 0x7ff34253f5a0'

protocol MediaTableViewCellDelegate: class {

    func didClickProfileImageOf(cell: MediaTableViewCell)
    func didClickProfileNameOf(cell: MediaTableViewCell)
}

class MediaTableViewCell: UITableViewCell {


    weak var delegate: MediaTableViewCellDelegate?

      @IBOutlet weak var mediaImageView: UIImageView! //the large image
      @IBOutlet weak var profileImageView: UIImageView!
      @IBOutlet weak var fullNameButton: UIButton!

       var tapGestureRecognizerProfileImage = UITapGestureRecognizer()
       var tapGestureRecognizerProfileName = UITapGestureRecognizer()



override func awakeFromNib() {
        super.awakeFromNib()
         initialize()
    }
     private func initialize() {

      tapGestureRecognizerProfileImage.addTarget(self.profileImageView, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))
      self.addGestureRecognizer(tapGestureRecognizerProfileImage)

      tapGestureRecognizerProfileName.addTarget(self.shareButton, action: #selector(MediaTableViewCell.nameOfuserTapped(gestureRecgonizer:)))
      self.addGestureRecognizer(tapGestureRecognizerProfileName)
   }

     func imageTapped(gestureRecgonizer: UITapGestureRecognizer) {
        delegate?.didClickProfileImageOf(cell: self)
  }

   func nameOfuserTapped(gestureRecgonizer: UITapGestureRecognizer) {
     delegate?.didClickProfileNameOf(cell: self)
 }

}//end class

【问题讨论】:

    标签: swift uitapgesturerecognizer


    【解决方案1】:

    您的问题标题具有误导性——您正在向 UIElements 添加手势识别器,而不是整个单元格。

    按钮不需要手势识别器,因为它们是 UIControl 的子类。

    private func initialize() {
        let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(profileTapped))
        profileImageView.addGestureRecognizer(imageTapGesture)
        // imageviews by default aren't interactable
        profileImageView.isUserInteractionEnabled = true 
    
        fullNameButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
    }
    
    @objc private func profileTapped() {
        delegate?.didClickProfileImageOf(cell: self)
    }
    
    @objc private func buttonTapped() {
        delegate?.didClickProfileNameOf(cell: self)
    }
    

    -- 根据评论更新--

    protocol MediaTableViewCellDelegate: class {
        func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileImage: Bool)
        func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileName: Bool)
    }
    

    【讨论】:

    • 我的 TableViewController 中有 3 种类型的原型单元格。一个单元格处理视频,另一个单元格处理图像,最后一个单元格处理文本。看起来我必须使用不同的方法名称,因为在 TableViewController Method 'didClickProfileImageOf(cell:)' with Objective-C selector 'didClickProfileImageOfCell:' conflicts with previous declaration with the same Objective-C selector 中采用协议时出现错误。有什么解决办法吗?
    • 使用 Objc 函数有时会妨碍 swift 代码。这就是为什么委托名称通常是冗长的。我会将委托名称更改为类似于 tableviews 的名称。查看更新的答案
    【解决方案2】:

    我认为你不应该为按钮添加 UIGestureRecognizer,因为它已经有触摸事件。

    只需为按钮添加此代码:

    fullNameButton.addTarget(self, action: #selector(some selector), for: .touchUpInside)
    

    至于profileImageView添加这行代码:

    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(some selector)) 
    profileImageView.isUserInteractionUnabled = true
    profileImageView.addGestureRecognizer(tapRecognizer)
    

    【讨论】:

      【解决方案3】:

      您将addTarget 的参数与addGestureRecognizer 的调用者混为一谈。

      手势识别器的addTarget 的目标是实现选择器的类(作为action 传递的闭包)。在这种情况下是self

      addGestureRecognizer 将识别器添加到视图本身。

      所以你想要:

      tapGestureRecognizerProfileImage.addTarget(self, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))
      
      self.profileImageView.addGestureRecognizer(tapGestureRecognizerProfileImage)
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-27
      • 2011-03-19
      相关资源
      最近更新 更多