DragDropiOS
DragDropiOS 是一个 iOS 上拖放管理的框架。它支持一个或多个扩展自 UIView 的类之间的拖放操作。该库包含了实现了拖放管理功能的 UICollectionView 和 UITableView。
示例
示例展示了 UICollectionView、UITableView 和 UIView 之间的拖放演示。
为了运行示例项目,克隆仓库,并从 Example 目录中首先运行 pod install
。
要求
- iOS 8.0+
- Xcode 10
- Swift 4.2
安装
Cocoapods
DragDropiOS 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod "DragDropiOS"
Carthage
如果您使用 Carthage,您可以通过将其添加到 Cartfile 来添加对 DragDropiOS 的依赖
github "KevinZhouRafael/DragDropiOS"
介绍
可拖动
@objc public protocol Draggable:NSObjectProtocol {
@objc optional func touchBeginAtPoint(_ point : CGPoint) -> Void
func canDragAtPoint(_ point : CGPoint) -> Bool
func representationImageAtPoint(_ point : CGPoint) -> UIView?
func dragInfoAtPoint(_ point : CGPoint) -> AnyObject?
func dragComplete(_ dragInfo:AnyObject,dropInfo : AnyObject?) -> Void
@objc optional func stopDragging() -> Void
}
可放置
@objc public protocol Droppable:NSObjectProtocol {
func canDropWithDragInfo(_ dragInfo:AnyObject, inRect rect : CGRect) -> Bool
func dropOverInfoInRect(_ rect:CGRect) -> AnyObject?
@objc optional func dropOutside(_ dragInfo:AnyObject, inRect rect:CGRect)->Void
func dropComplete(_ dragInfo : AnyObject,dropInfo:AnyObject?, atRect : CGRect) -> Void
@objc optional func checkFroEdgesAndScroll(_ item : AnyObject, inRect rect : CGRect) -> Void
@objc optional func stopDropping() -> Void
}
DragDropTableView 和 DragDropTableViewDelegate
@objc public protocol DragDropTableViewDelegate : NSObjectProtocol {
@objc optional func tableView(_ tableView: UITableView, indexPathForDragInfo dragInfo: AnyObject) -> IndexPath?
func tableView(_ tableView: UITableView, dragInfoForIndexPath indexPath: IndexPath) -> AnyObject
@objc optional func tableView(_ tableView: UITableView, representationImageAtIndexPath indexPath: IndexPath) -> UIImage?
//drag
func tableView(_ tableView: UITableView, touchBeginAtIndexPath indexPath:IndexPath) -> Void
func tableView(_ tableView: UITableView, canDragAtIndexPath indexPath: IndexPath) -> Bool
func tableView(_ tableView: UITableView, dragCompleteWithDragInfo dragInfo:AnyObject, atDragIndexPath dragIndexPath: IndexPath,withDropInfo dropInfo:AnyObject?) -> Void
func tableViewStopDragging(_ tableView: UITableView)->Void
//drop
func tableView(_ tableView: UITableView, canDropWithDragInfo info:AnyObject, AtIndexPath indexPath: IndexPath) -> Bool
@objc optional func tableView(_ tableView: UITableView, dropOutsideWithDragInfo info:AnyObject) -> Void
func tableView(_ tableView: UITableView, dropCompleteWithDragInfo dragInfo:AnyObject, atDragIndexPath dragIndexPath: IndexPath?,withDropInfo dropInfo:AnyObject?,atDropIndexPath dropIndexPath:IndexPath) -> Void
func tableViewStopDropping(_ tableView: UITableView)->Void
}
DragDropCollectionView 和 DragDropCollectionViewDelegate
@objc public protocol DragDropCollectionViewDelegate : NSObjectProtocol {
@objc optional func collectionView(_ collectionView: UICollectionView, indexPathForDragInfo dragInfo: AnyObject) -> IndexPath?
func collectionView(_ collectionView: UICollectionView, dragInfoForIndexPath indexPath: IndexPath) -> AnyObject
@objc optional func collectionView(_ collectionView: UICollectionView, representationImageAtIndexPath indexPath: IndexPath) -> UIImage?
//drag
func collectionView(_ collectionView: UICollectionView, touchBeginAtIndexPath indexPath:IndexPath) -> Void
func collectionView(_ collectionView: UICollectionView, canDragAtIndexPath indexPath: IndexPath) -> Bool
func collectionView(_ collectionView: UICollectionView, dragCompleteWithDragInfo dragInfo:AnyObject, atDragIndexPath dragIndexPath: IndexPath,withDropInfo dropInfo:AnyObject?) -> Void
func collectionViewStopDragging(_ collectionView: UICollectionView)->Void
//drop
func collectionView(_ collectionView: UICollectionView, canDropWithDragInfo info:AnyObject, AtIndexPath indexPath: IndexPath) -> Bool
@objc optional func collectionView(_ collectionView: UICollectionView, dropOutsideWithDragInfo info:AnyObject) -> Void
func collectionView(_ collectionView: UICollectionView, dropCompleteWithDragInfo dragInfo:AnyObject, atDragIndexPath dragIndexPath: IndexPath?,withDropInfo dropInfo:AnyObject?,atDropIndexPath dropIndexPath:IndexPath) -> Void
func collectionViewStopDropping(_ collectionView: UICollectionView)->Void
}
使用方法
-
只让视图实现Dragable接口,视图就可以拖拽。如果视图实现了Dropable接口,视图就可以放置。
-
如果你想在UITableView中使用拖拽或放置单元格,请创建DragDropTableView的子类,并实现DragDropTableViewDelegate代理。
-
如果你想在UICollectionView中使用拖拽或放置单元格,请创建DragDropCollectionView的子类,并实现DragDropCollectionViewDelegate代理。
-
在调用tableView或collectionView reloadData之前,你应该取消拖拽过程,请使用DragDropiOS.cancelDragging()。示例支持每5秒刷新一次,请在Example中使用startTimer()方法。
作者
周拉斐尔
- 给我发送邮件:[email protected]
- 在 Twitter 上关注我:@wumingapie
- 在 Facebook 上联系我:wumingapie
- 在 LinkedIn 上联系我:Rafael
许可证
DragDropiOS遵循MIT许可证。更多信息请参阅LICENSE文件。