swift - swift UICollectionView 类似预览和拖动项
问题描述
我是 swift 的新手,我正在开发 UICollectionView 拖放。
现在我几乎完成了,但我遇到了一些问题。我想删除透明预览并删除十字按钮。并使预览与可拖动对象相同。
这是我的代码:
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let photo = presenter.petPhotos[indexPath.row]
guard let cell = (collectionView.cellForItem(at: IndexPath(row: 2, section: 0)) as? PetPhotoCollectionViewCell) else {return []}
let cellSize = cell.mainImage.frame.size
guard let thumbnail = photo.path else {
return []
}
let itemProvider = NSItemProvider(object: photo.path! as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = photo
dragItem.previewProvider =
{ () -> UIDragPreview in
let image = UIImage(contentsOfFile: (URL(string: thumbnail)?.path)!)
let imageView = UIImageView(image: image)
imageView.bounds.size = cellSize
imageView.frame = imageView.contentClippingRect
imageView.cornerRadius = 9
imageView.layer.shadowColor = UIColor.black.cgColor
imageView.layer.shadowOffset = CGSize(width: 0, height: 2.0)
imageView.layer.shadowRadius = 3.0
imageView.layer.shadowOpacity = 0.5
imageView.layer.masksToBounds = true
imageView.layer.shadowPath = UIBezierPath(roundedRect: imageView.bounds, cornerRadius: imageView.layer.cornerRadius).cgPath
return UIDragPreview(view: imageView)
}
return [dragItem]
}
public func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
let cell = collectionView.cellForItem(at: IndexPath(row: 2, section: 0)) as? PetPhotoCollectionViewCell
let previewParameters = UIDragPreviewParameters()
previewParameters.visiblePath = UIBezierPath(roundedRect: (cell?.mainImage.frame)!, cornerRadius: 9)
return previewParameters
}
func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
解决方案
推荐阅读
- reactjs - 将文本复制到剪贴板 react.js
- php - 我想使用 php 隐藏没有图像的图像 div
- c - 如何使用C检查最后一次操作
- javascript - 我可以期望 DOM 在下一个 VM 滴答声中准备就绪吗
- windows - 小文件上的scipy bow内存错误
- python - 尝试在泡菜对象上使用 atomic_write
- perl - 为什么在使用 Perl 在线替换注释处理大文件(3Gb)文件时出现“内存不足错误”
- python - Sonarqube:行超出范围,因为与主文件合并后文件缩小
- python - Netsuite 命名空间冲突(core_2017_2.platform 与 accounting_2017_2.lists)
- hbase - 在更新 TTL 之前是否必须禁用 Hbase 表?