With iOS 11, you can use Drag and Drop and copy / paste the API to perform a drag and drop UIImage operation from one UIImageView to another UIImageView . According to your needs, you can choose one of the following two implementations of Swift 4.
# 1. Using UIDragInteraction , UIDragInteractionDelegate and UIPasteConfiguration
import UIKit class ViewController: UIViewController { let imageView1 = UIImageView() let imageView2 = UIImageView() override func viewDidLoad() { super.viewDidLoad() imageView1.image = UIImage(named: "image") imageView1.contentMode = .scaleAspectFit imageView1.isUserInteractionEnabled = true let dragInteraction = UIDragInteraction(delegate: self) dragInteraction.isEnabled = true imageView1.addInteraction(dragInteraction) imageView2.contentMode = .scaleAspectFit imageView2.isUserInteractionEnabled = true let configuration = UIPasteConfiguration(forAccepting: UIImage.self) imageView2.pasteConfiguration = configuration let stackView = UIStackView(arrangedSubviews: [imageView1, imageView2]) view.addSubview(stackView) stackView.distribution = .fillEqually stackView.frame = view.bounds stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight] } override func paste(itemProviders: [NSItemProvider]) { _ = itemProviders.first?.loadObject(ofClass: UIImage.self, completionHandler: { (image: NSItemProviderReading?, error: Error?) in DispatchQueue.main.async { self.imageView2.image = image as? UIImage } }) } }
extension ViewController: UIDragInteractionDelegate { func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] { guard let image = imageView1.image else { return [] } let item = UIDragItem(itemProvider: NSItemProvider(object: image)) return [item] } }
# 2. Using UIDragInteraction , UIDragInteractionDelegate , UIDropInteraction and UIDropInteractionDelegate
import UIKit class ViewController: UIViewController { let imageView1 = UIImageView() let imageView2 = UIImageView() override func viewDidLoad() { super.viewDidLoad() imageView1.image = UIImage(named: "image") imageView1.contentMode = .scaleAspectFit imageView1.isUserInteractionEnabled = true imageView2.contentMode = .scaleAspectFit imageView2.isUserInteractionEnabled = true let dragInteraction = UIDragInteraction(delegate: self) dragInteraction.isEnabled = true imageView1.addInteraction(dragInteraction) let dropInteraction = UIDropInteraction(delegate: self) imageView2.addInteraction(dropInteraction) let stackView = UIStackView(arrangedSubviews: [imageView1, imageView2]) view.addSubview(stackView) stackView.distribution = .fillEqually stackView.frame = view.bounds stackView.autoresizingMask = [.flexibleWidth, .flexibleHeight] } }
extension ViewController: UIDragInteractionDelegate { func dragInteraction(_ interaction: UIDragInteraction, itemsForBeginning session: UIDragSession) -> [UIDragItem] { guard let image = imageView1.image else { return [] } let item = UIDragItem(itemProvider: NSItemProvider(object: image)) item.localObject = image return [item] } }
extension ViewController: UIDropInteractionDelegate { func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool { return session.canLoadObjects(ofClass: UIImage.self) && session.items.count == 1 } func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal { let dropLocation = session.location(in: view) let operation: UIDropOperation if imageView2.frame.contains(dropLocation) { operation = session.localDragSession == nil ? .copy : .move } else { operation = .cancel } return UIDropProposal(operation: operation) } func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) { session.loadObjects(ofClass: UIImage.self) { imageItems in guard let images = imageItems as? [UIImage] else { return } self.imageView2.image = images.first } } }
source share