Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // DragNDropViewController.swift
- // Delta7
- //
- // Created by Oleksandr Lysenkov on 8/31/19.
- // Copyright © 2019 Oleksandr Lysenkov. All rights reserved.
- //
- import UIKit
- class DragNDropViewController: UIViewController {
- @IBOutlet weak var top: UICollectionView!
- @IBOutlet weak var bottom: UICollectionView!
- var topColors: [String] = ["#4FBCCA", "#3A9CA9", "#BE0E6D", "#A8045C"]
- var bottomColors: [String] = ["#4FBCCA", "#3A9CA9", "#BE0E6D", "#A8045C"]
- override func viewDidLoad() {
- super.viewDidLoad()
- top.delegate = self
- top.dataSource = self
- top.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "top")
- top.dragDelegate = self
- top.dropDelegate = self
- top.dragInteractionEnabled = true
- top.reorderingCadence = .fast
- bottom.delegate = self
- bottom.dataSource = self
- bottom.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "top")
- bottom.dragDelegate = self
- bottom.dropDelegate = self
- bottom.dragInteractionEnabled = true
- bottom.reorderingCadence = .fast
- }
- private func reorderItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
- let items = coordinator.items
- if items.count == 1, let item = items.first, let sourceIndexPath = item.sourceIndexPath {
- var dIndexPath = destinationIndexPath
- if dIndexPath.row >= collectionView.numberOfItems(inSection: 0)
- {
- dIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
- }
- collectionView.performBatchUpdates({
- if collectionView === self.bottom {
- self.bottomColors.remove(at: sourceIndexPath.row)
- self.bottomColors.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
- }
- else {
- self.topColors.remove(at: sourceIndexPath.row)
- self.topColors.insert(item.dragItem.localObject as! String, at: dIndexPath.row)
- }
- collectionView.deleteItems(at: [sourceIndexPath])
- collectionView.insertItems(at: [dIndexPath])
- })
- coordinator.drop(items.first!.dragItem, toItemAt: dIndexPath)
- }
- }
- private func copyItems(coordinator: UICollectionViewDropCoordinator, destinationIndexPath: IndexPath, collectionView: UICollectionView) {
- // collectionView.performBatchUpdates({
- // var indexPaths = [IndexPath]()
- // for (index, item) in coordinator.items.enumerated()
- // {
- // let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
- // if collectionView === self.bottom
- // {
- // self.bottomColors.insert(item.dragItem.localObject as! String, at: indexPath.row)
- // }
- // else
- // {
- // self.topColors.insert(item.dragItem.localObject as! String, at: indexPath.row)
- // }
- // indexPaths.append(indexPath)
- // }
- // collectionView.insertItems(at: indexPaths)
- // })
- guard let item = coordinator.items.first,
- let word = item.dragItem.localObject as? String else { return }
- //print(item.dragItem.localObject as! String)
- let lastIndex = collectionView.numberOfItems(inSection: 0)
- let destinationIndexPath = IndexPath(item: lastIndex, section: 0)
- // var destIndexPath = destinationIndexPath
- // if destIndexPath.row >= collectionView.numberOfItems(inSection: 0) {
- // destIndexPath.row = collectionView.numberOfItems(inSection: 0) - 1
- // }
- //
- collectionView.performBatchUpdates({
- if collectionView == top {
- topColors.append(word)
- topColors.remove(at: topColors.count-1)
- topColors.insert(item.dragItem.localObject as! String , at: destinationIndexPath.row)
- let index = bottomColors.firstIndex(where: { (wordTemp) -> Bool in
- return wordTemp == word
- })
- if let index = index {
- bottomColors.remove(at: index)
- bottom.deleteItems(at: [IndexPath(row: index, section: 0)])
- }
- collectionView.insertItems(at: [destinationIndexPath])
- }
- if collectionView == bottom {
- bottomColors.append(word)
- bottomColors.remove(at: bottomColors.count-1)
- bottomColors.insert(item.dragItem.localObject as! String , at: destinationIndexPath.row)
- let index = topColors.firstIndex(where: { (wordTemp) -> Bool in
- return wordTemp == word
- })
- if let index = index {
- topColors.remove(at: index)
- top.deleteItems(at: [IndexPath(row: index, section: 0)])
- }
- collectionView.insertItems(at: [destinationIndexPath])
- }
- })
- OperationQueue.main.addOperation { [weak self] in
- self?.top.reloadData()
- self?.bottom.reloadData()
- }
- // coordinator.drop(item.dragItem, toItemAt: destinationIndexPath)
- }
- }
- extension DragNDropViewController: UICollectionViewDelegate {
- }
- extension DragNDropViewController: UICollectionViewDataSource {
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return 4
- }
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- if collectionView == top {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "top", for: indexPath)
- cell.backgroundColor = UIColor(hexString: topColors[indexPath.row])
- return cell
- }
- if collectionView == bottom {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "top", for: indexPath)
- cell.backgroundColor = UIColor(hexString: bottomColors[indexPath.row])
- return cell
- }
- return UICollectionViewCell()
- }
- }
- extension DragNDropViewController: UICollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- let height = (collectionView.frame.height - 20) / 2
- return CGSize(width: height, height: height)
- }
- }
- extension DragNDropViewController: UICollectionViewDragDelegate {
- func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
- if collectionView == bottom {
- let color = bottomColors[indexPath.row]
- let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = color
- return [dragItem]
- }
- if collectionView == top {
- let color = topColors[indexPath.row]
- let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = color
- return [dragItem]
- }
- return []
- // let item = collectionView == top ? self.topColors[indexPath.row] : self.bottomColors[indexPath.row]
- // let itemProvider = NSItemProvider(object: item as NSString)
- // let dragItem = UIDragItem(itemProvider: itemProvider)
- // dragItem.localObject = item
- // return [dragItem]
- }
- func collectionView(_ collectionView: UICollectionView, itemsForAddingTo session: UIDragSession, at indexPath: IndexPath, point: CGPoint) -> [UIDragItem] {
- if collectionView == bottom {
- let color = bottomColors[indexPath.row]
- let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = color
- return [dragItem]
- }
- if collectionView == top {
- let color = topColors[indexPath.row]
- let itemProvider = NSItemProvider(object: color as NSItemProviderWriting )
- let dragItem = UIDragItem(itemProvider: itemProvider)
- dragItem.localObject = color
- return [dragItem]
- }
- //
- return []
- // let item = collectionView == top ? self.topColors[indexPath.row] : self.bottomColors[indexPath.row]
- // let itemProvider = NSItemProvider(object: item as NSString)
- // let dragItem = UIDragItem(itemProvider: itemProvider)
- // dragItem.localObject = item
- // return [dragItem]
- }
- func collectionView(_ collectionView: UICollectionView, dragPreviewParametersForItemAt indexPath: IndexPath) -> UIDragPreviewParameters? {
- return nil
- }
- }
- extension DragNDropViewController: UICollectionViewDropDelegate {
- func collectionView(_ collectionView: UICollectionView, canHandle session: UIDropSession) -> Bool {
- return session.canLoadObjects(ofClass: NSString.self)
- }
- func collectionView(_ collectionView: UICollectionView,
- dropSessionDidUpdate session: UIDropSession,
- withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
- if collectionView.hasActiveDrag {
- return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
- } else {
- return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
- }
- // if collectionView === self.top {
- // if collectionView.hasActiveDrag {
- // return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
- // } else {
- // return UICollectionViewDropProposal(operation: .forbidden)
- // }
- // } else {
- // if collectionView.hasActiveDrag {
- // return UICollectionViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
- // } else {
- // return UICollectionViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
- // }
- // }
- }
- func collectionView(_ collectionView: UICollectionView, performDropWith coordinator: UICollectionViewDropCoordinator) {
- let destinationIndexPath: IndexPath
- if let indexPath = coordinator.destinationIndexPath {
- destinationIndexPath = indexPath
- } else {
- let section = collectionView.numberOfSections - 1
- let row = collectionView.numberOfItems(inSection: section)
- destinationIndexPath = IndexPath(row: row, section: section)
- }
- switch coordinator.proposal.operation {
- case .move:
- reorderItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
- break
- case .copy:
- copyItems(coordinator: coordinator, destinationIndexPath:destinationIndexPath, collectionView: collectionView)
- default:
- return
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement