如何在 UICollectionView 拖放期间删除“幽灵”单元格,并使移动单元格不透明?

     2023-05-08     252

关键词:

【中文标题】如何在 UICollectionView 拖放期间删除“幽灵”单元格,并使移动单元格不透明?【英文标题】:How to remove 'ghost' cell during UICollectionView drag/drop, and make moving cell opaque? 【发布时间】:2019-09-25 12:05:24 【问题描述】:

我正在尝试在我的 UICollectionView 中实现拖放机制,这与在 Shortcuts 应用程序中重新排序快捷方式的组件非常相似。

到目前为止,行为是当您开始拖动时,会留下一个透明的单元格视图,而另一个透明的单元格视图会随着手指移动。

我想摆脱“幽灵”细胞,让移动的细胞完全不透明。

这是我的拖放行为(主要取自 this post)

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] 
    let item = itemData[indexPath.item]
    let itemProvider = NSItemProvider(object: item.title as NSString)
    let dragItem = UIDragItem(itemProvider: itemProvider)
    dragItem.localObject = item


    return [dragItem]


func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal 

    if collectionView.hasActiveDrag 
        return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
    

    return UICollectionViewDropProposal(operation: .forbidden)


func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) 

    var destinationIndexPath: IndexPath
    if let indexPath = coordinator.destinationIndexPath 
        destinationIndexPath = indexPath
    
    else 
        let row = collectionView.numberOfItems(inSection: 0)
        destinationIndexPath = IndexPath(row: row - 1, section: 0)
    

    reorderItems(coordinator: coordinator, destinationIndexPath: destinationIndexPath, collectionView: collectionView)



fileprivate func reorderItems(coordinator: UICollectionViewDropCoordinator,
                              destinationIndexPath: IndexPath,
                              collectionView: UICollectionView) 

    if let item = coordinator.items.first,
        let sourceIndexPath = item.sourceIndexPath 

        collectionView.performBatchUpdates(
            self.itemData.remove(at: sourceIndexPath.item)
            self.itemData.insert(item.dragItem.localObject as! PlanItem, at: destinationIndexPath.item)

            collectionView.deleteItems(at: [sourceIndexPath])
            collectionView.insertItems(at: [destinationIndexPath])
        , completion: nil)

        coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
    

【问题讨论】:

【参考方案1】:

不确定问题是否仍然存在,但如果是: 这是处理单元格拖动状态的方法:

Is there a method to check the UICollectionView item drag cancellation if the item wasn't moved?

只需通过 DispatchQueue.main.async

将其隐藏在拖动中

并在 .none 状态下再次可见。为我工作:)

【讨论】:

这并没有解释如何隐藏幽灵单元。

如何在 dragover/dragenter HTML 5 拖放期间更改图标

】如何在dragover/dragenterHTML5拖放期间更改图标【英文标题】:Howtochangeiconduringdragover/dragenterHTML5DragandDrop【发布时间】:2018-06-2102:47:50【问题描述】:如何在dragover或dragenter期间更改DnD的(拖放)图标?有可能吗?如果拖放源位于h... 查看详情

如何在 OSX 上拖放期间检测 META 按键

