iOS9 UICollectionView新推出的Item排序方法
2015-11-14 09:05
537 查看
协议签订
创建UICollectionView
指定代理人
添加手势
手势方法实现
代理方法实现
创建UICollectionView
指定代理人
添加手势
手势方法实现
代理方法实现
UICollectionView协议签订 添加属性
[code]@interface ZGLSubscribeCell () <UICollectionViewDataSource ,UICollectionViewDelegate> @property (nonatomic, strong) UICollectionView *subscribeCollectionView;
创建UICollectionView
[code]UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; /* ScaleX pch文件中定义的宏 用于适配屏幕尺寸 */ flow.itemSize = CGSizeMake(ScaleX * 375 / 3, ScaleX * 375 / 3); flow.minimumInteritemSpacing = 0; flow.minimumLineSpacing = 0; flow.scrollDirection = 0; self.subscribeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,ScaleX * 375, ScaleX * 375) collectionViewLayout:flow]; /* 指定代理人 */ self.subscribeCollectionView.delegate = self; self.subscribeCollectionView.dataSource = self; self.subscribeCollectionView.backgroundColor = [UIColor whiteColor]; /* UICollectionView注册cell的方法 */ [self.subscribeCollectionView registerClass:[ZGLSubscribeCollectionViewViewCell class] forCellWithReuseIdentifier:@"ZGLSubscribeCell_ZGLSubscribeCollectionViewViewCell"]; [self.subscribeCollectionView registerClass:[ZGLAddOptionsCell class] forCellWithReuseIdentifier:@"ZGLSubscribeCell_ZGLAddOptionsCell"]; /* 此处给其增加长按手势,用此手势触发cell移动效果 */ UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)]; [self.subscribeCollectionView addGestureRecognizer:longGesture]; [self.contentView addSubview:self.subscribeCollectionView];
长按手势方法实现
[code]- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture { /* 判断手势状态 */ switch (longGesture.state) { case UIGestureRecognizerStateBegan:{ /* 判断手势落点位置是否在路径上 */ NSIndexPath *indexPath = [self.subscribeCollectionView indexPathForItemAtPoint:[longGesture locationInView:self.subscribeCollectionView]]; if (indexPath == nil) { break; } /* 在路径上则开始移动该路径上的cell */ [self.subscribeCollectionView beginInteractiveMovementForItemAtIndexPath:indexPath]; } break; case UIGestureRecognizerStateChanged: /* 移动过程当中随时更新cell位置 */ [self.subscribeCollectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.subscribeCollectionView]]; break; case UIGestureRecognizerStateEnded: /* 移动结束后关闭cell移动 */ [self.subscribeCollectionView endInteractiveMovement]; break; default: [self.subscribeCollectionView cancelInteractiveMovement]; break; } }
允许移动的item
[code]- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{ /* 返回YES允许其item移动 */ return YES; }
对交换过的item数据进行操作
[code]- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath { /* 交换资源数组中移动item和目标位置item的资源位置 */ [self.optionsArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item]; [self.subscribeCollectionView reloadData]; }
抑制item移动
[code]- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath { /* 可以指定位置禁止交换 */ if (proposedIndexPath.item == _optionsArr.count) { return originalIndexPath; } else { return proposedIndexPath; } }
相关文章推荐
- iOS中 UITabBarController中自定义UITabBar
- iOS中 UITabBarController中自定义UITabBar
- Educational Codeforces Round 1 B. Queries on a String
- Definition Questions
- Leetcode Range Sum Query 2D - Immutable
- Leetcode Range Sum Query - Immutable
- 10005---servlet request请求对象常用方法总结
- GPUImage API文档之GLProgram类
- iOS之 基于UIImageView的图片浏览器
- poj2299 Ultra-QuickSort
- POJ - 1986 Distance Queries(离线Tarjan算法)
- 关于UEdiutor文本编辑器。前台展示分文字的问题
- OS X下UNIX环境高级编程(第三版)学习日志-第一章ChapterI,编译apue包与第一个例程
- 禁止uiscrollview垂直方向滚动,只允许水平方向滚动;或只允许垂直方向滚动
- UE4蓝图案例:制作按下键盘F键改变场景中模型的颜色
- iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)
- IOS的UI基础01
- hdoj 2817 A sequence of numbers【快速幂】
- Codeforces 597C Subsequences 【树状数组优化DP】