最近,在一些iOS开发群里看到好多人都在询问一些UICollectionViewCell的移动以及删除的轮子,本着好奇的心态,就去尝试的做一做。
效果图如下:
移动调用的代理方法
- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0); // returns NO if reordering was prevented from beginning - otherwise YES
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition NS_AVAILABLE_IOS(9_0);
- (void)endInteractiveMovement NS_AVAILABLE_IOS(9_0);
- (void)cancelInteractiveMovement NS_AVAILABLE_IOS(9_0);
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
上代码
1.先懒加载数据以及CollectionView
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UICollectionView *collectionView;
- (NSMutableArray *)dataArr {
if (!_dataArr) {
_dataArr = [NSMutableArray new];
for (int i = 0 ; i < 20; i ++) {
[_dataArr addObject:[NSString stringWithFormat:@"%d",i]];
}
}
return _dataArr;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
[_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
}
return _collectionView;
}
2.实现collectionView的代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.dataArr count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(100, 50);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = self.dataArr[indexPath.item];
cell.deleteBtn.hidden = YES;
return cell;
}
3.在collectionView上添加一个长按手势
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(moveAction:)];
_collectionView.userInteractionEnabled = YES;
[_collectionView addGestureRecognizer:longPressGesture];
4.实现手势的方法
- (void)moveAction:(UILongPressGestureRecognizer *)longGes {
if (longGes.state == UIGestureRecognizerStateBegan) {
NSIndexPath *selectPath = [self.collectionView indexPathForItemAtPoint:[longGes locationInView:longGes.view]];
CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectPath];
cell.deleteBtn.hidden = NO;
[cell.deleteBtn addTarget:self action:@selector(deleteItemAction:) forControlEvents:UIControlEventTouchUpInside];
cell.deleteBtn.tag = selectPath.item;
[self.collectionView beginInteractiveMovementForItemAtIndexPath:selectPath];
}else if (longGes.state == UIGestureRecognizerStateChanged) {
[self.collectionView updateInteractiveMovementTargetPosition:[longGes locationInView:longGes.view]];
}else if (longGes.state == UIGestureRecognizerStateEnded) {
[self.collectionView endInteractiveMovement];
}else {
[self.collectionView cancelInteractiveMovement];
}
}
5.删除cell方法
- (void)deleteItemAction:(UIButton *)btn {
[self.dataArr removeObjectAtIndex:btn.tag];
[self.collectionView reloadData];
}
6.移动cell方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
id obj = self.dataArr[sourceIndexPath.item];
[self.dataArr removeObjectAtIndex:sourceIndexPath.item];
[self.dataArr insertObject:obj atIndex:destinationIndexPath.item];
[self.collectionView reloadData];
}
到这一步就大功告成了。