首页 > 解决方案 > 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)
    }

标签: swiftxcode

解决方案


推荐阅读