1.长按cell的情况下实现拖动,所以理应想到用长按手势。

2.既然实现移动cell,就要看看UICollectionView 有没有方法或者协议可以移动的。通过查看UICollectionView的协议方法,可以在UICollectionViewDataSource中看到有两个方法。

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;

3.OK 知道了以上两个步骤,那么就可以开始写代码了。

// 设置手势
- (void)setupGesture
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)]; [self.collectionView addGestureRecognizer:longPress];
}
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
//判断手势状态
switch (longGesture.state) {
case UIGestureRecognizerStateBegan:
    {
//判断手势落点位置是否在路径上
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
//在路径上则开始移动该路径上的cell
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:
//移动过程当中随时更新cell位置
[self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
break;
case UIGestureRecognizerStateEnded:
//移动结束后关闭cell移动
[self.collectionView endInteractiveMovement];
break;
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
//返回YES允许其item移动
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
//取出源item数据
id objc = [_dataSource objectAtIndex:sourceIndexPath.item];
//从资源数组中移除该数据
[_dataSource removeObject:objc];
//将数据插入到资源数组中的目标位置上
[_dataSource insertObject:objc atIndex:destinationIndexPath.item];
}

注:_dataSource 是你的数据源。

最新文章

  1. 解决“添加远程依赖方式没有效果”的bug
  2. Eclipse导入项目出现红色叹号的解决方法
  3. 处于同一个域中的两台Sql server 实例无法连接
  4. 在Asp.Net MVC中实现RequiredIf标签对Model中的属性进行验证
  5. 【bzoj4010】 HNOI2015—菜肴制作
  6. mha 自动failover 原创
  7. 1018: [SHOI2008]堵塞的交通traffic - BZOJ
  8. C#解决微信支付Exception has been thrown by the target of an invocation(调用的目标发生了异常)的问题
  9. VMware GSX Server 3.2.1 Build 19281免费下载
  10. 使用Spring的@Autowired 实现DAO, Service, Controller三层的注入(转)
  11. scala与java的==的比较
  12. 【 js 基础 】【 源码学习 】backbone 源码阅读(一)
  13. .NET Core微服务之服务间的调用方式(REST and RPC)
  14. HTML+CSS基础(2)-HTML标签的简单介绍和网页注释
  15. BZOJ4484: [Jsoi2015]最小表示(拓扑排序乱搞+bitset)
  16. Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
  17. [转帖]前端-chromeF12 谷歌开发者工具详解 Console篇
  18. oracle dblink的创建与使用
  19. win10 校园宽带连接不上的解决办法(错误720、“以太网”没有有效的ip设置)
  20. django ---- models继承

热门文章

  1. JavaScript 之 Function
  2. python期末考试复习
  3. mysql插入文本文档及读取
  4. ansible-playbook权限提升多种方式
  5. nginx常用编译参数
  6. 计算机网络(6): http cookie
  7. 在WSL Ubuntu1804中安装Docker
  8. MyBatis从入门到精通(第4章):MyBatis动态SQL【foreach、bind、OGNL用法】
  9. LNMP应用
  10. java使用HSSFWorkbook下载Excel表格