】如何在OSX上拖放期间检测META按键【英文标题】:HowtodetectMETAkeypressduringDragandDroponOSX【发布时间】:2016-12-0123:57:34【问题描述】:在OSX上的Java6/7中存在一个错误,即在拖放操作期间,它会忽略META(CMD)键。(Ctrl键在Windows上工作... 查看详情

如何在 WPF 拖放期间更新拖动装饰器的位置?

】如何在WPF拖放期间更新拖动装饰器的位置?【英文标题】:HowtoupdatethepositionofadragadornerduringWPFdrag-n-drop?【发布时间】:2010-01-1915:10:55【问题描述】:我正在使用装饰器来显示被拖动元素的“幽灵”...varadornerLayer=AdornerLayer.GetAdor... 查看详情

UITableView - 如何在拖放期间更改单元格的背景颜色?

】UITableView-如何在拖放期间更改单元格的背景颜色?【英文标题】:UITableView-Howtochangethebackgroundcolorofcellduringdragdrop?【发布时间】:2018-03-1908:54:29【问题描述】:我正在两个UITableView之间实现拖放。当在tableView上拖动项目时,table... 查看详情

如何在从另一个小部件拖放事件期间突出显示表格单元格

】如何在从另一个小部件拖放事件期间突出显示表格单元格【英文标题】:Howtohighlighttablecellduringdrag&dropeventfromanotherwidget【发布时间】:2018-11-2420:41:44【问题描述】:有两个可停靠的小部件:一个带有图标(QLabel),另一个... 查看详情

在具有 UITableView 的 UICollectionView 中拖放

】在具有UITableView的UICollectionView中拖放【英文标题】:DragandDropinUICollectionViewthathasUITableView【发布时间】:2016-09-2711:05:53【问题描述】:我有水平滚动的UICollectionView。每个集合视图单元格都有一个垂直滚动的表格视图。我想在表... 查看详情

拖放 UICollectionView 单元格重用问题

】拖放UICollectionView单元格重用问题【英文标题】:Drag&DropUICollectionViewcellreuseproblem【发布时间】:2020-06-1404:17:01【问题描述】:所以我已经为我的UICollectionView实现了拖放,如下所示:funccollectionView(_collectionView:UICollectionView,ite... 查看详情

UICollectionView 快速拖放

】UICollectionView快速拖放【英文标题】:UICollectionViewDrag-N-Dropatswift【发布时间】:2019-10-1106:19:33【问题描述】:我正在快速使用UICollectionView制作一个项目,我需要对每个框执行拖放操作并交换它们的位置。每个盒子都有不同的数... 查看详情

UICollectionView 拖放单元格

】UICollectionView拖放单元格【英文标题】:UICollectionViewDragandDropcell【发布时间】:2019-10-1607:24:09【问题描述】:您好,我想使用uicollectionview进行拖放。执行拖放时,它正在移动内容,我想像照片中那样做。我想让盒子自己携带。... 查看详情

IOS拖放与缩放

...有一个UIView子类,其中包含多个子视图,我想将其拖放到UICollectionView中包含的其他几个UIView之一中。当拖动开始时,我想在拖动期间将拖动的视图从其当前大小缩放到更小的尺寸(原件太大,无法方便地选择放置目标而无需先... 查看详情

在添加期间在 UICollectionView 的屏幕上保留页脚

】在添加期间在UICollectionView的屏幕上保留页脚【英文标题】:KeepingafooteronscreeninUICollectionViewduringanAdd【发布时间】:2013-09-0322:23:29【问题描述】:我有一个标准的FlowLayoutUICollectionView(垂直滚动、单节、固定大小的项目,但列数... 查看详情

使控件在拖放期间对拖动事件透明

】使控件在拖放期间对拖动事件透明【英文标题】:Makecontroltransparenttodrageventsduringdrag-and-drop【发布时间】:2015-10-1506:44:02【问题描述】:我有一个ListView,它充当拖放操作的放置目标。当列表为空时,我在其顶部放置一个TextBloc... 查看详情

在滚动期间点击时,UICollectionView 不再识别单元格上的点击?

】在滚动期间点击时,UICollectionView不再识别单元格上的点击?【英文标题】:UICollectionViewdoesnotrecognizetaponcellanymorewhentappedduringscrolling?【发布时间】:2016-04-1712:12:38【问题描述】:UICollectionView出现,并且无需滚动触摸其中一个... 查看详情

在拖放期间在 NSTableView 中打开一个间隙

】在拖放期间在NSTableView中打开一个间隙【英文标题】:OpeningagapinNSTableViewduringdraganddrop【发布时间】:2012-04-2522:14:10【问题描述】:我有一个简单的、单列、基于视图的NSTableView,其中包含可以拖动以重新排序的项目。在拖放过... 查看详情

Chrome F8/热键调试器在拖放操作期间中断

】ChromeF8/热键调试器在拖放操作期间中断【英文标题】:ChromeF8/hotkeydebuggerbreakingduringadraganddropoperation【发布时间】:2015-08-1600:08:40【问题描述】:在Chrome的网络开发工具中,您可以随时按下F8中断。通常我想在拖放操作期间通过... 查看详情

如果在重新加载期间触摸单元格,则重新加载后无法选择 UICollectionView 单元格

...如果在重新加载期间触摸单元格,则重新加载后无法选择UICollectionView单元格【英文标题】:UICollectionViewcellcan\'tbeselectedafterreloadincaseifcellwastouchedduringreloading【发布时间】:2014-05-2918:29:07【问题描述】:我使用UICollectionView来显示... 查看详情

防止 UICollectionView 在 vi​​ew.layoutIfNeeded 期间设置动画

】防止UICollectionView在vi​​ew.layoutIfNeeded期间设置动画【英文标题】:PreventUICollectionViewfromanimatingduringview.layoutIfNeeded【发布时间】:2017-08-1721:03:10【问题描述】:我正在我的应用中构建一个与消息应用非常相似的页面。此页面顶... 查看详情

UICollectionView 拖放更新所有受影响的 indexPaths

】UICollectionView拖放更新所有受影响的indexPaths【英文标题】:UICollectionViewDragandDropupdateallaffectedindexPaths【发布时间】:2014-05-2710:27:55【问题描述】:我有一个UICollectionView,它使用拖放功能对单元格进行手动重新排序。我只有1个... 查看详情