繁体   English   中英

滑动时已选择CollectionView单元格

[英]CollectionView cell is selected when swiped on

我有一个collectionView显示单元格。 当我在某个项目上拖动/触摸/滑动手指时,如果触摸在该项目上结束,则将选中该单元格(进入详细信息屏幕)。

有什么方法可以将单元格选择( didSelectItemAt indexPath )限制为简单的点击? 即,如果在项目上拖动手指并且触摸结束,则不应选择该单元格。

这是默认行为吗?

我觉得这可能是我的自定义导航中存在一个神秘问题的原因。

谢谢

在此处输入图片说明

不要在您的cellForItem中添加关注

 let tap = UITapGestureRecognizer(target: self, action: #selector(cellTapped(tapGestureRecognizer:)))
    tap.numberOfTapsRequired = 1
cell.addGestureRecognizer(tap)

并添加以下功能

@IBAction func cellTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
//Do your required work.
}

您可以使用UITapGestureRecognizer ,因为它只会在Tap手势上响应:

@objc func tapAction(_ sender: UITapGestureRecognizer) {
    // TODO: - Action you need
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: <CellReuseId>, for: indexPath) 
    let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction(_:)))
    cell.contentView.addGestureRecognizer(tap)
    return cell
}

但是以这种方式didSelectItemAt将不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM