长按拖动排序封装,支持UICollectionViewCell、UITableViewCell、自定义View,以及嵌套使用。
To run the example project, clone the repo, and run pod install
from the Example directory first.
YHLongPressDragSort is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'YHLongPressDragSort', :git => 'https://github.com/yelinux/YHLongPressDragSort.git'
Run pod install
or pod update
.
Drag the YHLongPressDragSort/Classes
folder into your project.
- Enable long press drag sorting for UICollectionViewCell:
#import "YHLongPressDragSort.h"
__weak typeof(self)weakSelf = self;
// 启用长按拖动排序
[self.collectionView yh_enableLongPressDrag:^BOOL(NSIndexPath * _Nonnull indexPath, CGPoint pressPoint) {
// 哪些cell可以长按拖动排序
return indexPath.row != 0;
} isDragMoveItem:^BOOL(NSIndexPath * _Nonnull from, NSIndexPath * _Nonnull to) {
// 拖动到某位置是否确定插入排序
if (to.row != 0) {
//更新数据源
id obj = [weakSelf.models objectAtIndex:from.row];
[weakSelf.models removeObject:obj];
[weakSelf.models insertObject:obj atIndex:to.row];
return YES;//允许插入排序
}
return NO;//不允许插入排序
}];
- Enable long press drag sorting for UITableViewCell:
#import "YHLongPressDragSort.h"
__weak typeof(self)weakSelf = self;
// 启用长按拖动排序
[self.tableView yh_enableLongPressDrag:^BOOL(NSIndexPath * _Nonnull indexPath, CGPoint pressPoint) {
// 哪些cell可以长按拖动排序
return indexPath.row != 0;
} isDragMoveItem:^BOOL(NSIndexPath * _Nonnull from, NSIndexPath * _Nonnull to) {
// 拖动到某位置是否确定插入排序
if (to.row != 0) {
//更新数据源
id obj = [weakSelf.models objectAtIndex:from.row];
[weakSelf.models removeObject:obj];
[weakSelf.models insertObject:obj atIndex:to.row];
return YES;//允许插入排序
}
return NO;//不允许插入排序
}];
- Enable long press drag sorting for your custom view(建议参考Example/YHLongPressDragSort/Classes/YHDragSortGridView.h.m):
#import "YHLongPressDragSort.h"
//继承YHDragSortBaseView基类
@interface XXXView : YHDragSortBaseView
@end
@implementation XXXView
-(void)setXXXSubViews: (NSArray<UIView*>*)views{
self.subItemViews = views;
[self.subItemViews enumerateObjectsUsingBlock:^(UIView *view, NSUInteger idx, BOOL * _Nonnull stop) {
[self addSubview:view];
}];
// 布局
[self refreshSubItemPosition];
}
// 重写基类布局方法
-(void)refreshSubItemPosition{
[self.subItemViews mas_remakeConstraints:^(MASConstraintMaker *make) {
}];
// 按照subItemViews顺序布局
}
@end
WeChat:chenyehong666888, E-mail:[email protected]
YHLongPressDragSort is available under the MIT license. See the LICENSE file for more info.