DragIt 0.1.0

DragIt 0.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布上次发布2016年8月
SPM支持 SPM

Cantallops 维护。



DragIt 0.1.0

  • Alberto Cantallops

DragIt

示例

要运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install

要求

  • iOS 8.0+
  • Xcode 7.3+

安装

DragIt 通过 CocoaPods 可用。要安装它,只需将以下行添加到您的 Podfile 中:

pod "DragIt"

使用

使用 DragAndDropTableViewDragAndDropTableViewController

class MyController {
  var dragItTable: DragAndDropTableView

  // DO YOUR STUFF
}

或者您可以继承它

class MyController: DragAndDropTableView {

  // DO YOUR STUFF
}

但如果您不使用我的类,您可以创建自己的类并添加类似的东西

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)
    }
  }
}

代理

DragAndDropTableDelegate 有四个方法

@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
}

只有第一个是必需的.

函数 move()

该函数在移动单元格时调用,指示单元格从哪个位置移动到哪个位置。

如您在 示例 中所见,您可以使用这种方式移动您的项目

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)
}

函数 canMoveCell()

func canMoveCell(atIndex: NSIndexPath) -> Bool

返回一个布尔值,以指示在 atIndex 是否可以移动单元格。

默认为 true

函数 canMove()

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

返回一个布尔值,指示单元格是否可以从一个部分(fromSection)移动到另一个部分(toSection)。

默认为 false

函数 backgroundColor()

func backgroundColor(forCellAtIndex: NSIndexPath) -> UIColor

用于指示拖动单元格时单元格的背景颜色。

默认使用所选颜色为单元格的背景色。

下一步

  • [ ] 在代码中添加注释
  • [ ] 添加测试
  • [ ] 添加演示gif
  • [ ] 改进动画滚动

许可证

DragIt 由 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。