├── .gitignore ├── .swift-version ├── Classes ├── KDDragAndDropCollectionView.swift └── KDDragAndDropManager.swift ├── KDDragAndDropCollectionViews.podspec ├── KDDragAndDropCollectionViews.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ ├── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ │ └── michaelmichailidis.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── michaelmichailidis.xcuserdatad │ └── xcschemes │ ├── KDDragAndDropCollectionViews.xcscheme │ └── xcschememanagement.plist ├── KDDragAndDropCollectionViews ├── AppDelegate.swift ├── Base.lproj │ ├── LaunchScreen.xib │ └── Main.storyboard ├── ColorCell.swift ├── Images.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Info.plist └── ViewController.swift ├── KDDragAndDropCollectionViewsTests ├── Info.plist └── KDDragAndDropCollectionViewsTests.swift ├── LICENSE ├── README.md └── Resources ├── Screenshot.Installation.png └── header.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | DerivedData/ 3 | **/xcuserdata/** 4 | -------------------------------------------------------------------------------- /.swift-version: -------------------------------------------------------------------------------- 1 | 4.2 2 | -------------------------------------------------------------------------------- /Classes/KDDragAndDropCollectionView.swift: -------------------------------------------------------------------------------- 1 | /* 2 | * KDDragAndDropCollectionView.swift 3 | * Created by Michael Michailidis on 10/04/2015. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | * 23 | */ 24 | 25 | import UIKit 26 | 27 | public protocol KDDragAndDropCollectionViewDataSource : UICollectionViewDataSource { 28 | 29 | func collectionView(_ collectionView: UICollectionView, indexPathForDataItem dataItem: AnyObject) -> IndexPath? 30 | func collectionView(_ collectionView: UICollectionView, dataItemForIndexPath indexPath: IndexPath) -> AnyObject 31 | 32 | func collectionView(_ collectionView: UICollectionView, moveDataItemFromIndexPath from: IndexPath, toIndexPath to : IndexPath) -> Void 33 | func collectionView(_ collectionView: UICollectionView, insertDataItem dataItem : AnyObject, atIndexPath indexPath: IndexPath) -> Void 34 | func collectionView(_ collectionView: UICollectionView, deleteDataItemAtIndexPath indexPath: IndexPath) -> Void 35 | 36 | /* optional */ func collectionView(_ collectionView: UICollectionView, cellIsDraggableAtIndexPath indexPath: IndexPath) -> Bool 37 | /* optional */ func collectionView(_ collectionView: UICollectionView, cellIsDroppableAtIndexPath indexPath: IndexPath) -> Bool 38 | 39 | /* optional */ func collectionView(_ collectionView: UICollectionView, stylingRepresentationView: UIView) -> UIView? 40 | } 41 | 42 | extension KDDragAndDropCollectionViewDataSource { 43 | public func collectionView(_ collectionView: UICollectionView, stylingRepresentationView: UIView) -> UIView? { 44 | return nil 45 | } 46 | func collectionView(_ collectionView: UICollectionView, cellIsDraggableAtIndexPath indexPath: IndexPath) -> Bool { 47 | return true 48 | } 49 | func collectionView(_ collectionView: UICollectionView, cellIsDroppableAtIndexPath indexPath: IndexPath) -> Bool { 50 | return true 51 | } 52 | } 53 | 54 | open class KDDragAndDropCollectionView: UICollectionView, KDDraggable, KDDroppable { 55 | 56 | required public init?(coder aDecoder: NSCoder) { 57 | super.init(coder: aDecoder) 58 | } 59 | 60 | public var draggingPathOfCellBeingDragged : IndexPath? 61 | 62 | var iDataSource : UICollectionViewDataSource? 63 | var iDelegate : UICollectionViewDelegate? 64 | 65 | override open func awakeFromNib() { 66 | super.awakeFromNib() 67 | 68 | } 69 | 70 | override public init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { 71 | super.init(frame: frame, collectionViewLayout: layout) 72 | } 73 | 74 | 75 | // MARK : KDDraggable 76 | public func canDragAtPoint(_ point : CGPoint) -> Bool { 77 | if let dataSource = self.dataSource as? KDDragAndDropCollectionViewDataSource, 78 | let indexPathOfPoint = self.indexPathForItem(at: point) { 79 | return dataSource.collectionView(self, cellIsDraggableAtIndexPath: indexPathOfPoint) 80 | } 81 | 82 | return false 83 | } 84 | 85 | public func representationImageAtPoint(_ point : CGPoint) -> UIView? { 86 | 87 | guard let indexPath = self.indexPathForItem(at: point) else { 88 | return nil 89 | } 90 | 91 | guard let cell = self.cellForItem(at: indexPath) else { 92 | return nil 93 | } 94 | 95 | UIGraphicsBeginImageContextWithOptions(cell.bounds.size, cell.isOpaque, 0) 96 | cell.layer.render(in: UIGraphicsGetCurrentContext()!) 97 | let image = UIGraphicsGetImageFromCurrentImageContext() 98 | UIGraphicsEndImageContext() 99 | 100 | let imageView = UIImageView(image: image) 101 | imageView.frame = cell.frame 102 | 103 | return imageView 104 | } 105 | 106 | public func stylingRepresentationView(_ view: UIView) -> UIView? { 107 | guard let dataSource = self.dataSource as? KDDragAndDropCollectionViewDataSource else { 108 | return nil 109 | } 110 | return dataSource.collectionView(self, stylingRepresentationView: view) 111 | } 112 | 113 | public func dataItemAtPoint(_ point : CGPoint) -> AnyObject? { 114 | 115 | guard let indexPath = self.indexPathForItem(at: point) else { 116 | return nil 117 | } 118 | 119 | guard let dragDropDS = self.dataSource as? KDDragAndDropCollectionViewDataSource else { 120 | return nil 121 | } 122 | 123 | return dragDropDS.collectionView(self, dataItemForIndexPath: indexPath) 124 | } 125 | 126 | 127 | 128 | public func startDraggingAtPoint(_ point : CGPoint) -> Void { 129 | 130 | self.draggingPathOfCellBeingDragged = self.indexPathForItem(at: point) 131 | 132 | self.reloadData() 133 | 134 | } 135 | 136 | public func stopDragging() -> Void { 137 | 138 | if let idx = self.draggingPathOfCellBeingDragged { 139 | if let cell = self.cellForItem(at: idx) { 140 | cell.isHidden = false 141 | } 142 | } 143 | 144 | self.draggingPathOfCellBeingDragged = nil 145 | 146 | self.reloadData() 147 | 148 | } 149 | 150 | public func dragDataItem(_ item : AnyObject) -> Void { 151 | 152 | guard let dragDropDataSource = self.dataSource as? KDDragAndDropCollectionViewDataSource else { 153 | return 154 | } 155 | 156 | guard let existngIndexPath = dragDropDataSource.collectionView(self, indexPathForDataItem: item) else { 157 | return 158 | 159 | } 160 | 161 | dragDropDataSource.collectionView(self, deleteDataItemAtIndexPath: existngIndexPath) 162 | 163 | if self.animating { 164 | self.deleteItems(at: [existngIndexPath]) 165 | } 166 | else { 167 | 168 | self.animating = true 169 | self.performBatchUpdates({ () -> Void in 170 | self.deleteItems(at: [existngIndexPath]) 171 | }, completion: { complete -> Void in 172 | self.animating = false 173 | self.reloadData() 174 | }) 175 | } 176 | 177 | } 178 | 179 | // MARK : KDDroppable 180 | 181 | public func canDropAtRect(_ rect : CGRect) -> Bool { 182 | 183 | return (self.indexPathForCellOverlappingRect(rect) != nil) 184 | } 185 | 186 | public func indexPathForCellOverlappingRect( _ rect : CGRect) -> IndexPath? { 187 | 188 | var overlappingArea : CGFloat = 0.0 189 | var cellCandidate : UICollectionViewCell? 190 | let dataSource = self.dataSource as? KDDragAndDropCollectionViewDataSource 191 | 192 | 193 | let visibleCells = self.visibleCells 194 | if visibleCells.count == 0 { 195 | return IndexPath(row: 0, section: 0) 196 | } 197 | 198 | if isHorizontal && rect.origin.x > self.contentSize.width || 199 | !isHorizontal && rect.origin.y > self.contentSize.height { 200 | 201 | if dataSource?.collectionView(self, cellIsDroppableAtIndexPath: IndexPath(row: visibleCells.count - 1, section: 0)) == true { 202 | return IndexPath(row: visibleCells.count - 1, section: 0) 203 | } 204 | return nil 205 | } 206 | 207 | 208 | for visible in visibleCells { 209 | 210 | let intersection = visible.frame.intersection(rect) 211 | 212 | if (intersection.width * intersection.height) > overlappingArea { 213 | 214 | overlappingArea = intersection.width * intersection.height 215 | 216 | cellCandidate = visible 217 | } 218 | 219 | } 220 | 221 | if let cellRetrieved = cellCandidate, let indexPath = self.indexPath(for: cellRetrieved), dataSource?.collectionView(self, cellIsDroppableAtIndexPath: indexPath) == true { 222 | 223 | return self.indexPath(for: cellRetrieved) 224 | } 225 | 226 | return nil 227 | } 228 | 229 | 230 | fileprivate var currentInRect : CGRect? 231 | public func willMoveItem(_ item : AnyObject, inRect rect : CGRect) -> Void { 232 | 233 | let dragDropDataSource = self.dataSource as! KDDragAndDropCollectionViewDataSource // its guaranteed to have a data source 234 | 235 | if let _ = dragDropDataSource.collectionView(self, indexPathForDataItem: item) { // if data item exists 236 | return 237 | } 238 | 239 | if let indexPath = self.indexPathForCellOverlappingRect(rect) { 240 | 241 | dragDropDataSource.collectionView(self, insertDataItem: item, atIndexPath: indexPath) 242 | 243 | self.draggingPathOfCellBeingDragged = indexPath 244 | 245 | self.animating = true 246 | 247 | self.performBatchUpdates({ () -> Void in 248 | 249 | self.insertItems(at: [indexPath]) 250 | 251 | }, completion: { complete -> Void in 252 | 253 | self.animating = false 254 | 255 | // if in the meantime we have let go 256 | if self.draggingPathOfCellBeingDragged == nil { 257 | 258 | self.reloadData() 259 | } 260 | 261 | 262 | }) 263 | 264 | } 265 | 266 | currentInRect = rect 267 | 268 | } 269 | 270 | public var isHorizontal : Bool { 271 | return (self.collectionViewLayout as? UICollectionViewFlowLayout)?.scrollDirection == .horizontal 272 | } 273 | 274 | public var animating: Bool = false 275 | 276 | public var paging : Bool = false 277 | func checkForEdgesAndScroll(_ rect : CGRect) -> Void { 278 | 279 | if paging == true { 280 | return 281 | } 282 | 283 | let currentRect : CGRect = CGRect(x: self.contentOffset.x, y: self.contentOffset.y, width: self.bounds.size.width, height: self.bounds.size.height) 284 | var rectForNextScroll : CGRect = currentRect 285 | 286 | if isHorizontal { 287 | 288 | let leftBoundary = CGRect(x: -30.0, y: 0.0, width: 30.0, height: self.frame.size.height) 289 | let rightBoundary = CGRect(x: self.frame.size.width, y: 0.0, width: 30.0, height: self.frame.size.height) 290 | 291 | if rect.intersects(leftBoundary) == true { 292 | rectForNextScroll.origin.x -= self.bounds.size.width * 0.5 293 | if rectForNextScroll.origin.x < 0 { 294 | rectForNextScroll.origin.x = 0 295 | } 296 | } 297 | else if rect.intersects(rightBoundary) == true { 298 | rectForNextScroll.origin.x += self.bounds.size.width * 0.5 299 | if rectForNextScroll.origin.x > self.contentSize.width - self.bounds.size.width { 300 | rectForNextScroll.origin.x = self.contentSize.width - self.bounds.size.width 301 | } 302 | } 303 | 304 | } else { // is vertical 305 | 306 | let topBoundary = CGRect(x: 0.0, y: -30.0, width: self.frame.size.width, height: 30.0) 307 | let bottomBoundary = CGRect(x: 0.0, y: self.frame.size.height, width: self.frame.size.width, height: 30.0) 308 | 309 | if rect.intersects(topBoundary) == true { 310 | rectForNextScroll.origin.y -= self.bounds.size.height * 0.5 311 | if rectForNextScroll.origin.y < 0 { 312 | rectForNextScroll.origin.y = 0 313 | } 314 | } 315 | else if rect.intersects(bottomBoundary) == true { 316 | rectForNextScroll.origin.y += self.bounds.size.height * 0.5 317 | if rectForNextScroll.origin.y > self.contentSize.height - self.bounds.size.height { 318 | rectForNextScroll.origin.y = self.contentSize.height - self.bounds.size.height 319 | } 320 | } 321 | } 322 | 323 | // check to see if a change in rectForNextScroll has been made 324 | if currentRect.equalTo(rectForNextScroll) == false { 325 | self.paging = true 326 | self.scrollRectToVisible(rectForNextScroll, animated: true) 327 | 328 | let delayTime = DispatchTime.now() + Double(Int64(1 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC) 329 | DispatchQueue.main.asyncAfter(deadline: delayTime) { 330 | self.paging = false 331 | } 332 | 333 | } 334 | 335 | } 336 | 337 | public func didMoveItem(_ item : AnyObject, inRect rect : CGRect) -> Void { 338 | 339 | let dragDropDS = self.dataSource as! KDDragAndDropCollectionViewDataSource // guaranteed to have a ds 340 | 341 | if let existingIndexPath = dragDropDS.collectionView(self, indexPathForDataItem: item), 342 | let indexPath = self.indexPathForCellOverlappingRect(rect) { 343 | 344 | if indexPath.item != existingIndexPath.item { 345 | 346 | dragDropDS.collectionView(self, moveDataItemFromIndexPath: existingIndexPath, toIndexPath: indexPath) 347 | 348 | self.animating = true 349 | 350 | self.performBatchUpdates({ () -> Void in 351 | self.moveItem(at: existingIndexPath, to: indexPath) 352 | }, completion: { (finished) -> Void in 353 | self.animating = false 354 | self.reloadData() 355 | 356 | }) 357 | 358 | self.draggingPathOfCellBeingDragged = indexPath 359 | 360 | } 361 | } 362 | 363 | // Check Paging 364 | 365 | var normalizedRect = rect 366 | normalizedRect.origin.x -= self.contentOffset.x 367 | normalizedRect.origin.y -= self.contentOffset.y 368 | 369 | currentInRect = normalizedRect 370 | 371 | 372 | self.checkForEdgesAndScroll(normalizedRect) 373 | 374 | 375 | } 376 | 377 | public func didMoveOutItem(_ item : AnyObject) -> Void { 378 | 379 | guard let dragDropDataSource = self.dataSource as? KDDragAndDropCollectionViewDataSource, 380 | let existngIndexPath = dragDropDataSource.collectionView(self, indexPathForDataItem: item) else { 381 | 382 | return 383 | } 384 | 385 | dragDropDataSource.collectionView(self, deleteDataItemAtIndexPath: existngIndexPath) 386 | 387 | if self.animating { 388 | self.deleteItems(at: [existngIndexPath]) 389 | } 390 | else { 391 | self.animating = true 392 | self.performBatchUpdates({ () -> Void in 393 | self.deleteItems(at: [existngIndexPath]) 394 | }, completion: { (finished) -> Void in 395 | self.animating = false; 396 | self.reloadData() 397 | }) 398 | 399 | } 400 | 401 | if let idx = self.draggingPathOfCellBeingDragged { 402 | if let cell = self.cellForItem(at: idx) { 403 | cell.isHidden = false 404 | } 405 | } 406 | 407 | self.draggingPathOfCellBeingDragged = nil 408 | 409 | currentInRect = nil 410 | } 411 | 412 | 413 | public func dropDataItem(_ item : AnyObject, atRect : CGRect) -> Void { 414 | 415 | // show hidden cell 416 | if let index = draggingPathOfCellBeingDragged, 417 | let cell = self.cellForItem(at: index), cell.isHidden == true { 418 | 419 | cell.alpha = 1 420 | cell.isHidden = false 421 | 422 | } 423 | 424 | currentInRect = nil 425 | 426 | self.draggingPathOfCellBeingDragged = nil 427 | 428 | self.reloadData() 429 | 430 | } 431 | 432 | 433 | } 434 | -------------------------------------------------------------------------------- /Classes/KDDragAndDropManager.swift: -------------------------------------------------------------------------------- 1 | /* 2 | * KDDragAndDropManager.swift 3 | * Created by Michael Michailidis on 10/04/2015. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in 13 | * all copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | * THE SOFTWARE. 22 | * 23 | */ 24 | 25 | import UIKit 26 | 27 | public protocol KDDraggable { 28 | func canDragAtPoint(_ point : CGPoint) -> Bool 29 | func representationImageAtPoint(_ point : CGPoint) -> UIView? 30 | func stylingRepresentationView(_ view: UIView) -> UIView? 31 | func dataItemAtPoint(_ point : CGPoint) -> AnyObject? 32 | func dragDataItem(_ item : AnyObject) -> Void 33 | 34 | /* optional */ func startDraggingAtPoint(_ point : CGPoint) -> Void 35 | /* optional */ func stopDragging() -> Void 36 | } 37 | 38 | extension KDDraggable { 39 | public func startDraggingAtPoint(_ point : CGPoint) -> Void {} 40 | public func stopDragging() -> Void {} 41 | } 42 | 43 | 44 | public protocol KDDroppable { 45 | func canDropAtRect(_ rect : CGRect) -> Bool 46 | func willMoveItem(_ item : AnyObject, inRect rect : CGRect) -> Void 47 | func didMoveItem(_ item : AnyObject, inRect rect : CGRect) -> Void 48 | func didMoveOutItem(_ item : AnyObject) -> Void 49 | func dropDataItem(_ item : AnyObject, atRect : CGRect) -> Void 50 | } 51 | 52 | public class KDDragAndDropManager: NSObject, UIGestureRecognizerDelegate { 53 | 54 | fileprivate var canvas : UIView = UIView() 55 | fileprivate var views : [UIView] = [] 56 | fileprivate var longPressGestureRecogniser = UILongPressGestureRecognizer() 57 | 58 | 59 | struct Bundle { 60 | var offset : CGPoint = CGPoint.zero 61 | var sourceDraggableView : UIView 62 | var overDroppableView : UIView? 63 | var representationImageView : UIView 64 | var dataItem : AnyObject 65 | } 66 | var bundle : Bundle? 67 | 68 | public init(canvas : UIView, collectionViews : [UIView]) { 69 | 70 | super.init() 71 | 72 | self.canvas = canvas 73 | 74 | self.longPressGestureRecogniser.delegate = self 75 | self.longPressGestureRecogniser.minimumPressDuration = 0.3 76 | self.longPressGestureRecogniser.addTarget(self, action: #selector(KDDragAndDropManager.updateForLongPress(_:))) 77 | self.canvas.isMultipleTouchEnabled = false 78 | self.canvas.addGestureRecognizer(self.longPressGestureRecogniser) 79 | self.views = collectionViews 80 | } 81 | 82 | public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { 83 | 84 | guard gestureRecognizer.state == .possible else { return false } 85 | 86 | for view in self.views where view is KDDraggable { 87 | 88 | let draggable = view as! KDDraggable 89 | 90 | let touchPointInView = touch.location(in: view) 91 | 92 | guard draggable.canDragAtPoint(touchPointInView) == true else { continue } 93 | 94 | guard var representation = draggable.representationImageAtPoint(touchPointInView) else { continue } 95 | 96 | representation.frame = self.canvas.convert(representation.frame, from: view) 97 | representation.alpha = 0.5 98 | if let decoredView = draggable.stylingRepresentationView(representation) { 99 | representation = decoredView 100 | } 101 | 102 | let pointOnCanvas = touch.location(in: self.canvas) 103 | 104 | let offset = CGPoint(x: pointOnCanvas.x - representation.frame.origin.x, y: pointOnCanvas.y - representation.frame.origin.y) 105 | 106 | if let dataItem: AnyObject = draggable.dataItemAtPoint(touchPointInView) { 107 | 108 | self.bundle = Bundle( 109 | offset: offset, 110 | sourceDraggableView: view, 111 | overDroppableView : view is KDDroppable ? view : nil, 112 | representationImageView: representation, 113 | dataItem : dataItem 114 | ) 115 | 116 | return true 117 | 118 | } 119 | 120 | } 121 | 122 | return false 123 | 124 | } 125 | 126 | @objc public func updateForLongPress(_ recogniser : UILongPressGestureRecognizer) -> Void { 127 | 128 | guard let bundle = self.bundle else { return } 129 | 130 | let pointOnCanvas = recogniser.location(in: self.canvas) 131 | let sourceDraggable : KDDraggable = bundle.sourceDraggableView as! KDDraggable 132 | let pointOnSourceDraggable = recogniser.location(in: bundle.sourceDraggableView) 133 | 134 | switch recogniser.state { 135 | 136 | 137 | case .began : 138 | self.canvas.addSubview(bundle.representationImageView) 139 | sourceDraggable.startDraggingAtPoint(pointOnSourceDraggable) 140 | 141 | case .changed : 142 | 143 | // Update the frame of the representation image 144 | var repImgFrame = bundle.representationImageView.frame 145 | repImgFrame.origin = CGPoint(x: pointOnCanvas.x - bundle.offset.x, y: pointOnCanvas.y - bundle.offset.y); 146 | bundle.representationImageView.frame = repImgFrame 147 | 148 | var overlappingAreaMAX: CGFloat = 0.0 149 | 150 | var mainOverView: UIView? 151 | 152 | for view in self.views where view is KDDraggable { 153 | 154 | let viewFrameOnCanvas = self.convertRectToCanvas(view.frame, fromView: view) 155 | 156 | 157 | /* ┌────────┐ ┌────────────┐ 158 | * │ ┌┼───│Intersection│ 159 | * │ ││ └────────────┘ 160 | * │ ▼───┘│ 161 | * ████████████████│████████│████████████████ 162 | * ████████████████└────────┘████████████████ 163 | * ██████████████████████████████████████████ 164 | */ 165 | 166 | let overlappingAreaCurrent = bundle.representationImageView.frame.intersection(viewFrameOnCanvas).area 167 | 168 | if overlappingAreaCurrent > overlappingAreaMAX { 169 | 170 | overlappingAreaMAX = overlappingAreaCurrent 171 | 172 | mainOverView = view 173 | } 174 | 175 | 176 | } 177 | 178 | 179 | 180 | if let droppable = mainOverView as? KDDroppable { 181 | 182 | let rect = self.canvas.convert(bundle.representationImageView.frame, to: mainOverView) 183 | 184 | if droppable.canDropAtRect(rect) { 185 | 186 | if mainOverView != bundle.overDroppableView { // if it is the first time we are entering 187 | 188 | (bundle.overDroppableView as! KDDroppable).didMoveOutItem(bundle.dataItem) 189 | droppable.willMoveItem(bundle.dataItem, inRect: rect) 190 | } 191 | 192 | // set the view the dragged element is over 193 | self.bundle!.overDroppableView = mainOverView 194 | 195 | droppable.didMoveItem(bundle.dataItem, inRect: rect) 196 | 197 | } 198 | } 199 | 200 | 201 | case .ended : 202 | 203 | if bundle.sourceDraggableView != bundle.overDroppableView { // if we are actually dropping over a new view. 204 | 205 | if let droppable = bundle.overDroppableView as? KDDroppable { 206 | 207 | sourceDraggable.dragDataItem(bundle.dataItem) 208 | 209 | let rect = self.canvas.convert(bundle.representationImageView.frame, to: bundle.overDroppableView) 210 | 211 | droppable.dropDataItem(bundle.dataItem, atRect: rect) 212 | 213 | } 214 | } 215 | 216 | bundle.representationImageView.removeFromSuperview() 217 | sourceDraggable.stopDragging() 218 | 219 | default: 220 | break 221 | 222 | } 223 | 224 | } 225 | 226 | // MARK: Helper Methods 227 | func convertRectToCanvas(_ rect : CGRect, fromView view : UIView) -> CGRect { 228 | 229 | var r = rect 230 | var v = view 231 | 232 | while v != self.canvas { 233 | 234 | guard let sv = v.superview else { break; } 235 | 236 | r.origin.x += sv.frame.origin.x 237 | r.origin.y += sv.frame.origin.y 238 | 239 | v = sv 240 | } 241 | 242 | return r 243 | } 244 | 245 | } 246 | 247 | 248 | extension CGRect: Comparable { 249 | 250 | public var area: CGFloat { 251 | return self.size.width * self.size.height 252 | } 253 | 254 | public static func <=(lhs: CGRect, rhs: CGRect) -> Bool { 255 | return lhs.area <= rhs.area 256 | } 257 | public static func <(lhs: CGRect, rhs: CGRect) -> Bool { 258 | return lhs.area < rhs.area 259 | } 260 | public static func >(lhs: CGRect, rhs: CGRect) -> Bool { 261 | return lhs.area > rhs.area 262 | } 263 | public static func >=(lhs: CGRect, rhs: CGRect) -> Bool { 264 | return lhs.area >= rhs.area 265 | } 266 | } 267 | 268 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | 3 | s.name = "KDDragAndDropCollectionViews" 4 | s.version = "1.5.2" 5 | s.summary = "Dragging & Dropping data across multiple UICollectionViews" 6 | 7 | s.homepage = "https://github.com/mmick66/KDDragAndDropCollectionView" 8 | 9 | s.license = { :type => "MIT", :file => "LICENSE" } 10 | 11 | s.author = "Michael Michailidis" 12 | 13 | s.swift_version = "4.2" 14 | s.platform = :ios, "9.0" 15 | 16 | s.source = { :git => "https://github.com/mmick66/KDDragAndDropCollectionView.git", :tag => s.version } 17 | 18 | s.source_files = "Classes/*.swift" 19 | 20 | end 21 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | BE3603B61AD7DE06005EE44C /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE3603B51AD7DE06005EE44C /* AppDelegate.swift */; }; 11 | BE3603B81AD7DE06005EE44C /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE3603B71AD7DE06005EE44C /* ViewController.swift */; }; 12 | BE3603BB1AD7DE06005EE44C /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BE3603B91AD7DE06005EE44C /* Main.storyboard */; }; 13 | BE3603BD1AD7DE06005EE44C /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BE3603BC1AD7DE06005EE44C /* Images.xcassets */; }; 14 | BE3603C01AD7DE06005EE44C /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = BE3603BE1AD7DE06005EE44C /* LaunchScreen.xib */; }; 15 | BE3603CC1AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE3603CB1AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.swift */; }; 16 | BE3603DB1ADBE397005EE44C /* ColorCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BE3603DA1ADBE397005EE44C /* ColorCell.swift */; }; 17 | C7683C272057DC5100EA7698 /* KDDragAndDropCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7683C242057DC5100EA7698 /* KDDragAndDropCollectionView.swift */; }; 18 | C7683C292057DC5100EA7698 /* KDDragAndDropManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7683C262057DC5100EA7698 /* KDDragAndDropManager.swift */; }; 19 | C7683C2A2057DD0200EA7698 /* KDDragAndDropCollectionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7683C242057DC5100EA7698 /* KDDragAndDropCollectionView.swift */; }; 20 | C7683C2C2057DD0600EA7698 /* KDDragAndDropManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7683C262057DC5100EA7698 /* KDDragAndDropManager.swift */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXContainerItemProxy section */ 24 | BE3603C61AD7DE06005EE44C /* PBXContainerItemProxy */ = { 25 | isa = PBXContainerItemProxy; 26 | containerPortal = BE3603A81AD7DE06005EE44C /* Project object */; 27 | proxyType = 1; 28 | remoteGlobalIDString = BE3603AF1AD7DE06005EE44C; 29 | remoteInfo = KDDragAndDropCollectionViews; 30 | }; 31 | /* End PBXContainerItemProxy section */ 32 | 33 | /* Begin PBXFileReference section */ 34 | BE3603B01AD7DE06005EE44C /* KDDragAndDropCollectionViews.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KDDragAndDropCollectionViews.app; sourceTree = BUILT_PRODUCTS_DIR; }; 35 | BE3603B41AD7DE06005EE44C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 36 | BE3603B51AD7DE06005EE44C /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 37 | BE3603B71AD7DE06005EE44C /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 38 | BE3603BA1AD7DE06005EE44C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 39 | BE3603BC1AD7DE06005EE44C /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; 40 | BE3603BF1AD7DE06005EE44C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; 41 | BE3603C51AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = KDDragAndDropCollectionViewsTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | BE3603CA1AD7DE06005EE44C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 43 | BE3603CB1AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KDDragAndDropCollectionViewsTests.swift; sourceTree = ""; }; 44 | BE3603DA1ADBE397005EE44C /* ColorCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorCell.swift; sourceTree = ""; }; 45 | C7683C242057DC5100EA7698 /* KDDragAndDropCollectionView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KDDragAndDropCollectionView.swift; sourceTree = ""; }; 46 | C7683C262057DC5100EA7698 /* KDDragAndDropManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KDDragAndDropManager.swift; sourceTree = ""; }; 47 | /* End PBXFileReference section */ 48 | 49 | /* Begin PBXFrameworksBuildPhase section */ 50 | BE3603AD1AD7DE06005EE44C /* Frameworks */ = { 51 | isa = PBXFrameworksBuildPhase; 52 | buildActionMask = 2147483647; 53 | files = ( 54 | ); 55 | runOnlyForDeploymentPostprocessing = 0; 56 | }; 57 | BE3603C21AD7DE06005EE44C /* Frameworks */ = { 58 | isa = PBXFrameworksBuildPhase; 59 | buildActionMask = 2147483647; 60 | files = ( 61 | ); 62 | runOnlyForDeploymentPostprocessing = 0; 63 | }; 64 | /* End PBXFrameworksBuildPhase section */ 65 | 66 | /* Begin PBXGroup section */ 67 | BE3603A71AD7DE06005EE44C = { 68 | isa = PBXGroup; 69 | children = ( 70 | BE3603B21AD7DE06005EE44C /* KDDragAndDropCollectionViews */, 71 | BE3603C81AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests */, 72 | BE3603B11AD7DE06005EE44C /* Products */, 73 | ); 74 | sourceTree = ""; 75 | }; 76 | BE3603B11AD7DE06005EE44C /* Products */ = { 77 | isa = PBXGroup; 78 | children = ( 79 | BE3603B01AD7DE06005EE44C /* KDDragAndDropCollectionViews.app */, 80 | BE3603C51AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.xctest */, 81 | ); 82 | name = Products; 83 | sourceTree = ""; 84 | }; 85 | BE3603B21AD7DE06005EE44C /* KDDragAndDropCollectionViews */ = { 86 | isa = PBXGroup; 87 | children = ( 88 | BE3603B51AD7DE06005EE44C /* AppDelegate.swift */, 89 | BE3603B71AD7DE06005EE44C /* ViewController.swift */, 90 | BE3603DA1ADBE397005EE44C /* ColorCell.swift */, 91 | C7683C232057DC5100EA7698 /* Classes */, 92 | BE3603B91AD7DE06005EE44C /* Main.storyboard */, 93 | BE3603BC1AD7DE06005EE44C /* Images.xcassets */, 94 | BE3603BE1AD7DE06005EE44C /* LaunchScreen.xib */, 95 | BE3603B31AD7DE06005EE44C /* Supporting Files */, 96 | ); 97 | path = KDDragAndDropCollectionViews; 98 | sourceTree = ""; 99 | }; 100 | BE3603B31AD7DE06005EE44C /* Supporting Files */ = { 101 | isa = PBXGroup; 102 | children = ( 103 | BE3603B41AD7DE06005EE44C /* Info.plist */, 104 | ); 105 | name = "Supporting Files"; 106 | sourceTree = ""; 107 | }; 108 | BE3603C81AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests */ = { 109 | isa = PBXGroup; 110 | children = ( 111 | BE3603CB1AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.swift */, 112 | BE3603C91AD7DE06005EE44C /* Supporting Files */, 113 | ); 114 | path = KDDragAndDropCollectionViewsTests; 115 | sourceTree = ""; 116 | }; 117 | BE3603C91AD7DE06005EE44C /* Supporting Files */ = { 118 | isa = PBXGroup; 119 | children = ( 120 | BE3603CA1AD7DE06005EE44C /* Info.plist */, 121 | ); 122 | name = "Supporting Files"; 123 | sourceTree = ""; 124 | }; 125 | C7683C232057DC5100EA7698 /* Classes */ = { 126 | isa = PBXGroup; 127 | children = ( 128 | C7683C262057DC5100EA7698 /* KDDragAndDropManager.swift */, 129 | C7683C242057DC5100EA7698 /* KDDragAndDropCollectionView.swift */, 130 | ); 131 | path = Classes; 132 | sourceTree = SOURCE_ROOT; 133 | }; 134 | /* End PBXGroup section */ 135 | 136 | /* Begin PBXNativeTarget section */ 137 | BE3603AF1AD7DE06005EE44C /* KDDragAndDropCollectionViews */ = { 138 | isa = PBXNativeTarget; 139 | buildConfigurationList = BE3603CF1AD7DE06005EE44C /* Build configuration list for PBXNativeTarget "KDDragAndDropCollectionViews" */; 140 | buildPhases = ( 141 | BE3603AC1AD7DE06005EE44C /* Sources */, 142 | BE3603AD1AD7DE06005EE44C /* Frameworks */, 143 | BE3603AE1AD7DE06005EE44C /* Resources */, 144 | ); 145 | buildRules = ( 146 | ); 147 | dependencies = ( 148 | ); 149 | name = KDDragAndDropCollectionViews; 150 | productName = KDDragAndDropCollectionViews; 151 | productReference = BE3603B01AD7DE06005EE44C /* KDDragAndDropCollectionViews.app */; 152 | productType = "com.apple.product-type.application"; 153 | }; 154 | BE3603C41AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests */ = { 155 | isa = PBXNativeTarget; 156 | buildConfigurationList = BE3603D21AD7DE06005EE44C /* Build configuration list for PBXNativeTarget "KDDragAndDropCollectionViewsTests" */; 157 | buildPhases = ( 158 | BE3603C11AD7DE06005EE44C /* Sources */, 159 | BE3603C21AD7DE06005EE44C /* Frameworks */, 160 | BE3603C31AD7DE06005EE44C /* Resources */, 161 | ); 162 | buildRules = ( 163 | ); 164 | dependencies = ( 165 | BE3603C71AD7DE06005EE44C /* PBXTargetDependency */, 166 | ); 167 | name = KDDragAndDropCollectionViewsTests; 168 | productName = KDDragAndDropCollectionViewsTests; 169 | productReference = BE3603C51AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.xctest */; 170 | productType = "com.apple.product-type.bundle.unit-test"; 171 | }; 172 | /* End PBXNativeTarget section */ 173 | 174 | /* Begin PBXProject section */ 175 | BE3603A81AD7DE06005EE44C /* Project object */ = { 176 | isa = PBXProject; 177 | attributes = { 178 | LastSwiftMigration = 0700; 179 | LastSwiftUpdateCheck = 0700; 180 | LastUpgradeCheck = 0940; 181 | ORGANIZATIONNAME = Karmadust; 182 | TargetAttributes = { 183 | BE3603AF1AD7DE06005EE44C = { 184 | CreatedOnToolsVersion = 6.2; 185 | LastSwiftMigration = 0900; 186 | ProvisioningStyle = Automatic; 187 | }; 188 | BE3603C41AD7DE06005EE44C = { 189 | CreatedOnToolsVersion = 6.2; 190 | LastSwiftMigration = 0900; 191 | TestTargetID = BE3603AF1AD7DE06005EE44C; 192 | }; 193 | }; 194 | }; 195 | buildConfigurationList = BE3603AB1AD7DE06005EE44C /* Build configuration list for PBXProject "KDDragAndDropCollectionViews" */; 196 | compatibilityVersion = "Xcode 3.2"; 197 | developmentRegion = English; 198 | hasScannedForEncodings = 0; 199 | knownRegions = ( 200 | en, 201 | Base, 202 | ); 203 | mainGroup = BE3603A71AD7DE06005EE44C; 204 | productRefGroup = BE3603B11AD7DE06005EE44C /* Products */; 205 | projectDirPath = ""; 206 | projectRoot = ""; 207 | targets = ( 208 | BE3603AF1AD7DE06005EE44C /* KDDragAndDropCollectionViews */, 209 | BE3603C41AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests */, 210 | ); 211 | }; 212 | /* End PBXProject section */ 213 | 214 | /* Begin PBXResourcesBuildPhase section */ 215 | BE3603AE1AD7DE06005EE44C /* Resources */ = { 216 | isa = PBXResourcesBuildPhase; 217 | buildActionMask = 2147483647; 218 | files = ( 219 | BE3603BB1AD7DE06005EE44C /* Main.storyboard in Resources */, 220 | BE3603C01AD7DE06005EE44C /* LaunchScreen.xib in Resources */, 221 | BE3603BD1AD7DE06005EE44C /* Images.xcassets in Resources */, 222 | ); 223 | runOnlyForDeploymentPostprocessing = 0; 224 | }; 225 | BE3603C31AD7DE06005EE44C /* Resources */ = { 226 | isa = PBXResourcesBuildPhase; 227 | buildActionMask = 2147483647; 228 | files = ( 229 | ); 230 | runOnlyForDeploymentPostprocessing = 0; 231 | }; 232 | /* End PBXResourcesBuildPhase section */ 233 | 234 | /* Begin PBXSourcesBuildPhase section */ 235 | BE3603AC1AD7DE06005EE44C /* Sources */ = { 236 | isa = PBXSourcesBuildPhase; 237 | buildActionMask = 2147483647; 238 | files = ( 239 | C7683C272057DC5100EA7698 /* KDDragAndDropCollectionView.swift in Sources */, 240 | BE3603B81AD7DE06005EE44C /* ViewController.swift in Sources */, 241 | C7683C292057DC5100EA7698 /* KDDragAndDropManager.swift in Sources */, 242 | BE3603DB1ADBE397005EE44C /* ColorCell.swift in Sources */, 243 | BE3603B61AD7DE06005EE44C /* AppDelegate.swift in Sources */, 244 | ); 245 | runOnlyForDeploymentPostprocessing = 0; 246 | }; 247 | BE3603C11AD7DE06005EE44C /* Sources */ = { 248 | isa = PBXSourcesBuildPhase; 249 | buildActionMask = 2147483647; 250 | files = ( 251 | C7683C2A2057DD0200EA7698 /* KDDragAndDropCollectionView.swift in Sources */, 252 | C7683C2C2057DD0600EA7698 /* KDDragAndDropManager.swift in Sources */, 253 | BE3603CC1AD7DE06005EE44C /* KDDragAndDropCollectionViewsTests.swift in Sources */, 254 | ); 255 | runOnlyForDeploymentPostprocessing = 0; 256 | }; 257 | /* End PBXSourcesBuildPhase section */ 258 | 259 | /* Begin PBXTargetDependency section */ 260 | BE3603C71AD7DE06005EE44C /* PBXTargetDependency */ = { 261 | isa = PBXTargetDependency; 262 | target = BE3603AF1AD7DE06005EE44C /* KDDragAndDropCollectionViews */; 263 | targetProxy = BE3603C61AD7DE06005EE44C /* PBXContainerItemProxy */; 264 | }; 265 | /* End PBXTargetDependency section */ 266 | 267 | /* Begin PBXVariantGroup section */ 268 | BE3603B91AD7DE06005EE44C /* Main.storyboard */ = { 269 | isa = PBXVariantGroup; 270 | children = ( 271 | BE3603BA1AD7DE06005EE44C /* Base */, 272 | ); 273 | name = Main.storyboard; 274 | sourceTree = ""; 275 | }; 276 | BE3603BE1AD7DE06005EE44C /* LaunchScreen.xib */ = { 277 | isa = PBXVariantGroup; 278 | children = ( 279 | BE3603BF1AD7DE06005EE44C /* Base */, 280 | ); 281 | name = LaunchScreen.xib; 282 | sourceTree = ""; 283 | }; 284 | /* End PBXVariantGroup section */ 285 | 286 | /* Begin XCBuildConfiguration section */ 287 | BE3603CD1AD7DE06005EE44C /* Debug */ = { 288 | isa = XCBuildConfiguration; 289 | buildSettings = { 290 | ALWAYS_SEARCH_USER_PATHS = NO; 291 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 292 | CLANG_CXX_LIBRARY = "libc++"; 293 | CLANG_ENABLE_MODULES = YES; 294 | CLANG_ENABLE_OBJC_ARC = YES; 295 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 296 | CLANG_WARN_BOOL_CONVERSION = YES; 297 | CLANG_WARN_COMMA = YES; 298 | CLANG_WARN_CONSTANT_CONVERSION = YES; 299 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 300 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 301 | CLANG_WARN_EMPTY_BODY = YES; 302 | CLANG_WARN_ENUM_CONVERSION = YES; 303 | CLANG_WARN_INFINITE_RECURSION = YES; 304 | CLANG_WARN_INT_CONVERSION = YES; 305 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 306 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 307 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 308 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 309 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 310 | CLANG_WARN_STRICT_PROTOTYPES = YES; 311 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 312 | CLANG_WARN_UNREACHABLE_CODE = YES; 313 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 314 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 315 | COPY_PHASE_STRIP = NO; 316 | ENABLE_STRICT_OBJC_MSGSEND = YES; 317 | ENABLE_TESTABILITY = YES; 318 | GCC_C_LANGUAGE_STANDARD = gnu99; 319 | GCC_DYNAMIC_NO_PIC = NO; 320 | GCC_NO_COMMON_BLOCKS = YES; 321 | GCC_OPTIMIZATION_LEVEL = 0; 322 | GCC_PREPROCESSOR_DEFINITIONS = ( 323 | "DEBUG=1", 324 | "$(inherited)", 325 | ); 326 | GCC_SYMBOLS_PRIVATE_EXTERN = NO; 327 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 328 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 329 | GCC_WARN_UNDECLARED_SELECTOR = YES; 330 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 331 | GCC_WARN_UNUSED_FUNCTION = YES; 332 | GCC_WARN_UNUSED_VARIABLE = YES; 333 | IPHONEOS_DEPLOYMENT_TARGET = 8.2; 334 | MTL_ENABLE_DEBUG_INFO = YES; 335 | ONLY_ACTIVE_ARCH = YES; 336 | SDKROOT = iphoneos; 337 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 338 | SWIFT_VERSION = 4.2; 339 | }; 340 | name = Debug; 341 | }; 342 | BE3603CE1AD7DE06005EE44C /* Release */ = { 343 | isa = XCBuildConfiguration; 344 | buildSettings = { 345 | ALWAYS_SEARCH_USER_PATHS = NO; 346 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 347 | CLANG_CXX_LIBRARY = "libc++"; 348 | CLANG_ENABLE_MODULES = YES; 349 | CLANG_ENABLE_OBJC_ARC = YES; 350 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 351 | CLANG_WARN_BOOL_CONVERSION = YES; 352 | CLANG_WARN_COMMA = YES; 353 | CLANG_WARN_CONSTANT_CONVERSION = YES; 354 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 355 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 356 | CLANG_WARN_EMPTY_BODY = YES; 357 | CLANG_WARN_ENUM_CONVERSION = YES; 358 | CLANG_WARN_INFINITE_RECURSION = YES; 359 | CLANG_WARN_INT_CONVERSION = YES; 360 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 361 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 362 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 363 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 364 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 365 | CLANG_WARN_STRICT_PROTOTYPES = YES; 366 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 367 | CLANG_WARN_UNREACHABLE_CODE = YES; 368 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 369 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 370 | COPY_PHASE_STRIP = NO; 371 | ENABLE_NS_ASSERTIONS = NO; 372 | ENABLE_STRICT_OBJC_MSGSEND = YES; 373 | GCC_C_LANGUAGE_STANDARD = gnu99; 374 | GCC_NO_COMMON_BLOCKS = YES; 375 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 376 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 377 | GCC_WARN_UNDECLARED_SELECTOR = YES; 378 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 379 | GCC_WARN_UNUSED_FUNCTION = YES; 380 | GCC_WARN_UNUSED_VARIABLE = YES; 381 | IPHONEOS_DEPLOYMENT_TARGET = 8.2; 382 | MTL_ENABLE_DEBUG_INFO = NO; 383 | SDKROOT = iphoneos; 384 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 385 | SWIFT_VERSION = 4.2; 386 | VALIDATE_PRODUCT = YES; 387 | }; 388 | name = Release; 389 | }; 390 | BE3603D01AD7DE06005EE44C /* Debug */ = { 391 | isa = XCBuildConfiguration; 392 | buildSettings = { 393 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 394 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 395 | CODE_SIGN_STYLE = Automatic; 396 | DEVELOPMENT_TEAM = ""; 397 | INFOPLIST_FILE = KDDragAndDropCollectionViews/Info.plist; 398 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 399 | PRODUCT_BUNDLE_IDENTIFIER = "KD.$(PRODUCT_NAME:rfc1034identifier)"; 400 | PRODUCT_NAME = "$(TARGET_NAME)"; 401 | PROVISIONING_PROFILE_SPECIFIER = ""; 402 | SWIFT_SWIFT3_OBJC_INFERENCE = Default; 403 | SWIFT_VERSION = 4.2; 404 | }; 405 | name = Debug; 406 | }; 407 | BE3603D11AD7DE06005EE44C /* Release */ = { 408 | isa = XCBuildConfiguration; 409 | buildSettings = { 410 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 411 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 412 | CODE_SIGN_STYLE = Automatic; 413 | DEVELOPMENT_TEAM = ""; 414 | INFOPLIST_FILE = KDDragAndDropCollectionViews/Info.plist; 415 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 416 | PRODUCT_BUNDLE_IDENTIFIER = "KD.$(PRODUCT_NAME:rfc1034identifier)"; 417 | PRODUCT_NAME = "$(TARGET_NAME)"; 418 | PROVISIONING_PROFILE_SPECIFIER = ""; 419 | SWIFT_SWIFT3_OBJC_INFERENCE = Default; 420 | SWIFT_VERSION = 4.2; 421 | }; 422 | name = Release; 423 | }; 424 | BE3603D31AD7DE06005EE44C /* Debug */ = { 425 | isa = XCBuildConfiguration; 426 | buildSettings = { 427 | BUNDLE_LOADER = "$(TEST_HOST)"; 428 | DEVELOPMENT_TEAM = ""; 429 | FRAMEWORK_SEARCH_PATHS = ( 430 | "$(SDKROOT)/Developer/Library/Frameworks", 431 | "$(inherited)", 432 | ); 433 | GCC_PREPROCESSOR_DEFINITIONS = ( 434 | "DEBUG=1", 435 | "$(inherited)", 436 | ); 437 | INFOPLIST_FILE = KDDragAndDropCollectionViewsTests/Info.plist; 438 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 439 | PRODUCT_BUNDLE_IDENTIFIER = "KD.$(PRODUCT_NAME:rfc1034identifier)"; 440 | PRODUCT_NAME = "$(TARGET_NAME)"; 441 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 442 | SWIFT_VERSION = 4.2; 443 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/KDDragAndDropCollectionViews.app/KDDragAndDropCollectionViews"; 444 | }; 445 | name = Debug; 446 | }; 447 | BE3603D41AD7DE06005EE44C /* Release */ = { 448 | isa = XCBuildConfiguration; 449 | buildSettings = { 450 | BUNDLE_LOADER = "$(TEST_HOST)"; 451 | DEVELOPMENT_TEAM = ""; 452 | FRAMEWORK_SEARCH_PATHS = ( 453 | "$(SDKROOT)/Developer/Library/Frameworks", 454 | "$(inherited)", 455 | ); 456 | INFOPLIST_FILE = KDDragAndDropCollectionViewsTests/Info.plist; 457 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 458 | PRODUCT_BUNDLE_IDENTIFIER = "KD.$(PRODUCT_NAME:rfc1034identifier)"; 459 | PRODUCT_NAME = "$(TARGET_NAME)"; 460 | SWIFT_SWIFT3_OBJC_INFERENCE = On; 461 | SWIFT_VERSION = 4.2; 462 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/KDDragAndDropCollectionViews.app/KDDragAndDropCollectionViews"; 463 | }; 464 | name = Release; 465 | }; 466 | /* End XCBuildConfiguration section */ 467 | 468 | /* Begin XCConfigurationList section */ 469 | BE3603AB1AD7DE06005EE44C /* Build configuration list for PBXProject "KDDragAndDropCollectionViews" */ = { 470 | isa = XCConfigurationList; 471 | buildConfigurations = ( 472 | BE3603CD1AD7DE06005EE44C /* Debug */, 473 | BE3603CE1AD7DE06005EE44C /* Release */, 474 | ); 475 | defaultConfigurationIsVisible = 0; 476 | defaultConfigurationName = Release; 477 | }; 478 | BE3603CF1AD7DE06005EE44C /* Build configuration list for PBXNativeTarget "KDDragAndDropCollectionViews" */ = { 479 | isa = XCConfigurationList; 480 | buildConfigurations = ( 481 | BE3603D01AD7DE06005EE44C /* Debug */, 482 | BE3603D11AD7DE06005EE44C /* Release */, 483 | ); 484 | defaultConfigurationIsVisible = 0; 485 | defaultConfigurationName = Release; 486 | }; 487 | BE3603D21AD7DE06005EE44C /* Build configuration list for PBXNativeTarget "KDDragAndDropCollectionViewsTests" */ = { 488 | isa = XCConfigurationList; 489 | buildConfigurations = ( 490 | BE3603D31AD7DE06005EE44C /* Debug */, 491 | BE3603D41AD7DE06005EE44C /* Release */, 492 | ); 493 | defaultConfigurationIsVisible = 0; 494 | defaultConfigurationName = Release; 495 | }; 496 | /* End XCConfigurationList section */ 497 | }; 498 | rootObject = BE3603A81AD7DE06005EE44C /* Project object */; 499 | } 500 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.xcodeproj/project.xcworkspace/xcuserdata/michaelmichailidis.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmick66/KDDragAndDropCollectionView/d5d853e935a877eef70d8b2a54b0e48c85948278/KDDragAndDropCollectionViews.xcodeproj/project.xcworkspace/xcuserdata/michaelmichailidis.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.xcodeproj/xcuserdata/michaelmichailidis.xcuserdatad/xcschemes/KDDragAndDropCollectionViews.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 53 | 54 | 64 | 66 | 72 | 73 | 74 | 75 | 76 | 77 | 83 | 85 | 91 | 92 | 93 | 94 | 96 | 97 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews.xcodeproj/xcuserdata/michaelmichailidis.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | KDDragAndDropCollectionViews.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | BE3603AF1AD7DE06005EE44C 16 | 17 | primary 18 | 19 | 20 | BE3603C41AD7DE06005EE44C 21 | 22 | primary 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | /* 2 | * AppDelegate.swift 3 | * Created by Michael Michailidis on 10/04/2015. 4 | * http://blog.karmadust.com/ 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | import UIKit 27 | 28 | @UIApplicationMain 29 | class AppDelegate: UIResponder, UIApplicationDelegate { 30 | 31 | var window: UIWindow? 32 | 33 | 34 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 35 | // Override point for customization after application launch. 36 | return true 37 | } 38 | 39 | func applicationWillResignActive(_ application: UIApplication) { 40 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 41 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 42 | } 43 | 44 | func applicationDidEnterBackground(_ application: UIApplication) { 45 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 46 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 47 | } 48 | 49 | func applicationWillEnterForeground(_ application: UIApplication) { 50 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 51 | } 52 | 53 | func applicationDidBecomeActive(_ application: UIApplication) { 54 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 55 | } 56 | 57 | func applicationWillTerminate(_ application: UIApplication) { 58 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 59 | } 60 | 61 | 62 | } 63 | 64 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/Base.lproj/LaunchScreen.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | HelveticaNeue 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/ColorCell.swift: -------------------------------------------------------------------------------- 1 | /* 2 | * ColorCell.swift 3 | * Created by Michael Michailidis on 13/04/2015. 4 | * http://blog.karmadust.com/ 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | import UIKit 27 | 28 | class ColorCell: UICollectionViewCell { 29 | 30 | @IBOutlet weak var label: UILabel! 31 | 32 | } 33 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/Images.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ios-marketing", 45 | "size" : "1024x1024", 46 | "scale" : "1x" 47 | } 48 | ], 49 | "info" : { 50 | "version" : 1, 51 | "author" : "xcode" 52 | } 53 | } -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViews/ViewController.swift: -------------------------------------------------------------------------------- 1 | /* 2 | * ViewController.swift 3 | * Created by Michael Michailidis on 10/04/2015. 4 | * http://blog.karmadust.com/ 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | import UIKit 27 | 28 | class DataItem : Equatable { 29 | 30 | var indexes: String 31 | var colour: UIColor 32 | init(_ indexes: String, _ colour: UIColor = UIColor.clear) { 33 | self.indexes = indexes 34 | self.colour = colour 35 | } 36 | 37 | static func ==(lhs: DataItem, rhs: DataItem) -> Bool { 38 | return lhs.indexes == rhs.indexes && lhs.colour == rhs.colour 39 | } 40 | } 41 | 42 | extension UIColor { 43 | static var kdBrown:UIColor { 44 | return UIColor(red: 177.0/255.0, green: 88.0/255.0, blue: 39.0/255.0, alpha: 1.0) 45 | } 46 | static var kdGreen:UIColor { 47 | return UIColor(red: 138.0/255.0, green: 149.0/255.0, blue: 86.0/255.0, alpha: 1.0) 48 | } 49 | static var kdBlue:UIColor { 50 | return UIColor(red: 53.0/255.0, green: 102.0/255.0, blue: 149.0/255.0, alpha: 1.0) 51 | } 52 | } 53 | 54 | let colours = [UIColor.kdBrown, UIColor.kdGreen, UIColor.kdBlue] 55 | 56 | class ViewController: UIViewController, KDDragAndDropCollectionViewDataSource { 57 | 58 | @IBOutlet weak var firstCollectionView: KDDragAndDropCollectionView! 59 | @IBOutlet weak var secondCollectionView: KDDragAndDropCollectionView! 60 | @IBOutlet weak var thirdCollectionView: KDDragAndDropCollectionView! 61 | 62 | var data : [[DataItem]] = [[DataItem]]() 63 | 64 | var dragAndDropManager : KDDragAndDropManager? 65 | 66 | override func viewDidLoad() { 67 | 68 | super.viewDidLoad() 69 | 70 | // generate some mock data (change in real world project) 71 | self.data = (0...2).map({ i in (0...20).map({ j in DataItem("\(String(i)):\(String(j))", colours[i])})}) 72 | 73 | self.dragAndDropManager = KDDragAndDropManager( 74 | canvas: self.view, 75 | collectionViews: [firstCollectionView, secondCollectionView, thirdCollectionView] 76 | ) 77 | 78 | } 79 | 80 | 81 | // MARK : UICollectionViewDataSource 82 | 83 | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 84 | return data[collectionView.tag].count 85 | } 86 | 87 | // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: 88 | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 89 | 90 | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ColorCell 91 | 92 | let dataItem = data[collectionView.tag][indexPath.item] 93 | 94 | cell.label.text = String(indexPath.item) + "\n\n" + dataItem.indexes 95 | cell.backgroundColor = dataItem.colour 96 | 97 | cell.isHidden = false 98 | 99 | if let kdCollectionView = collectionView as? KDDragAndDropCollectionView { 100 | 101 | if let draggingPathOfCellBeingDragged = kdCollectionView.draggingPathOfCellBeingDragged { 102 | 103 | if draggingPathOfCellBeingDragged.item == indexPath.item { 104 | 105 | cell.isHidden = true 106 | 107 | } 108 | } 109 | } 110 | 111 | return cell 112 | } 113 | 114 | // MARK : KDDragAndDropCollectionViewDataSource 115 | 116 | func collectionView(_ collectionView: UICollectionView, dataItemForIndexPath indexPath: IndexPath) -> AnyObject { 117 | return data[collectionView.tag][indexPath.item] 118 | } 119 | func collectionView(_ collectionView: UICollectionView, insertDataItem dataItem : AnyObject, atIndexPath indexPath: IndexPath) -> Void { 120 | 121 | if let di = dataItem as? DataItem { 122 | data[collectionView.tag].insert(di, at: indexPath.item) 123 | } 124 | 125 | 126 | } 127 | func collectionView(_ collectionView: UICollectionView, deleteDataItemAtIndexPath indexPath : IndexPath) -> Void { 128 | data[collectionView.tag].remove(at: indexPath.item) 129 | } 130 | 131 | func collectionView(_ collectionView: UICollectionView, moveDataItemFromIndexPath from: IndexPath, toIndexPath to : IndexPath) -> Void { 132 | 133 | let fromDataItem: DataItem = data[collectionView.tag][from.item] 134 | data[collectionView.tag].remove(at: from.item) 135 | data[collectionView.tag].insert(fromDataItem, at: to.item) 136 | 137 | } 138 | 139 | func collectionView(_ collectionView: UICollectionView, indexPathForDataItem dataItem: AnyObject) -> IndexPath? { 140 | 141 | guard let candidate = dataItem as? DataItem else { return nil } 142 | 143 | for (i,item) in data[collectionView.tag].enumerated() { 144 | if candidate != item { continue } 145 | return IndexPath(item: i, section: 0) 146 | } 147 | 148 | return nil 149 | 150 | } 151 | 152 | 153 | } 154 | 155 | 156 | 157 | 158 | 159 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViewsTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /KDDragAndDropCollectionViewsTests/KDDragAndDropCollectionViewsTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // KDDragAndDropCollectionViewsTests.swift 3 | // KDDragAndDropCollectionViewsTests 4 | // 5 | // Created by Michael Michailidis on 10/04/2015. 6 | // Copyright (c) 2015 Karmadust. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import XCTest 11 | 12 | class KDDragAndDropCollectionViewsTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | XCTAssert(true, "Pass") 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measure() { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Karmadust Ltd. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Drag and Drop Collection Views 2 | 3 | Written for Swift 4.0, it is an implementation of Dragging and Dropping data across multiple UICollectionViews. 4 | 5 | ![Drag and Drop Illustration](https://github.com/mmick66/KDDragAndDropCollectionView/blob/master/Resources/header.png?raw=true "Drag and Drop") 6 | 7 | Try it on [Appetize.io!](https://appetize.io/embed/exaf5fdj5auryhu174ta69t1gm?device=iphone5s&scale=75&orientation=portrait&osVersion=9.3) 8 | 9 | [![Language](https://img.shields.io/badge/Swift-4.0-orange.svg?style=flat)](https://swift.org) 10 | [![Licence](https://img.shields.io/dub/l/vibe-d.svg?maxAge=2592000)](https://opensource.org/licenses/MIT) 11 | [![CocoaPods](https://img.shields.io/cocoapods/v/KDCalendar.svg?style=flat)](https://cocoapods.org/pods/KDDragAndDropCollectionViews) 12 | [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/vsouza/awesome-ios) 13 | 14 | ## Requirements 15 | 16 | * iOS 8.0+ 17 | * XCode 9.0+ 18 | * Swift 4.0 + 19 | 20 | ## Installation 21 | 22 | #### Cocoa Pods 23 | ``` 24 | pod 'KDDragAndDropCollectionViews', '~> 1.5.2' 25 | ``` 26 | #### Manual 27 | 28 | Add the files in `Classes/` to your project. 29 | 30 | ## Quick Guide 31 | 32 | Make the UICollectionView of interest a `KDDragAndDropCollectionView` 33 | 34 | ![XCode Interface Builder Screen](https://github.com/mmick66/KDDragAndDropCollectionView/blob/master/Resources/Screenshot.Installation.png?raw=true) 35 | 36 | Then set a class as dataSource implementing the `KDDragAndDropCollectionViewDataSource` protocol. 37 | 38 | ```Swift 39 | class ViewController: UIViewController, KDDragAndDropCollectionViewDataSource { 40 | 41 | @IBOutlet weak var firstCollectionView: KDDragAndDropCollectionView! 42 | @IBOutlet weak var secondCollectionView: KDDragAndDropCollectionView! 43 | @IBOutlet weak var thirdCollectionView: KDDragAndDropCollectionView! 44 | 45 | var data : [[DataItem]] = [[DataItem]]() // just for this example 46 | 47 | var dragAndDropManager : KDDragAndDropManager? 48 | 49 | override func viewDidLoad() { 50 | let all = [firstCollectionView, secondCollectionView, thirdCollectionView] 51 | self.dragAndDropManager = KDDragAndDropManager(canvas: self.view, collectionViews: all) 52 | } 53 | } 54 | ``` 55 | 56 | The only responsibility of the user code is to manage the data that the collection view cells are representing. The data source of the collection views must implement the `KDDragAndDropCollectionViewDataSource` protocol. 57 | 58 | In the example we have 3 UICollectionViews distinguishable by their tags (bad practice, I know... but it's only an example ;-) and a data array holding 3 arrays respectively. In a case like this, an implementation of the above could be: 59 | 60 | ```Swift 61 | func collectionView(collectionView: UICollectionView, dataItemForIndexPath indexPath: NSIndexPath) -> AnyObject { 62 | return data[collectionView.tag][indexPath.item] 63 | } 64 | 65 | func collectionView(collectionView: UICollectionView, insertDataItem dataItem : AnyObject, atIndexPath indexPath: NSIndexPath) -> Void { 66 | if let di = dataItem as? DataItem { 67 | data[collectionView.tag].insert(di, atIndex: indexPath.item) 68 | } 69 | } 70 | 71 | func collectionView(collectionView: UICollectionView, deleteDataItemAtIndexPath indexPath : NSIndexPath) -> Void { 72 | data[collectionView.tag].removeAtIndex(indexPath.item) 73 | } 74 | 75 | func collectionView(collectionView: UICollectionView, moveDataItemFromIndexPath from: NSIndexPath, toIndexPath to : NSIndexPath) -> Void { 76 | let fromDataItem: DataItem = data[collectionView.tag][from.item] 77 | data[collectionView.tag].removeAtIndex(from.item) 78 | data[collectionView.tag].insert(fromDataItem, atIndex: to.item) 79 | } 80 | 81 | func collectionView(_ collectionView: UICollectionView, indexPathForDataItem dataItem: AnyObject) -> IndexPath? { 82 | 83 | guard let candidate = dataItem as? DataItem else { return nil } 84 | 85 | for (i,item) in data[collectionView.tag].enumerated() { 86 | if candidate != item { continue } 87 | return IndexPath(item: i, section: 0) 88 | } 89 | return nil 90 | } 91 | ``` 92 | 93 | ## Advanced Use 94 | 95 | #### Prevent specific Items from being Dragged and/or Dropped 96 | 97 | For a finer tuning on what items are draggable and which ones are not we can implement the following function from the `KDDragAndDropCollectionViewDataSource` protocol 98 | 99 | ```Swift 100 | func collectionView(_ collectionView: UICollectionView, cellIsDraggableAtIndexPath indexPath: IndexPath) -> Bool { 101 | return indexPath.row % 2 == 0 102 | } 103 | ``` 104 | 105 | 106 | #### Data Items and Equatable 107 | 108 | In the example code included in this project, I have created a `DataItem` class to represent the data displayed by the collection view. 109 | 110 | ```Swift 111 | class DataItem : Equatable { 112 | var indexes: String 113 | var colour: UIColor 114 | init(indexes: String, colour: UIColor = UIColor.clear) { 115 | self.indexes = indexes 116 | self.colour = colour 117 | } 118 | static func ==(lhs: DataItem, rhs: DataItem) -> Bool { 119 | return lhs.indexes == rhs.indexes && lhs.colour == rhs.colour 120 | } 121 | } 122 | ``` 123 | 124 | In the course of development you will be making your own types that must comform to the `Equatable` protocol as above. Each data item must be **uniquely idenfyiable** so be careful when creating cells that can have duplicate display values as for example a ["Scrabble"](https://en.wikipedia.org/wiki/Scrabble) type game where the same letter appears more than once. In cases like these, a simple identifier will do to implement the equality. 125 | -------------------------------------------------------------------------------- /Resources/Screenshot.Installation.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmick66/KDDragAndDropCollectionView/d5d853e935a877eef70d8b2a54b0e48c85948278/Resources/Screenshot.Installation.png -------------------------------------------------------------------------------- /Resources/header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mmick66/KDDragAndDropCollectionView/d5d853e935a877eef70d8b2a54b0e48c85948278/Resources/header.png --------------------------------------------------------------------------------