I can grab and drag an image onto an image, but it immediately disappears.If I drag a littleImage onto a FtbllImage, then as soon as I release it, the littleImage disappears and is not saved on top What could be the cause and how can I fix it?
class ViewController: UIViewController, UIDragInteractionDelegate,UIDropInteractionDelegate {
@IBOutlet weak var directIV: UIImageView!
@IBOutlet weak var littleIV: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let dragInteraction = UIDragInteraction(delegate: self)
littleIV.addInteraction(dragInteraction)
directIV.isUserInteractionEnabled = true
littleIV.isUserInteractionEnabled = true
let dropInteraction = UIDropInteraction(delegate: self)
directIV.addInteraction(dropInteraction)
directIV.image = UIImage(named: "FtbllImage")
littleIV.image = UIImage(named: "player")
}
func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] {
let itemProvider = NSItemProvider(object: directIV.image!)
let dragItem = UIDragItem(itemProvider: itemProvider)
return [dragItem]
}
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: UIImage.self)
}
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
let operation: UIDropOperation = session.localDragSession == nil ? .copy : .move
return UIDropProposal(operation: operation)
}
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
session.loadObjects(ofClass: UIImage.self) { imageItems in
guard let images = imageItems as? [UIImage], let image = images.first else { return }
DispatchQueue.main.async {
self.directIV.image = image
}
}
}
}
I’m trying to figure out the drag and drop mechanics for another app of mine, but I don’t understand why the image disappears after dragging here
New contributor
Александр Узбек is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
6