如何在拖动项目时更改预览图像

时间:2019-08-18 17:10:12

标签: swift uicollectionview

我有一个集合视图,其中一个大单元格位于(0,0),其余的单元格较小。我想使用拖放来重新排列单元格。

我希望预览图像调整到我所悬停的像元大小。因此,在单元格(0,0)上,它应该大于其余单元格。

但是一旦拖动完成,我就找不到改变预览图像大小的方法。

开始拖动时,我使用以下方法生成dragItem

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {

    let object = photos[indexPath.item].image
    let item = NSItemProvider(object: object)
    dragItem = UIDragItem(itemProvider: item)
    return [dragItem]
}

我尝试使用此功能,因为它为我提供了当前要拖动的indexPath。但是最终会产生抖动效果,并且单元不再重新排列。我猜是因为在拖动过程中多次调用了此方法。有什么解决方案?

func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if destinationIndexPath?.row == 0 {
  for item in session.items {
    item.previewProvider = {
      let test = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 200))
      return UIDragPreview(view: test)
    }

  }
} else {
  for item in session.items {
    item.previewProvider = {
      self.test = UITextView.init(frame: CGRect.init(x: 0, y: 0, width: 100, height: 100))
      return UIDragPreview(view: test)
    }
  }
}
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}

0 个答案:

没有答案