在使用苹果手机的时候,大家可能会发现这样的一种操作效果:
UICollectionView.gif
就是在一个UICollectionView上,长按某一个cell,就能移动它,并且能够放在新的位置。这个方法就用到了UICollectionVIew的移动方法了,话不多说,直接上代码:
#import "ViewController.h"
#import "CollectionViewCell.h"
#import "ImageModel.h"
@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
/// collectionView
@property (nonatomic, strong) UICollectionView *collectionView;
/// 数据
@property (nonatomic, strong) NSMutableArray<ImageModel *> *imageModels;
/// 是否移动
@property (nonatomic, assign) BOOL isBeganMove;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//获取数据
[self getDatas];
//注册cell
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
[self.collectionView reloadData];
}
/// 创建假数据
-(void)getDatas {
self.imageModels = [NSMutableArray array];
for (int index = 0; index < 10; index ++) {
ImageModel *model = [[ImageModel alloc] init];
model.imageName = [NSString stringWithFormat:@"image%d", index];
[self.imageModels addObject:model];
}
}
#pragma mark - 手势事件
-(void)moveCollectionViewCell:(UILongPressGestureRecognizer *)gesture {
switch (gesture.state) {
case UIGestureRecognizerStateBegan: {
if (!self.isBeganMove) {
self.isBeganMove = YES;
//获取点击的cell的indexPath
NSIndexPath *selectedIndexPath = [self.collectionView indexPathForItemAtPoint:[gesture locationInView:self.collectionView]];
//开始移动对应的cell
[self.collectionView beginInteractiveMovementForItemAtIndexPath:selectedIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
//移动cell
[self.collectionView updateInteractiveMovementTargetPosition:[gesture locationInView:self.collectionView]];
break;
}
case UIGestureRecognizerStateEnded: {
self.isBeganMove = false;
//结束移动
[self.collectionView endInteractiveMovement];
break;
}
default:
[self.collectionView cancelInteractiveMovement];
break;
}
}
#pragma mark - UICollectionViewDataSource/UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.imageModels.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:self.imageModels[indexPath.row].imageName];
//添加长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveCollectionViewCell:)];
[cell addGestureRecognizer:longPress];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
//处理数据(删除之前的位置数据,插入到新的位置)
ImageModel *selectModel = self.imageModels[sourceIndexPath.item];
[self.imageModels removeObjectAtIndex:sourceIndexPath.item];
[self.imageModels insertObject:selectModel atIndex:destinationIndexPath.item];
//此处可以根据需要,上传给后台目前数据的顺序
}
#pragma mark - 懒加载
-(UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 10;
layout.minimumLineSpacing = 10;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 2.f - 20.f, [UIScreen mainScreen].bounds.size.width / 2.f - 20.f);
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_collectionView];
}
return _collectionView;
}
@end
这些代码其实说的很明白了。大家发现,其实在移动的过程中,相当不停的调用UICollectionView的移动cell的方法,并且不停地变化着数据数组的顺序。这样就可以保证在改变cell的位置的同时,也改变了数据的顺序。
看到代码之后,大家就会发现,这个需求简直太容易了,喜欢的话就点个赞吧。
网友评论