【reference】http://www.jianshu.com/p/31d07bf32d62

iOS 9之后: 示例如下

效果

前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好.

  1. @property (nonatomic, strong) UICollectionView *xtCollectionView;
  2. @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
  3. @property (nonatomic, strong) CALayer *dotLayer;
  4. @property (nonatomic, assign) CGFloat endPoint_x;
  5. @property (nonatomic, assign) CGFloat endPoint_y;
  6. @property (nonatomic, strong) UIBezierPath *path;
  7. @property (nonatomic, strong) NSMutableArray *array;
  8. @property (nonatomic, strong) UILongPressGestureRecognizer *longPress;

初始化一个数组

  1. self.array = [NSMutableArray arrayWithObjects:@"红包", @"转账", @"手机充值", @"芝麻信用",
  2. @"天猫", @"生活缴费", @"蚂蚁呗", @"世界那么大",
  3. @"余额宝", @"安全快付", @"蚂蚁聚宝", @"哈哈",@"红包1", @"转账1", @"手机充值1", @"芝麻信用1",
  4. @"天猫1", @"生活缴费1", @"蚂蚁呗1", @"世界那么大1",
  5. @"余额宝1", @"安全快付1", @"蚂蚁聚宝1", @"哈哈1", nil];

创建CollectionView

  1. - (UICollectionView *)xtCollectionView
  2. {
  3. if (!_xtCollectionView) {
  4. _flowLayout = [[UICollectionViewFlowLayout alloc] init];
  5. _flowLayout.minimumLineSpacing = 1;
  6. _flowLayout.minimumInteritemSpacing = 1;
  7. _xtCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, Screen_Width, Screen_Height - 20) collectionViewLayout:_flowLayout];
  8. _xtCollectionView.dataSource = self;
  9. _xtCollectionView.backgroundColor = [UIColor colorWithRed:0.8568 green:0.8568 blue:0.8568 alpha:1.0];
  10. _xtCollectionView.delegate = self;
  11. [_xtCollectionView registerClass:[XTCollectCell class] forCellWithReuseIdentifier:@"cellIdentiifer"];
  12. }
  13. return _xtCollectionView;
  14. }

添加一个长按的手势

  1. _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonePressMoving:)];
  2. [self.xtCollectionView addGestureRecognizer:_longPress];

手势方法的实现

  1. - (void)lonePressMoving:(UILongPressGestureRecognizer *)longPress
  2. {
  3. switch (_longPress.state) {
  4. case UIGestureRecognizerStateBegan: {
  5. {
  6. NSIndexPath *selectIndexPath = [self.xtCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.xtCollectionView]];
  7. // 找到当前的cell
  8. XTCollectCell *cell = (XTCollectCell *)[self.xtCollectionView cellForItemAtIndexPath:selectIndexPath];
  9. // 定义cell的时候btn是隐藏的, 在这里设置为NO
  10. [cell.btnDelete setHidden:NO];
  11. [_xtCollectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
  12. }
  13. break;
  14. }
  15. case UIGestureRecognizerStateChanged: {
  16. [self.xtCollectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
  17. break;
  18. }
  19. case UIGestureRecognizerStateEnded: {
  20. [self.xtCollectionView endInteractiveMovement];
  21. break;
  22. }
  23. default: [self.xtCollectionView cancelInteractiveMovement];
  24. break;
  25. }
  26. }

移动方法

  1. - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
  2. {
  3. NSIndexPath *selectIndexPath = [self.xtCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.xtCollectionView]];
  4. // 找到当前的cell
  5. XTCollectCell *cell = (XTCollectCell *)[self.xtCollectionView cellForItemAtIndexPath:selectIndexPath];
  6. [cell.btnDelete setHidden:YES];
  7. [self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
  8. [self.xtCollectionView reloadData];
  9. }

效果图的解释: collectionView的可编辑状态是"假的", 只是对数据进行了处理
你可能想知道动画的实现可以看我的另一篇博客iOS仿美团外卖饿了吗App点餐动画

iOS9之前可以参照这个

效果

Github上很早的项目了, 希望对有需要的同学有启发的作用, 点我下载感谢作者

补充说明: LXReorderableCollectionViewFlowLayout 这个继承于UICollectionViewFlowLayout So 对于cell的调整是比较随意像系统的一样.
比如调整cell的间距你可以这样.

调整---cell间距
  1. LXReorderableCollectionViewFlowLayout *flowLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
  2. flowLayout.minimumLineSpacing = ...;
  3. flowLayout.minimumInteritemSpacing = ...;
  4. _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, s_w, s_h) collectionViewLayout:flowLayout];

