現(xiàn)在很多項(xiàng)目都會(huì)用到類似拖動(dòng)的效果阻问,比如今日頭條和網(wǎng)易新聞之類的資訊類產(chǎn)品沦疾,都有用該技術(shù)設(shè)置模塊順序的操作。
在iOS9.0之后刨秆,蘋果提供相關(guān)的方法忆畅,非常方便。
設(shè)定三個(gè)私有屬性
@property(nonatomic,strong) NSMutableArray *arr;
@property(nonatomic,weak) UICollectionView *colView;
@property(nonatomic,strong) UILongPressGestureRecognizer *longPress;
//數(shù)據(jù)源
- (NSMutableArray *)arr{
if (!_arr) {
_arr = [NSMutableArray arrayWithObjects:@(1),@(2),@(3),@(4),@(5),@(6),@(7),@(8),@(9), nil];
}
return _arr;
}
- 先創(chuàng)建UICollectionView
//創(chuàng)建布局對象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//view
UICollectionView *colView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
//背景色
colView.backgroundColor = [UIColor whiteColor];
colView.delegate = self;
colView.dataSource = self;
//控制布局
colView.contentInset = UIEdgeInsetsMake(30, 20, 0, 20);
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat space = 20;
NSInteger col = 3;
CGFloat itemSize = (screenW - (( col + 1 ) * space) - 6) / 3;
flowLayout.itemSize = CGSizeMake(itemSize, itemSize);
flowLayout.minimumInteritemSpacing = space;
flowLayout.minimumLineSpacing = space;
//添加長按手勢
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressMoving:)];
[colView addGestureRecognizer:_longPress];
//屬性連接
self.colView = colView;
//注冊cell,記得先創(chuàng)建一個(gè)自定義cell
[colView registerNib:[UINib nibWithNibName:@"MyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
[self.view addSubview:colView];
--------------------------------------數(shù)據(jù)源方法---------------------------------------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.arr.count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.num.text = [NSString stringWithFormat:@"%@",self.arr[indexPath.row]];
return cell;
}
2.長按手勢響應(yīng)事件
- (void)longPressMoving:(UILongPressGestureRecognizer *)longPress{
// 篩選長按手勢狀態(tài)
switch (_longPress.state) {
// 開始
case UIGestureRecognizerStateBegan: {
{
//手勢作用的位置
NSIndexPath *selectIndexPath = [self.colView indexPathForItemAtPoint:[_longPress locationInView:self.colView]];
// 找到當(dāng)前的cell
MyCollectionViewCell *cell = (MyCollectionViewCell *)[self.colView cellForItemAtIndexPath:selectIndexPath];
// 拽起變大動(dòng)畫效果
[UIView animateWithDuration:0.3 animations:^{
[cell setTransform:CGAffineTransformMakeScale(1.2, 1.2)];
}];
//開始移動(dòng)
[_colView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
//更新移動(dòng)的位置
[self.colView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
break;
}
case UIGestureRecognizerStateEnded: {
//結(jié)束移動(dòng)
[self.colView endInteractiveMovement];
break;
}
default: [self.colView cancelInteractiveMovement];
break;
}
}
3.實(shí)現(xiàn)蘋果官方的代理方法
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"%zd---%zd",sourceIndexPath.row,destinationIndexPath.row);
NSIndexPath *selectIndexPath = [self.colView indexPathForItemAtPoint:[_longPress locationInView:self.colView]];
//交換數(shù)據(jù)源的內(nèi)容
[self.arr exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
// [self.colView reloadData];
NSLog(@"%@",self.arr);
}
實(shí)現(xiàn)完以上的方法肆饶,可以快速構(gòu)建一個(gè)可拖拽排序的cell界面。