最近,在一些iOS開發(fā)群里看到好多人都在詢問一些UICollectionViewCell的移動以及刪除的輪子略水,本著好奇的心態(tài)徽诲,就去嘗試的做一做。
效果圖如下:
移動調(diào)用的代理方法
- (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.先懶加載數(shù)據(jù)以及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.實現(xiàn)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.實現(xiàn)手勢的方法
- (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];
}
到這一步就大功告成了牡属。