UITableViewCell中的UIImageView上的UITapGestureRecognizer
问题描述:
我在.xib中设计了一个UITableViewCell
。该单元格包含仅部分覆盖单元格的UIImageView
。我正在寻找的行为是,如果用户点击UIImageView
,UIImageView
的UITapGestureRecognizer
的选择器被触发,并且UITableViewCell
不会触发didSelectRowAtIndexPath
。如果用户点击UITableViewCell
而不是UIImageView
,那么我想要相反的行为。知道我得到的是,当用户点击UIImageView
时,UITapGestureRecognizer
的选择器和didSelectRowAtIndexPath
都被触发。当用户点击UITableViewCell
时,一切正常。我怎样才能获得理想的行为?我曾试图用cancelsTouchesInView
和其他东西搞乱。UITableViewCell中的UIImageView上的UITapGestureRecognizer
答
您不需要在单元格中使用手势识别器,而是允许为imageView设置userInteractionEnabled为true并将目标添加到imageView。请参考以下代码:
yourImageView.userInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped")
yourImageView.addGestureRecognizer(tapGesture)
如果在由手势识别器触发的方法中添加此项,会发生什么情况? 'cell.selectionStyle = UITableViewCellSelectionStyleNone;' –
您可能需要在UIImageView的顶部添加一个'UIButton'。 –
其实,我认为这个问题的答案有你的解决方案:http://stackoverflow.com/questions/28334216/uitableview-doesnt-get-touches-with-single-gesture-recognizer –