如何实现UITableView的滑动以删除UICollectionView [英] How to implement UITableView`s swipe to delete for UICollectionView

查看:534
本文介绍了如何实现UITableView的滑动以删除UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想问一下如何在UICollectionView中实现与UITableView的滑动相同的删除行为.我正在尝试查找教程,但找不到任何内容.

I just like to ask how can I implement the same behavior of UITableView`s swipe to delete in UICollectionView. I am trying to find a tutorial but I cannot find any.

此外,我正在使用PSTCollectionView包装器来支持iOS5.

Also, I am using PSTCollectionView wrapper to support iOS 5.

谢谢!

滑动识别器已经很好. 我现在需要的是与取消删除模式时的UITableView相同的功能,例如当用户点击表视图中的单元格或空白时(即,当用户点击删除"按钮之外时). UITapGestureRecognizer不起作用,因为它仅在释放触摸时检测到轻敲. UITableView在手势开始时(而不是在释放时)检测到触摸,并立即取消删除"模式.

The swipe recognizer is already good. What I need now is the same functionality as UITableView's when cancelling the Delete mode, e.g. when user taps on a cell or on a blank space in the table view (that is, when user taps outside of the Delete button). UITapGestureRecognizer won't work, since it only detects taps on release of a touch. UITableView detects a touch on begin of the gesture (and not on release), and immediately cancels the Delete mode.

推荐答案

有一个更简单的解决方案,可以避免使用手势识别器.该解决方案基于UIScrollViewUIStackView组合.

There is a simpler solution to your problem that avoids using gesture recognizers. The solution is based on UIScrollView in combination with UIStackView.

  1. 首先,您需要创建2个容器视图-一个用于单元格的可见部分,一个用于隐藏部分.您将这些视图添加到UIStackView. stackView将充当内容视图.确保视图与stackView.distribution = .fillEqually的宽度相等.

  1. First, you need to create 2 container views - one for the visible part of the cell and one for the hidden part. You’ll add these views to a UIStackView. The stackView will act as a content view. Make sure that the views have equal widths with stackView.distribution = .fillEqually.

您将把stackView嵌入到已启用分页的UIScrollView中. scrollView应该限制在单元格的边缘.然后,将stackView的宽度设置为scrollView宽度的2倍,以便每个容器视图都具有单元格的宽度.

You’ll embed the stackView inside a UIScrollView that has paging enabled. The scrollView should be constrained to the edges of the cell. Then you’ll set the stackView’s width to be 2 times the scrollView’s width so each of the container views will have the width of the cell.

通过此简单的实现,您已经创建了具有可见和隐藏视图的基本单元.使用可见视图将内容添加到单元格,在隐藏视图中可以添加删除按钮.这样,您可以实现以下目标:

With this simple implementation, you have created the base cell with a visible and hidden view. Use the visible view to add content to the cell and in the hidden view you can add a delete button. This way you can achieve this:

我已经在GitHub上建立了示例项目.您也可以在此处详细了解此解决方案.
此解决方案的最大优点是操作简便,无需处理约束和手势识别器.

I've set up an example project on GitHub. You can also read more about this solution here.
The biggest advantage of this solution is the simplicity and that you don't have to deal with constraints and gesture recognizers.

这篇关于如何实现UITableView的滑动以删除UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