iOS UICollectionView高级用法(长按自由移动cell)
iOS 9之后: 示例如下

前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. . 手残效果图没弄好.
@property (nonatomic, strong) UICollectionView *xtCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) CALayer *dotLayer;
@property (nonatomic, assign) CGFloat endPoint_x;
@property (nonatomic, assign) CGFloat endPoint_y;
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
初始化一个数组
self.array = [NSMutableArray arrayWithObjects:@"红包", @"转账", @"手机充值", @"芝麻信用",
@"天猫", @"生活缴费", @"蚂蚁呗", @"世界那么大",
@"余额宝", @"安全快付", @"蚂蚁聚宝", @"哈哈",@"红包1", @"转账1", @"手机充值1", @"芝麻信用1",
@"天猫1", @"生活缴费1", @"蚂蚁呗1", @"世界那么大1",
@"余额宝1", @"安全快付1", @"蚂蚁聚宝1", @"哈哈1", nil];
创建CollectionView
- (UICollectionView *)xtCollectionView
{
if (!_xtCollectionView) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.minimumLineSpacing = 1;
_flowLayout.minimumInteritemSpacing = 1;
_xtCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, Screen_Width, Screen_Height - 20) collectionViewLayout:_flowLayout];
_xtCollectionView.dataSource = self;
_xtCollectionView.backgroundColor = [UIColor colorWithRed:0.8568 green:0.8568 blue:0.8568 alpha:1.0];
_xtCollectionView.delegate = self;
[_xtCollectionView registerClass:[XTCollectCell class] forCellWithReuseIdentifier:@"cellIdentiifer"];
}
return _xtCollectionView;
}
添加一个长按的手势
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonePressMoving:)];
[self.xtCollectionView addGestureRecognizer:_longPress];
手势方法的实现
- (void)lonePressMoving:(UILongPressGestureRecognizer *)longPress
{
switch (_longPress.state) {
case UIGestureRecognizerStateBegan: {
{
NSIndexPath *selectIndexPath = [self.xtCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.xtCollectionView]];
// 找到当前的cell
XTCollectCell *cell = (XTCollectCell *)[self.xtCollectionView cellForItemAtIndexPath:selectIndexPath];
// 定义cell的时候btn是隐藏的, 在这里设置为NO
[cell.btnDelete setHidden:NO];
[_xtCollectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
[self.xtCollectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
break;
}
case UIGestureRecognizerStateEnded: {
[self.xtCollectionView endInteractiveMovement];
break;
}
default: [self.xtCollectionView cancelInteractiveMovement];
break;
}
}
移动方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
NSIndexPath *selectIndexPath = [self.xtCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.xtCollectionView]];
// 找到当前的cell
XTCollectCell *cell = (XTCollectCell *)[self.xtCollectionView cellForItemAtIndexPath:selectIndexPath];
[cell.btnDelete setHidden:YES];
[self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
[self.xtCollectionView reloadData];
}
效果图的解释: collectionView的可编辑状态是"假的", 只是对数据进行了处理
你可能想知道动画的实现可以看我的另一篇博客iOS仿美团外卖饿了吗App点餐动画
iOS9之前可以参照这个

Github上很早的项目了, 希望对有需要的同学有启发的作用, 点我下载感谢作者
补充说明: LXReorderableCollectionViewFlowLayout 这个继承于UICollectionViewFlowLayout So 对于cell的调整是比较随意像系统的一样.
比如调整cell的间距你可以这样.

LXReorderableCollectionViewFlowLayout *flowLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = ...;
flowLayout.minimumInteritemSpacing = ...;
_collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, s_w, s_h) collectionViewLayout:flowLayout];
搭配下面这个方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(..., ...);
}
iOS UICollectionView高级用法(长按自由移动cell)的更多相关文章
- iOS UICollectionView高级用法(长按自由移动cell)-新
[reference]http://www.jianshu.com/p/31d07bf32d62 iOS 9之后: 示例如下 效果 前言: 看完你可以学到哪些呢? 就是文章标题那么多, 只有那么多. ...
- iOS自动布局高级用法 && 纯代码约束写法
本文主要介绍几个我遇到的总结的高级用法(当然我相信肯定有不少比这还高级的). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以后 ...
- iOS UICollectionView(转一) XIB+纯代码创建:cell,头脚视图 cell间距
之前用CollectionViewController只是皮毛,一些iOS从入门到精通的书上也是泛泛而谈.这几天好好的搞了搞苹果的开发文档上CollectionViewController的内容,亲身 ...
- iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值)
iOS开发:使用Block在两个界面之间传值(Block高级用法:Block传值) 使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: ...
- iOS UICollectionView之三(基本用法)
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...
- iOS开发——高级技术OC篇&运行时(Runtime)机制
运行时(Runtime)机制 本文将会以笔者个人的小小研究为例总结一下关于iOS开发中运行时的使用和常用方法的介绍,关于跟多运行时相关技术请查看笔者之前写的运行时高级用法及相关语法或者查看响应官方文档 ...
- SQL[连载3]sql的一些高级用法
SQL[连载3]sql的一些高级用法 SQL 高级教程 SQL SELECT TOP SQL SELECT TOP 子句 SELECT TOP 子句用于规定要返回的记录的数目. SELECT TOP ...
- Android(java)学习笔记264:Android下的属性动画高级用法(Property Animation)
1. 大家好,在上一篇文章当中,我们学习了Android属性动画的基本用法,当然也是最常用的一些用法,这些用法足以覆盖我们平时大多情况下的动画需求了.但是,正如上篇文章当中所说到的,属性动画对补间动画 ...
- iOS UICollectionView简单使用
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableVie ...
随机推荐
- javaSe-反射3
package com.java.chap07.sec04; public class Student { private String name; private Integer age; publ ...
- 晒一下MAC下终端颜色配置
效果图: ~/.vimrc 配置 filetype on set history=1000 set background=dark syntax on set autoindent set smart ...
- GWTDesigner_v5.1.0破解码
GWTDesigner_v5.1.0_win32_x86.exe破解码,双击运行keygeno.jar,然后输入用户名.网卡MAC,然后单击Generate,将生成的文件放在C:\Documents ...
- jquerymobi总结
http://app-framework-software.intel.com/ http://app-framework-software.intel.com/api2/#$_proxy
- 解决Error"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系
今天写程序的时候调用到一个第三方的DLL文件,本机调试一切都正常,但是程序不是到服务器以后一直提示一个BUG:"基础连接已经关闭: 未能为SSL/TLS 安全通道建立信任关系". ...
- TCP/UDP 协议介绍
TCP/IP五层网络结构模型 物理层:物理层建立在物理通信介质的基础上,作为系统和通信介质的接口,用来实现数据链路实体间透明的比特 (bit) 流传输.只有该层为真实物理通信,其它各层为虚拟通信 数据 ...
- RabbitMQ学习(二):Java使用RabbitMQ要点知识
转 https://blog.csdn.net/leixiaotao_java/article/details/78924863 1.maven依赖 <dependency> <g ...
- Python——format汇总
一.str.format 按照指定格式格式化字符串,然后返回格式化的字符串,源字符串不变. 以下是Python2.7环境. 1.1.按照位置替换 参考下面例子: >>> s = '{ ...
- 【linux】【进程】stand alone 与 super daemon 区别
本文引用自 鸟哥的linux私房菜如果依据 daemon 的启动与管理方式来区分,基本上,可以将 daemon 分为可独立启动的 stand alone , 与透过一支 super daemon 来 ...
- select2插件+ajax笔记
目录 手册 思路 1. 如果是自己写的ajax这样就可以了. html里 控制器里 2. 如果是ecshop里,需要改写call方法为JQuery的ajax方法,才可以select2需要JQuery支 ...