YHLongPressDragSort
长按拖动排序封装,支持UICollectionViewCell、UITableViewCell、自定义View,以及嵌套使用。
示例
安装
CocoaPods (推荐)
打开 Podfile 并编辑:
pod 'YHLongPressDragSort', :git => 'https://github.com/yelinux/YHLongPressDragSort.git'
执行命令 pod install 或者 pod update.
手动集成
把YHLongPressDragSort/Classes文件夹拖进你的项目.
使用
- UICollectionView启用长按拖动排序:
#import "YHLongPressDragSort.h"
__weak typeof(self)weakSelf = self;
// 启用长按拖动排序
[self.collectionView yh_enableLongPressDrag:^BOOL(NSIndexPath * _Nonnull indexPath, CGPoint pressPoint) {
// 哪些cell可以长按拖动排序
return indexPath.row != 0;
} isDragBeginBlock:^(NSIndexPath * _Nonnull indexPath) {
// 开始拖动,启用自定义动画
for (NSIndexPath *ixPath in [weakSelf.collectionView indexPathsForVisibleItems]){
if (ixPath.row != 0) {
UICollectionViewCell *cell = [weakSelf.collectionView cellForItemAtIndexPath:ixPath];
[cell.contentView.layer addAnimation:weakSelf.dragAnim forKey:nil];
}
}
} 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;//不允许插入排序
} dragEndBlock:^{
// 拖动结束,关闭自定义动画
for (NSIndexPath *ixPath in [weakSelf.collectionView indexPathsForVisibleItems]){
if (ixPath.row != 0) {
UICollectionViewCell *cell = [weakSelf.collectionView cellForItemAtIndexPath:ixPath];
[cell.contentView.layer removeAllAnimations];
}
}
}];
- UITableView启用长按拖动排序:
#import "YHLongPressDragSort.h"
__weak typeof(self)weakSelf = self;
// 启用长按拖动排序
[self.tableView yh_enableLongPressDrag:^BOOL(NSIndexPath * _Nonnull indexPath, CGPoint pressPoint) {
// 哪些cell可以长按拖动排序
return indexPath.row != 0;
} dragBeginBlock:^(NSIndexPath * _Nonnull indexPath) {
// 开始拖动,启用自定义动画
for (NSIndexPath *ixPath in [weakSelf.tableView indexPathsForVisibleRows]){
if (ixPath.row != 0) {
UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:ixPath];
[cell.contentView.layer addAnimation:weakSelf.dragAnim forKey:nil];
}
}
} 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;//不允许插入排序
} dragEndBlock:^{
// 拖动结束,关闭自定义动画
for (NSIndexPath *ixPath in [weakSelf.tableView indexPathsForVisibleRows]){
if (ixPath.row != 0) {
UITableViewCell *cell = [weakSelf.tableView cellForRowAtIndexPath:ixPath];
[cell.contentView.layer removeAllAnimations];
}
}
}];
- 自定义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
源码
Author
WeChat:chenyehong666888, E-mail:ichenevan@126.com
License
YHLongPressDragSort is available under the MIT license. See the LICENSE file for more info.