自定义 UIDragPreview 相对于拖动触摸的位置

Custom UIDragPreview's position relative to drag touch

提问人:Kashif 提问时间:10/19/2023 更新时间:10/19/2023 访问量:20

问:

当用户从我的 UICollectionView 拖动项目时,我正在尝试提供自定义 UIDragPreview。它可以工作,但会向下移动预览,导致用户从顶部按住拖动的预览。默认行为是从用户开始拖动的点开始保持预览。有没有办法重置预览的位置或偏移其相对于触摸的位置。以下是我的代码的要点:

extension UIDragItem {
    func preview(size: CGSize) -> UIDragPreview? {
        var dragPreview: UIDragPreview?
        let expansion: CGFloat = 1.1
        let view = UIView(frame: CGRect(
            x: 0,
            y: 0,
            width: size.width * expansion,
            height: size.height * expansion
        ))
        let imageView = UIImageView(image: self.localObject as? UIImage)
        imageView.frame = CGRect(
            x: 0,
            y: 0,
            width: size.width * expansion,
            height: size.height * expansion
        )
        imageView.contentMode = .scaleAspectFit
        view.addSubview(imageView)

        dragPreview = UIDragPreview(view: view)
        return dragPreview
    }
}

我打电话来自:

func collectionView(_ collectionView: UICollectionView, dropSessionDidEnter session: UIDropSession) 
{
    if collectionView.hasActiveDrag {            
        for dragItem in session.items {
            dragItem.previewProvider = { () -> UIDragPreview? in
                return dragItem.preview(size: <SendingCollectionViewCellSizeHere>) 
            }
        }    
    }
}
iOS Swift UIColictionView UIDagPreview

评论

0赞 rmp 11/2/2023
你有没有想过这一点?我面临同样的问题。

答: 暂无答案