最近工程里面需要添加一個UICollectionViewCell的移動和刪除的效果,查看了很多網(wǎng)上的資料發(fā)現(xiàn),很多人寫的cell移動的效果其實不是移動的效果,只是個交換的效果 。還有刪除的效果發(fā)現(xiàn)這方面的資料太少了,所以自己寫了這篇文章。歡迎批評,吐槽?。?!
一.效果圖

2016-07-22_13-22-28.gif
二.不啰嗦,上代碼
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.array = [NSMutableArray arrayWithObjects:@"1", @"2", @"3", @"4",
@"5", @"6", @"7", @"8",
@"9", @"10", @"11", @"12",@"13", @"14", @"15", @"16",
@"17", @"18", @"19", @"20", nil];
//長按手勢添加到self.collectionView上面
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonePressMoving:)];
[self.collectionView addGestureRecognizer:_longPress];
[self.view addSubview:self.collectionView];
}
懶加載和布局collectionview的代碼
//懶加載
- (NSMutableArray *)array{
if (!_array) {
_array = [NSMutableArray array];
}
return _array;
}
//UICollectionViewCell布局代碼
- (UICollectionView *)collectionView
{
if (!_collectionView) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.minimumLineSpacing = 10;
_flowLayout.minimumInteritemSpacing = 10;
_flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);//分區(qū)內(nèi)邊距
CGFloat totalWidth = kWidth-40;
CGFloat itemWidth = (totalWidth)/3.0;
CGFloat itemHeght = 33;
//注意:item的寬高必須要提前算好
_flowLayout.itemSize = CGSizeMake(itemWidth, itemHeght);
//創(chuàng)建collectionView對象,并賦值布局
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, kWidth, kHeight - 20) collectionViewLayout:_flowLayout];
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor whiteColor];
_collectionView.delegate = self;
[_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentiifer"];
}
return _collectionView;
}
長按手勢事件判斷->核心代碼
- (void)lonePressMoving: (UILongPressGestureRecognizer *)longPress
{
switch (_longPress.state) {
case UIGestureRecognizerStateBegan: {
{
NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
// 找到當前的cell
CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
// 定義cell的時候btn是隱藏的, 在這里設置為NO
[cell.btnDelete setHidden:NO];
cell.btnDelete.tag = selectIndexPath.item;
//添加刪除的點擊事件
[cell.btnDelete addTarget:self action:@selector(btnDelete:) forControlEvents:UIControlEventTouchUpInside];
[_collectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
[self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
break;
}
case UIGestureRecognizerStateEnded: {
[self.collectionView endInteractiveMovement];
break;
}
default: [self.collectionView cancelInteractiveMovement];
break;
}
}
UICollectionViewCell的代理事件
#pragma mark---UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.array.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentiifer" forIndexPath:indexPath];
cell.lable.text = self.array[indexPath.item];
cell.btnDelete.hidden = YES;
return cell;
}
#pragma mark---UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%@",self.array[indexPath.item]);
}
//添加代碼
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
// 找到當前的cell
CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
cell.btnDelete.hidden = YES;
/*1.存在的問題,移動是二個一個移動的效果*/
// [collectionView moveItemAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
/*2.存在的問題:只是交換而不是移動的效果*/
// [self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
/*3.完整的解決效果*/
//取出源item數(shù)據(jù)
id objc = [self.array objectAtIndex:sourceIndexPath.item];
//從資源數(shù)組中移除該數(shù)據(jù)
[self.array removeObject:objc];
//將數(shù)據(jù)插入到資源數(shù)組中的目標位置上
[self.array insertObject:objc atIndex:destinationIndexPath.item];
[self.collectionView reloadData];
}
//刪除代碼
#pragma mark---btn的刪除cell事件
- (void)btnDelete:(UIButton *)btn{
//cell的隱藏刪除設置
NSIndexPath *selectIndexPath = [self.collectionView indexPathForItemAtPoint:[_longPress locationInView:self.collectionView]];
// 找到當前的cell
CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView cellForItemAtIndexPath:selectIndexPath];
cell.btnDelete.hidden = NO;
//取出源item數(shù)據(jù)
id objc = [self.array objectAtIndex:btn.tag];
//從資源數(shù)組中移除該數(shù)據(jù)
[self.array removeObject:objc];
[self.collectionView reloadData];
}
三. 實現(xiàn)功能---注意事項
3.1移動cell的主要注意點
/*1.存在的問題,移動是二個一個移動的效果*/
// [collectionView moveItemAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
/*2.存在的問題:只是交換而不是移動的效果*/
// [self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
/*3.完整的解決效果*/
//取出源item數(shù)據(jù)
id objc = [self.array objectAtIndex:sourceIndexPath.item];
//從資源數(shù)組中移除該數(shù)據(jù)
[self.array removeObject:objc];
//將數(shù)據(jù)插入到資源數(shù)組中的目標位置上
[self.array insertObject:objc atIndex:destinationIndexPath.item];
[self.collectionView reloadData];
3.2遇到一個問題:長安手勢不是很靈敏
發(fā)現(xiàn)的問題是:自己將長安的手勢添加到了具體的每個cell上面了.
解決的方法:將長按手勢添加到self.collectionView上即可.