如何解决点击按钮后如何立即激活拖放模式
基于https://github.com/pgpt10/DragAndDrop-CollectionView
通过使用
self.collectionView.dragInteractionEnabled = true
self.collectionView.dragDelegate = self
self.collectionView.dropDelegate = self
一旦您长按一个集合视图单元格中的任意位置,就会触发以下功能
extension DragDropViewController : UICollectionViewDragDelegate
{
func collectionView(_ collectionView: UICollectionView,itemsForBeginning session: UIDragSession,at indexPath: IndexPath) -> [UIDragItem]
{
let item = collectionView == collectionView1 ? self.items1[indexPath.row] : self.items2[indexPath.row]
let itemProvider = NSItemProvider(object: item as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
但是,我有不同的要求。
我有一个看起来如下的集合
我希望当用户点击(而不是长按)右侧的3条水平线图标时,他可以立即执行拖动和重新排序。轻按其他区域或长按单元格将不会具有拖动和重新排序的效果。
我可以知道如何实现吗?
App Store中一些能够实现此功能的著名应用
我注意到Google Keep在App Store中能够实现此功能。只需点击其待办事项列表中最左侧的图标,我们就可以立即对待办事项列表项进行重新排序。
想知道他们是怎么做到的?
方法1:在Cell的重新排序图标上安装长按手势
我尝试过
-
在单元格的重新排序图标上安装长
UILongPressGestureRecognizer
。 -
使用
gesture.minimumPressDuration = 0
模仿点击行为。TabInfoSettingsItemCell类:UICollectionViewCell {
override func awakeFromNib() { super.awakeFromNib() ... let gesture = UILongPressGestureRecognizer(target:self,action: #selector(longPressGesture)) gesture.minimumPressDuration = 0 reorderImageView.addGestureRecognizer(gesture) }
但是结果并不令人鼓舞。 “移动”操作根本不起作用!
完整的代码可以在这里找到:https://github.com/yccheok/ios-tutorial/tree/gesture-on-cell/TabDemo
方法2:在“收藏夹视图”上安装长按手势
我尝试过
- 在收藏夹视图上安装长
UILongPressGestureRecognizer
- 使用
gesture.minimumPressDuration = 0
模仿点击行为。
这是代码段
let gesture = UILongPressGestureRecognizer(target:self,action: #selector(longPressGesture))
// Mimic short tap. But this blocks the events for delete button and text field :-(
gesture.minimumPressDuration = 0
collectionView.addGestureRecognizer(gesture)
但是结果并不完美。
- 我们如何仅在重新排序图标(带有3条水平线的图标)边界内才能识别点击事件。
- 删除按钮不再起作用,因为
UILongPressGestureRecognizer
阻止了它接收事件。 - 文本字段不再起作用,因为
UILongPressGestureRecognizer
阻止了它接收事件。
完整的代码可以在这里找到:https://github.com/yccheok/ios-tutorial/tree/gesture-on-collection-view/TabDemo
解决方法
从 UICollectionView 中删除 UILongPressGestureRecognizer ,从 TabInfoSettingsItemCell 类中删除手势注释。 在 TabInfoSettingsController 中替换此方法:
func changed(_ gesture: UILongPressGestureRecognizer) {
print("==changed==")
collectionView?.updateInteractiveMovementTargetPosition(gesture.location(in: collectionView))
}
尝试一下。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。