iOS9 UICollectionView新推出的Item排序方法
来源:互联网 发布:万科弗农小镇 知乎 编辑:程序博客网 时间:2024/06/11 14:02
协议签订
创建UICollectionView
指定代理人
添加手势
手势方法实现
代理方法实现
UICollectionView协议签订 添加属性
@interface ZGLSubscribeCell () <UICollectionViewDataSource ,UICollectionViewDelegate>@property (nonatomic, strong) UICollectionView *subscribeCollectionView;
创建UICollectionView
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];
长按手势方法实现
- (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
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{ /* 返回YES允许其item移动 */ return YES;}
对交换过的item数据进行操作
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath { /* 交换资源数组中移动item和目标位置item的资源位置 */ [self.optionsArr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item]; [self.subscribeCollectionView reloadData];}
抑制item移动
- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath { /* 可以指定位置禁止交换 */ if (proposedIndexPath.item == _optionsArr.count) { return originalIndexPath; } else { return proposedIndexPath; }}
1 0
- iOS9 UICollectionView新推出的Item排序方法
- iOS9的新特性
- ios9的新特性
- iOS9的新特性
- UICollectionView的无缝排序
- UICollectionView关于item的报错
- UICollectionView实现item的重新排布
- UICollectionView的item长按移动编辑
- iOS UIcollectionView 和 UITableView 限制item 或cell的交换方法
- iOS9 新的通讯录框架
- iOS9开始的新特性
- iOS6新特征:UICollectionView介绍 --UICollectionView不错的文章
- UICollectionView可移动item
- iOS9的一些新的关键字
- iOS9开放的新API--Spotlight使用指南
- iOS8和iOS9新特性的总结
- iOS9新特性需要注意的问题
- iOS9 iOS10 iOS11的新特性
- HTML5提供的拖拽功能
- Android基础入门教程——8.3.16 Canvas API详解(Part 1)
- 【UML学习】UML概述
- TableView的各种小问题,比如去掉多余的行数,去掉 tableView 上面的空白部分
- iOS9与XCode7中不能使用http连接的解决办法
- iOS9 UICollectionView新推出的Item排序方法
- Linux 2.6内核启动传递命令行的过程分析
- Yii2 菜单menu管理
- 计算机视觉基础1——视差与深度信息
- JavascriptDOM编程艺术学习笔记2
- iOS开发——通过代码方式使用AutoLayout
- EditText属性总结
- nyoj 免费馅饼 613 (DP状态转移方程)
- CF--B - Queries on a String