一榄笙、先上gif效果圖,DEMO鏈接:https://gitee.com/xudongxiang/DragDropCollectionView.git
201812251450165.gif
二糊啡、代碼實現(xiàn)細(xì)節(jié)
1. 初始化collectionView堕扶,以及遵守UICollectionViewDelegate、UICollectionViewDataSource ,并且注冊cell等一波常規(guī)操作
UICollectionViewFlowLayout *collectionFlowLayout = [[UICollectionViewFlowLayout alloc] init];
collectionFlowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 667) collectionViewLayout:collectionFlowLayout];
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
self.collectionView.dataSource = self;
self.collectionView.delegate = self;
[self.view addSubview:self.collectionView];
2. 為collectionView添加長按手勢
// 添加長按抖動手勢
[self addRecognize];
- (void)addRecognize
{
UILongPressGestureRecognizer *recognize = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
//設(shè)置長按響應(yīng)時間為0.5秒
recognize.minimumPressDuration = 0.5;
[self.collectionView addGestureRecognizer:recognize];
}
3. 實現(xiàn)長按后collectionView的抖動效果匠抗,并且為collectionView添加拖動手勢
// 長按抖動手勢方法
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longGesture {
switch (longGesture.state) {
case UIGestureRecognizerStateBegan: {
// 移除長按手勢
[self.collectionView removeGestureRecognizer:longGesture];
// 為collectionView添加拖動手勢
[self addGesture];
// 用一個BOOL類型的全局變量,記錄collectionView是否為抖動狀態(tài)门驾,YES:抖動,NO:停止抖動
_isItemShake = YES;
[self.collectionView reloadData];
}
break;
case UIGestureRecognizerStateChanged: {}
break;
case UIGestureRecognizerStateEnded: {}
break;
default:
break;
}
}
- (void)addGesture
{
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
longGesture.minimumPressDuration = 0;
[self.collectionView addGestureRecognizer:longGesture];
}
4. 實現(xiàn)collectionView的拖動效果聂沙,下面會使用iOS9的新特性實現(xiàn)拖拽,必須調(diào)用UICollectionView的下面兩個方法
// 開始移動的時候調(diào)用此方法坷随,可以獲取相應(yīng)的datasource方法設(shè)置特殊的indexpath 能否移動,如果能移動返回的是YES ,不能移動返回的是NO
- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath
// 更新移動過程的位置
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition
// 長按抖動手勢方法
- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
switch (longGesture.state) {
case UIGestureRecognizerStateBegan:{
// 通過手勢獲取點,通過點獲取點擊的indexPath, 移動該cell
NSIndexPath *aIndexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
[self.collectionView beginInteractiveMovementForItemAtIndexPath:aIndexPath];
}
break;
case UIGestureRecognizerStateChanged:{
// 通過手勢獲取點窃躲,通過點獲取拖動到的indexPath, 更新該cell位置
[self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
}
break;
case UIGestureRecognizerStateEnded:{
// 移動完成關(guān)閉cell移動
[self.collectionView endInteractiveMovement];
// 移除拖動手勢
[self.collectionView removeGestureRecognizer:longGesture];
// collectionView停止抖動
_isItemShake = NO;
// 為collectionView添加拖動手勢
[self addRecognize];
NSArray *cellArray = [self.collectionView visibleCells];
for (CollectionViewCell *cell in cellArray ) {
// 調(diào)用cell停止抖動方法
[cell stopShake];
}
}
break;
default:
[self.collectionView endInteractiveMovement];
[self.collectionView removeGestureRecognizer:longGesture];
_isItemShake = NO;
[self addRecognize];
NSArray *cellArray = [self.collectionView visibleCells];
for (CollectionViewCell *cell in cellArray ) {
[cell stopShake];
}
break;
}
}
5. 實現(xiàn)UICollectionViewDelegate钦睡、UICollectionViewDataSource
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
// 刪除數(shù)據(jù)源中初始位置的數(shù)據(jù)
id objc = [self.colorArray objectAtIndex:sourceIndexPath.item];
[self.colorArray removeObject:objc];
// 將數(shù)據(jù)插入數(shù)據(jù)源中新的位置蒂窒,實現(xiàn)數(shù)據(jù)源的更新
[self.colorArray insertObject:objc atIndex:destinationIndexPath.item];
NSArray *cellArray = [self.collectionView visibleCells];
for (CollectionViewCell *cell in cellArray ) {
[cell stopShake];
}
}
//配置cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(167, 167);
}
//配置行間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 15;
}
//配置每組上下左右的間距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0, 13, 15, 13);
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//配置每個組里面有多少個cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.colorArray.count;
}
//配置cell,并且通過_isItemShake控制cell是否抖動
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
[cell loadWithModel:self.colorArray[indexPath.row] ];
if (_isItemShake) {
// 調(diào)用cell開始抖動方法
[cell beginShake];
}else{
// 調(diào)用cell停止抖動方法
[cell stopShake];
}
return cell;
}
6. 在CollectionViewCell.m文件中實現(xiàn)cell的抖動和停止抖動方法
// 實現(xiàn)cell抖動方法
- (void)beginShake
{
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
anim.keyPath = @"transform.rotation";
anim.duration = 0.2;
anim.repeatCount = MAXFLOAT;
anim.values = @[@(-0.03),@(0.03),@(-0.03)];
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.layer addAnimation:anim forKey:@"shake"];
}
// 實現(xiàn)cell停止抖動方法
- (void)stopShake
{
[self.layer removeAllAnimations];
}