Skip to content

Instantly share code, notes, and snippets.

View paul1893's full-sized avatar

Paul paul1893

View GitHub Profile
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
let image:UIImage = getImage()
let provider = NSItemProvider(object: image)
let item = UIDragItem(itemProvider: provider) // Build your drag item
item.localObject = image
return [item]
}
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession)
let itemProvider = NSItemProvider()
itemProvider.registerFileRepresentation(forTypeIdentifier: kUTTypeJPEG as String, fileOptions: [.openInPlace], visibility: .all){ completionHandler in
//Get Url
let url = Attachement.url(forName: self.contactCard.name)
completionHandler(url, true, nil)
return nil
}
func dragInteraction(_ interaction:UIDragInteraction, previewForLifting item:UIDragItem, session:UIDragSession) -> UITargetedDragPreview? {
let imageView = UIImageView(image: UIImage(named: "MyDragImage"))
let dragView = interaction.view!
let dragPoint = session.location(in: dragView)
let target = UIDragPreviewTarget(container: dragView, center: dragPoint)
return UITargetedDragPreview(view: imageView, parameters:UIDragPreviewParameters())
}
func tableView(_ tableView: UITableView, dragPreviewParametersForRowAt indexPath: IndexPath) -> UIDragPreviewParameters? {
let parameters = UIDragPreviewParameters()
parameters.visiblePath = yourCustomBezierPath
return parameters
}
func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
let destinationIndexPath = coordinator.destinationIndexPath ?? IndexPath(item: 0, section: 0)
coordinator.session.progressIndicatorStyle = .none
for item in coordinator.items {
let itemProvider = item.dragItem.itemProvider
guard itemProvider.canLoadObject(ofClass: UIImage.self) else { continue }
var placeholderContext: UICollectionViewDropPlaceholderContext? = nil
func collectionView(_ collectionView: UICollectionView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
if session.localDragSession != nil {
return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
} else {
return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
}
}
func tableView(_ tableView: UITableView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
// Return an empty array to handle the tap normally or array to add items already got from itemsForBeginning
let image = arrImg[indexPath.row]
let provider = NSItemProvider(object: image as NSItemProviderWriting)
let item = UIDragItem(itemProvider: provider)
item.localObject = image
return [item]
}
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
// 1) Première possibilité
let progress = session.loadObjects(ofClass: UIImage.self) { imageItems in
let images = imageItems as! [UIImage]
self.selectedImageView.image = images.first
}
// 2) Seconde possibilité
for item in session.items {
item.itemProvider.loadObject(ofClass: UIImage.self) { (object, error) in
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
let dropLocation = session.location(in: view)
let operation: UIDropOperation
if selectedImageView.frame.contains(dropLocation) {
if session.localDragSession == nil { // Drag come frome the app
operation = .copy
} else { // Drag come from a third app
operation = .move
}
@paul1893
paul1893 / Drop interaction - specify data type
Last active January 2, 2018 13:15
drag and drop ios 11 octo article
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
let identifiers = [kUTTypeImage as String] // Can be very specific [kUTTypeImagePNG as String] (Only PNG)
return session.hasItemsConforming(toTypeIdentifiers: identifiers) && session.items.count == 1
}