DragIt 0.1.0

DragIt 0.1.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2016
SPMSupports SPM

Maintained by Cantallops.



DragIt 0.1.0

  • By
  • Alberto Cantallops

DragIt

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+
  • Xcode 7.3+

Installation

DragIt is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "DragIt"

Usage

Use DragAndDropTableView or DragAndDropTableViewController

class MyController {
  var dragItTable: DragAndDropTableView

  // DO YOUR STUFF
}

Or you can subclass it

class MyController: DragAndDropTableView {

  // DO YOUR STUFF
}

But if you do not use my classes you can create your own class and add something like:

private var dragAndDropFactory: DragAndDropTableFactory?
public var dragAndDropDelegate: DragAndDropTableDelegate? {
  get {
    return dragAndDropFactory?.delegate
  }
  set {
    if let dAD = dragAndDropFactory {
      dAD.delegate = newValue
    } else {
      let table = self.tableView
      dragAndDropFactory = DragAndDropTableFactory(tableView: table, dragAndDropDelegate: newValue)
    }
  }
}

Delegate

The DragAndDropTableDelegate has four methods:

@objc public protocol DragAndDropTableDelegate {
  func move(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath)
  optional func canMoveCell(atIndex: NSIndexPath) -> Bool
  optional func canMove(fromSection: Int, toSection: Int) -> Bool
  optional func backgroundColor(forCellAtIndex: NSIndexPath) -> UIColor
}

Only the first one is required.

Function move()

That function it’s called when move the cell and indicates from which position and to which position the cell had been moved.

As you can see in the Example you can move your item using in this way:

func move(fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
  let fS = fromIndexPath.section
  let fR = fromIndexPath.row
  let tS = toIndexPath.section
  let tR = toIndexPath.row

  let color = items[fS][fR]
  items[fS].removeAtIndex(fR)
  items[tS].insert(color, atIndex: tR)
}

Function canMoveCell()

func canMoveCell(atIndex: NSIndexPath) -> Bool

Return a boolean to indicate if the cell at atIndex can be moved.

true by default

Function canMove()

func canMove(fromSection: Int, toSection: Int) -> Bool

Return a boolean to indicate if the cell can move from one section (fromSection) to another (toSection).

false by default

Function backgroundColor()

func backgroundColor(forCellAtIndex: NSIndexPath) -> UIColor

Used to indicate which color has to be the background of the cell when drag it.

By default uses a selected color for the cell.

Next

  • [ ] Add comments in code
  • [ ] Add tests
  • [ ] Add demo gif
  • [ ] Improve animation scroll

License

DragIt is available under the MIT license. See the LICENSE file for more info.