现在很多项目都会用到类似拖动的效果,比如今日头条和网易新闻之类的资讯类产品,都有用该技术设置模块顺序的操作。

在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;
}
  1. 先创建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排序的更多相关文章

  1. VUE +element el-table运用sortable 拖拽table排序,实现行排序,列排序

    Sortable.js是一款轻量级的拖放排序列表的js插件(虽然体积小,但是功能很强大) 项目需求是要求能对element中 的table进行拖拽行排序 这里用到了sorttable Sortable ...

  2. 通过layout实现可拖拽自动排序的UICollectionView

    文/CenturyGuo(简书作者)原文链接:http://www.jianshu.com/p/8d1bf1838882著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. Translat ...

  3. js 利用jquery.gridly.js实现拖拽并且排序

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. ios 为什么拖拽的控件为weak 手写的strong

    ib拖拽的控件自动声明为weak  而平时自己手写的为strong 在ios中,对象默认都是强引用,不是强引用赋值后会立即释放 ib声明weak 不立即被释放 简单说就是 1.声明的弱引用指向强引用 ...

  5. Jquery 多行拖拽图片排序 jq优化

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. IOS view拖拽(触摸事件)

    • iOS中的事件可以分为3大类型 触摸事件 加速计事件 远程控制事件 响应者对象 • 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事 件.我们称之为“响应 ...

  7. zTree的拖拽排序

    ztree本身是可以支持拖拽的,但是却没有找到明确的支持拖拽的排序,也就是说,在拖拽过程中,需要自定义维护拖拽后的顺序并保存至后台. 在这样一个比较常规的需求情况下,网上也有朋友给出了一些解决方案,比 ...

  8. vue中基于sortablejs与el-upload实现文件上传后拖拽排序

    今天做冒烟测试的时候发现商品发布有一个拖拽图片排序功能没做,赶紧加上 之前别的同事基于 vuedraggable 实现过这个功能,我这里自己深度封装了 el-upload ,用这种方式改动很大,而且感 ...

  9. Android 仿今日头条频道管理(上)(GridView之间Item的移动和拖拽)

    前言 常常逛今日头条.发现它的频道管理功能做的特别赞.交互体验很好.如图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fo ...

随机推荐

  1. CheckBoxList

    CheckBoxList 控件基本用法 定义和用法 CheckBoxList 控件用来建立一个多选的复选框组. CheckBoxList 控件中的每个可选项由一个 ListItem 元素来定义! 提示 ...

  2. 弹性布局学习-详解 align-items(四)

    目录 弹性布局学习-介绍(一)  弹性布局学习-详解 flex-direction[决定主轴的方向](二) 弹性布局学习-详解 justify-content(三) 弹性布局学习-详解 align-i ...

  3. hibernate的查询 (比较get 与load)

    hibernate的查询的比较hibernate的查询有很多,Query,find,Criteria,get,load query使用hsql语句,可以设置参数是常用的一种方式 criteria的方式 ...

  4. flask-session总结

    一.session       session和cookie的原理和区别: cookie是保存在浏览器上的键值对             session是存在服务端的键值对(服务端的session就是 ...

  5. Android开发ListView嵌套ImageView实现单选按钮

    做Android开发两年的时间,技术稍稍有一些提升,刚好把自己实现的功能写出来,记录一下,如果能帮助到同行的其他人,我也算是做了件好事,哈哈!!废话不多说,先上个图. 先上一段代码: if (last ...

  6. <Android 应用 之路> 简易手电筒

    前言 快一个月没有写自己的博客了,由于最近换了工作,换了居住地,所以有一些杂事需要处理,从今天开始恢复正常,不赘述了.进入今天的主题 -– 简易的手电筒. 这个Demo中使用的是比较新的API,M版本 ...

  7. 关于Java中的线程安全(线程同步)

    java中的线程安全是什么: 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法进行访问 什么叫 ...

  8. RecycleView + SwipeRefreshLayout 实现下拉刷新和底部自动加载

    前段时间项目里面使用了RecycleView 但是里面的刷新和加载都是框架里面封装好的,直接使用 这几天比较闲就自己来实现以下. 因为SwipeRefreshLayout是一个下拉刷新控件所有直接和R ...

  9. wxpython 按钮等事件的触发

    1.按钮事件的触发 方法中第二个参数为event

  10. Tomcat中部署web应用的三种方式

    Tomcat中部署web应用的三种方式(静态部署)       第一种,针对war或解压后的war,最为常用的是直接操作webapp目录,将完整的war包或者web应用直接放到webapp目录下.使用 ...