搭配下面这个方法

  1. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. return CGSizeMake(..., ...);
  4. }

文/夏天然后(简书作者)
原文链接:http://www.jianshu.com/p/31d07bf32d62
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

iOS UICollectionView高级用法(长按自由移动cell)-新的更多相关文章

  1. iOS UICollectionView高级用法(长按自由移动cell)

    iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好. @property (nonatomic, strong) UIColle ...

  2. iOS自动布局高级用法 && 纯代码约束写法

    本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...

  3. iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距

    之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...

  4. iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)

    iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)   使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...

  5. iOS UICollectionView之三(基本用法)

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  6. iOS开发——高级技术OC篇&运行时(Runtime)机制

    运行时(Runtime)机制 本文将会以笔者个人的小小研究为例总结一下关于iOS开发中运行时的使用和常用方法的介绍,关于跟多运行时相关技术请查看笔者之前写的运行时高级用法及相关语法或者查看响应官方文档 ...

  7. SQL[连载3]sql的一些高级用法

    SQL[连载3]sql的一些高级用法 SQL 高级教程 SQL SELECT TOP SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP ...

  8. Android(java)学习笔记264:Android下的属性动画高级用法(Property Animation)

    1. 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是最常用的一些用法,这些用法足以覆盖我们平时大多情况下的动画需求了.但是,正如上篇文章当中所说到的,属性动画对补间动画 ...

  9. iOS UICollectionView简单使用

    UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...

随机推荐

  1. CodeForces 383D Antimatter

    线性DP. dp[i][j]表示以第i个数字为结尾的,字串和为j的有几种. #include<cstdio> #include<cstring> #include<cma ...

  2. AngularJs: Reload page

    <a ng-click="reloadRoute()" class="navbar-brand" title="home" data- ...

  3. Struts2 语法--result type

    result type: dispatcher,redirect:只能跳转到jsp,html之类的页面,dispatcher属于服务器跳转, redirect属于客户端跳转 chain: 等同于for ...

  4. attach

    http://bbs.chinaunix.net/thread-2091967-1-1.html 大概跟父进程,子进程,信号等有关,一个没有操作系统的赤裸裸的单片机上是不可以attach的.

  5. 过滤字符串html标签方法

    过滤字符串html标签方法,如果输入的过滤标签为“*”,那么给字符串加上p标签 public static string noTagHtml(string str, string tagname) { ...

  6. Delphi MaskEdit用法(转)

    源:http://www.cnblogs.com/zhangzhifeng/archive/2011/10/12/2208640.html MaskEdit是用来建立编辑框的,但它与Edit编辑框可以 ...

  7. AngularJS Front-End App with Cloud Storage Tutorial Part 1: Building a Minimal App in Seven Steps

    原文 : http://www.codeproject.com/Articles/1027709/AngularJS-Front-End-App-with-Cloud-Storage-Tutoria ...

  8. 自行修改android.jar使其包含隐藏api

    1) 从指定版本的rom内获取到framework.jar 2) 解压framework.jar和android sdk内的android.jar 3) 将framework.jar解出来的东西拷到a ...

  9. zencart 具体页面调用规则: $body_code变量解析

    zencart $body_code变量解析 修改centerColumn 可以修改中间产品方框的大小 2.2.5 .BODY文件在这个文件生效 require($body_code) include ...

  10. select下拉框

    <optgroup label="Alaskan/Hawaiian Time Zone"> <option value="AK">Ala ...