UITableViewCell 中 UIImageView 上的 UITapGestureRecognizer

Posted

技术标签:

【中文标题】UITableViewCell 中 UIImageView 上的 UITapGestureRecognizer【英文标题】:UITapGestureRecognizer on UIImageView in UITableViewCell 【发布时间】:2016-03-06 04:09:05 【问题描述】:

我在 .xib 中设计了一个UITableViewCell。该单元格包含一个仅部分覆盖该单元格的UIImageView。我正在寻找的行为是,如果用户点击UIImageViewUIImageViewUITapGestureRecognizer 的选择器被触发,UITableViewCell 不会触发didSelectRowAtIndexPath。如果用户点击UITableViewCell 而不是UIImageView,那么我想要相反的行为。知道我得到的是,当用户点击UIImageView 时,UITapGestureRecognizer 的选择器和didSelectRowAtIndexPath 都会被触发。当用户只点击UITableViewCell 时,一切正常。如何获得所需的行为?我试过搞乱cancelsTouchesInView 等等。

【问题讨论】:

如果在手势识别器触发的方法中添加这个会发生什么? cell.selectionStyle = UITableViewCellSelectionStyleNone; 您可能需要在UIImageView 顶部添加UIButton 其实我觉得这个问题的答案有你的解决方案:***.com/questions/28334216/… Victor - 我试过了,但没用; Tim - 我还有一个想要使用的双击手势识别器;维克多——据我所知,这个问题指的是我遇到的相反问题。答案所依赖的委托方法似乎不允许我决定是否调用 didSelectRowAtIndexPath。 我将尝试使用 UITapGestureRecognizer 来选择单元格,我认为这会起作用。 【参考方案1】:

您不需要对单元格使用手势识别器,而是允许将 imageView 的 userInteractionEnabled 设置为 true 并将目标添加到 imageView。参考这段代码:

yourImageView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped")
yourImageView.addGestureRecognizer(tapGesture)

【讨论】:

以上是关于UITableViewCell 中 UIImageView 上的 UITapGestureRecognizer的主要内容,如果未能解决你的问题,请参考以下文章

UITableviewcell 布局中的 UIImage

自定义 UITableViewCell 中不同的 UIImage 大小

如何在每个 UITableViewCell 中分配不同的 UIImage?

如何在 uitableviewcell 上显示 UIImage [关闭]

UIImage 作为 UITableViewCell 中的辅助视图未显示

仅在 Swift 3 中,圆形 UIImage 在 UITableViewCell 中消失