iOS Development

ios – Customized UIDragPreview’s place relative to pull contact

I’m attempting to offer a customized UIDragPreview when consumer drags an merchandise from my UICollectionView. It really works however shifts the preview down leading to consumer holding the dragged preview from the highest. Default behaviour is to carry the preview from the purpose consumer initiated the drag from. Is there a means I can reset the place of the preview OR offset its place relative to the contact. Beneath is gist of my code:

extension UIDragItem {
    func preview(measurement: CGSize) -> UIDragPreview? {
        var dragPreview: UIDragPreview?
        let enlargement: CGFloat = 1.1
        let view = UIView(body: CGRect(
            x: 0,
            y: 0,
            width: measurement.width * enlargement,
            top: measurement.top * enlargement
        ))
        let imageView = UIImageView(picture: self.localObject as? UIImage)
        imageView.body = CGRect(
            x: 0,
            y: 0,
            width: measurement.width * enlargement,
            top: measurement.top * enlargement
        )
        imageView.contentMode = .scaleAspectFit
        view.addSubview(imageView)

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

which I’m calling from:

func collectionView(_ collectionView: UICollectionView, dropSessionDidEnter session: UIDropSession) 
{
    if collectionView.hasActiveDrag {            
        for dragItem in session.gadgets {
            dragItem.previewProvider = { () -> UIDragPreview? in
                return dragItem.preview(measurement: <SendingCollectionViewCellSizeHere>) 
            }
        }    
    }
}

Credit: www.ismmailgsm.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button