协议签订
创建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;
}
}