iOS | 实现拖拽CollectionViewCell排序
现在很多项目都会用到类似拖动的效果,比如今日头条和网易新闻之类的资讯类产品,都有用该技术设置模块顺序的操作。
在iOS9.0之后,苹果提供相关的方法,非常方便。
设定三个私有属性
@property(nonatomic,strong) NSMutableArray *arr;
@property(nonatomic,weak) UICollectionView *colView;
@property(nonatomic,strong) UILongPressGestureRecognizer *longPress;
//数据源
- (NSMutableArray *)arr{
if (!_arr) {
_arr = [NSMutableArray arrayWithObjects:@(1),@(2),@(3),@(4),@(5),@(6),@(7),@(8),@(9), nil];
}
return _arr;
}
- 先创建UICollectionView
//创建布局对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//view
UICollectionView *colView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//背景色
colView.backgroundColor = [UIColor whiteColor];
colView.delegate = self;
colView.dataSource = self;
//控制布局
colView.contentInset = UIEdgeInsetsMake(30, 20, 0, 20);
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat space = 20;
NSInteger col = 3;
CGFloat itemSize = (screenW - (( col + 1 ) * space) - 6) / 3;
flowLayout.itemSize = CGSizeMake(itemSize, itemSize);
flowLayout.minimumInteritemSpacing = space;
flowLayout.minimumLineSpacing = space;
//添加长按手势
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMoving:)];
[colView addGestureRecognizer:_longPress];
//属性连接
self.colView = colView;
//注册cell,记得先创建一个自定义cell
[colView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:colView];
--------------------------------------数据源方法---------------------------------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arr.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.num.text = [NSString stringWithFormat:@"%@",self.arr[indexPath.row]];
return cell;
}
2.长按手势响应事件
- (void)longPressMoving:(UILongPressGestureRecognizer *)longPress{
// 筛选长按手势状态
switch (_longPress.state) {
// 开始
case UIGestureRecognizerStateBegan: {
{
//手势作用的位置
NSIndexPath *selectIndexPath = [self.colView indexPathForItemAtPoint:[_longPress locationInView:self.colView]];
// 找到当前的cell
MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.colView cellForItemAtIndexPath:selectIndexPath];
// 拽起变大动画效果
[UIView animateWithDuration:0.3 animations:^{
[cell setTransform:CGAffineTransformMakeScale(1.2, 1.2)];
}];
//开始移动
[_colView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
//更新移动的位置
[self.colView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
break;
}
case UIGestureRecognizerStateEnded: {
//结束移动
[self.colView endInteractiveMovement];
break;
}
default: [self.colView cancelInteractiveMovement];
break;
}
}
3.实现苹果官方的代理方法
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"%zd---%zd",sourceIndexPath.row,destinationIndexPath.row);
NSIndexPath *selectIndexPath = [self.colView indexPathForItemAtPoint:[_longPress locationInView:self.colView]];
//交换数据源的内容
[self.arr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
// [self.colView reloadData];
NSLog(@"%@",self.arr);
}
实现完以上的方法,可以快速构建一个可拖拽排序的cell界面。
iOS | 实现拖拽CollectionViewCell排序的更多相关文章
- VUE +element el-table运用sortable 拖拽table排序,实现行排序,列排序
Sortable.js是一款轻量级的拖放排序列表的js插件(虽然体积小,但是功能很强大) 项目需求是要求能对element中 的table进行拖拽行排序 这里用到了sorttable Sortable ...
- 通过layout实现可拖拽自动排序的UICollectionView
文/CenturyGuo(简书作者)原文链接:http://www.jianshu.com/p/8d1bf1838882著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. Translat ...
- js 利用jquery.gridly.js实现拖拽并且排序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ios 为什么拖拽的控件为weak 手写的strong
ib拖拽的控件自动声明为weak 而平时自己手写的为strong 在ios中,对象默认都是强引用,不是强引用赋值后会立即释放 ib声明weak 不立即被释放 简单说就是 1.声明的弱引用指向强引用 ...
- Jquery 多行拖拽图片排序 jq优化
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- IOS view拖拽(触摸事件)
• iOS中的事件可以分为3大类型 触摸事件 加速计事件 远程控制事件 响应者对象 • 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事 件.我们称之为“响应 ...
- zTree的拖拽排序
ztree本身是可以支持拖拽的,但是却没有找到明确的支持拖拽的排序,也就是说,在拖拽过程中,需要自定义维护拖拽后的顺序并保存至后台. 在这样一个比较常规的需求情况下,网上也有朋友给出了一些解决方案,比 ...
- vue中基于sortablejs与el-upload实现文件上传后拖拽排序
今天做冒烟测试的时候发现商品发布有一个拖拽图片排序功能没做,赶紧加上 之前别的同事基于 vuedraggable 实现过这个功能,我这里自己深度封装了 el-upload ,用这种方式改动很大,而且感 ...
- Android 仿今日头条频道管理(上)(GridView之间Item的移动和拖拽)
前言 常常逛今日头条.发现它的频道管理功能做的特别赞.交互体验很好.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fo ...
随机推荐
- The nineteenth day
Twinkle,twinkle,little start! 闪烁,闪烁,小星星 How I wonder what you are, 我想知道你是什么 Up above the world so hi ...
- CSS深入理解学习笔记之border
1.border-width border-width为何不支持百分比:语义和使用场景决定的,现实中各种边框本身的概念就不存在百分比的使用方法. border-width支持关键字:thin.medi ...
- JavaScript入门几个概念
JavaScript入门几个概念 刚刚入门JavaScript的时候,搞懂DOM.BOM以及它们的对象document和window很有必要. DOM是为了操作文档出现的API,document是它的 ...
- Android照片库选择图片裁剪闪退(兼容小米以及7.0以上机型)
未经允许,禁止
- WinBatch基础命令
1.echo --> echo[{on|off}][message] Simple -->: @echooff echo hello world 2.@ -->@echo off 3 ...
- IOS APP 瘦身
只保留其中一宗编译环境包 lipo -thin armv7 XXAPP -output XXAPP.armv7
- JDE获取所有字典数据
select a.*,b.DTDL01 FROM crpctl.f0004 a,crpctl.f0004d b where a.dtsy =b.dtsy(+) and a.dtrt =b.dtrt(+ ...
- linux c 监控目录
static void* thread_monitor(void* args) { pthread_detach(pthread_self()); int fd; int wd; int len; i ...
- wxPython控件学习之wx.grid.Grid 表格控件
wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEdit ...
- #学习笔记#e2e学习使用(二)
前言: <#学习笔记#e2e学习使用(一)>主要记录了Vue项目的创建到e2e环境的搭建,以及期间遇到的各种问题和解决方法.本文建立在基础测试环境搭建完毕能正确运行的情况下,编写测试代码, ...