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

在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. 重构指南 - 引入参数对象(Introduce Parameter Object)

    当一个方法的参数超过3个以上,就可以考虑将参数封装成一个对象.将参数封装成对象后提高了代码的可读性,并且该参数对象也可以供多个方法调用,以后如果增加删除参数,方法本身不需要修改,只需要修改参数对象就可 ...

  2. echarts环形图点击旋转并高亮

    通过计算某个扇形区域的值占整个圆的百分比来得到这个扇形的角度,从而根据startAngle这个属性来设定图形的开始渲染的角度,使点击某个扇形时圆环旋转使之始终对准某个点. 期间考虑到某扇形区域太小点击 ...

  3. SQLAlchemy的使用---查询的更多操作

    # 查询更多操作 from create_table import User, engine from sqlalchemy.orm import sessionmaker Session = ses ...

  4. Python基础 数据类型 (字符串、列表、字典、元组、集合、堆、栈、树)

    数据类型有整型.布尔.字符串.列表.字典.元组.集合.堆.栈和树. 1.整型: 整型就是数字 数字表示 python2 64位机器,范围-2^63~2^63-1 超出上述范围,python自动转化为l ...

  5. js中实现多态

    最近读到一本书<JavaScript设计模式与开发实践>上,讲到js的多态,我在JavaScript高级程序编程里貌似都没有见过关于这个的详细讲解,所以想问问大家有没有什么推荐的文章或者博 ...

  6. 为mongodb添加账号

    进入切换到某一个数据库,我这里是位每个模块分配一个DataBase use 0 执行添加账号命令 db.createUser( { user: "*****", pwd: &quo ...

  7. android,getExternalStorageDirectory()和getExternalFilesDir()的区别

    转载地址:https://blog.csdn.net/nugongahou110/article/details/48154859 之前看到一位网友的吐槽:当我把手机连接到电脑上时,在SD卡根目录看到 ...

  8. JS高级程序设计第三版——JavaScript简介

    JavaScript简史 JavaScript由Netscape(网景)公司在1995年发布,最开始的主要目的是处理以前由服务器端语言负责的一些输入验证操作,以便提高用户体验,后来就慢慢的发展为一门强 ...

  9. haproxy学习——安装(一)

    安装包:haproxy-1.5.4.tar.gz (挺小的,大约1.3M) ①.首先要sz到本地虚拟机上(centos-6.5),tar zxvf haproxy-1.5.4.tar.gz,完成解压. ...

  10. (二)selenium元素定位

    selenium定位方法 Selenium提供了8种定位方式. id name class name tag name link text partial link text xpath css se ...