這里僅僅只是記錄一下使用collectionView實現(xiàn)cell的可拖動改變位置。
效果圖:
Simulator Screen Shot - iPhone 8 - 2022-11-16 at 16.13.14.png
這里選擇使用collectionview來實現(xiàn)腿椎。
- collectionViewCell實現(xiàn)
- collectionView布局思灌、數(shù)據(jù)源設(shè)置
- collectionView代理實現(xiàn)
- collectionView添加長按手勢及手勢處理
collectionViewCell實現(xiàn)
繼承collectionViewCell照激,可以使用xib實現(xiàn),也可使用純代碼實現(xiàn)铃拇。這里使用xib實現(xiàn)
HmCollectionViewCell
collectionView布局钞瀑、數(shù)據(jù)源設(shè)置
collectionView布局
在繼承自UIViewController的VC中聲明collectionView變量、實現(xiàn)懶加載和添加到vc的view上
// collectionView變量聲明
@property (nonatomic, strong) UICollectionView *collectionView;
// collectionView添加到view上
[self.view addSubview:self.collectionView];
// collectionView懶加載
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(100, 100);
layout.minimumLineSpacing = 3;
layout.minimumInteritemSpacing = 3;
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 306, 306) collectionViewLayout:layout];
_collectionView.center = self.view.center;
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = UIColor.whiteColor;
[_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HmCollectionViewCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
}
return _collectionView;
}
數(shù)據(jù)源設(shè)置
因為涉及到cell的拖動和位置改變慷荔,所以數(shù)據(jù)源應(yīng)該使用可變數(shù)組來承載數(shù)據(jù)雕什。
// 聲明
@property (nonatomic, strong) NSMutableArray *data;
// 懶加載
- (NSMutableArray *)data {
if (!_data) {
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < 9; i ++) {
[arr addObject:[NSNumber numberWithInt:(i + 1)]];
}
_data = arr;
}
return _data;
}
collectionView代理實現(xiàn)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.data.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
HmCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.nameLabel.text = [NSString stringWithFormat:@"%@", self.data[indexPath.row]];
return cell;
}
// 只有實現(xiàn)這個代理并返回YES,cell才可移動
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
id selected = self.data[sourceIndexPath.row];
[self.data removeObject:selected];
[self.data insertObject:selected atIndex:destinationIndexPath.row];
}
collectionView添加長按手勢及手勢處理
長按手勢添加在collectionView上显晶,通過手指長按的位置進行轉(zhuǎn)換至對應(yīng)的cell上贷岸,然后進行響應(yīng)的處理。
// 添加長按手勢
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
[self.collectionView addGestureRecognizer:longPress];
// 長按方法實現(xiàn)
- (void)handlelongGesture:(UIGestureRecognizer *)longPress {
switch (longPress.state) {
case UIGestureRecognizerStateBegan:
{ //手勢開始
//判斷手勢落點位置是否在row上
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longPress locationInView:self.collectionView]];
if (indexPath == nil) {
break;
}
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
[self.view bringSubviewToFront:cell];
cell.backgroundColor = [UIColor colorWithRed:1.0 green:0.2 blue:0.1 alpha:0.5];
//iOS9方法 移動cell
[self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
}
break;
case UIGestureRecognizerStateChanged:
{ // 手勢改變
// iOS9方法 移動過程中隨時更新cell位置
[self.collectionView updateInteractiveMovementTargetPosition:[longPress locationInView:self.collectionView]];
}
break;
case UIGestureRecognizerStateEnded:
{ // 手勢結(jié)束
// iOS9方法 移動結(jié)束后關(guān)閉cell移動
[self.collectionView endInteractiveMovement];
}
break;
default: //手勢其他狀態(tài)
[self.collectionView cancelInteractiveMovement];
break;
}
}
總結(jié)
至此整個collectionView的可移動操作設(shè)置結(jié)束磷雇。其中最重要的點在于實現(xiàn)collectionView的響應(yīng)代理偿警,尤其是可移動代理的實現(xiàn)。
這里僅僅做個記錄唯笙,方便以后使用到的時候查找螟蒸。