iOS 9之后: 示例如下
效果
前言: 看完你可以學到哪些呢? 就是文章標題那么多, 只有那么多. . 手殘效果圖沒弄好.
@property (nonatomic, strong) UICollectionView *xtCollectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) CALayer *dotLayer;
@property (nonatomic, assign) CGFloat endPoint_x;
@property (nonatomic, assign) CGFloat endPoint_y;
@property (nonatomic, strong) UIBezierPath *path;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UILongPressGestureRecognizer *longPress;
初始化一個數組
self.array = [NSMutableArray arrayWithObjects:@"紅包", @"轉賬", @"手機充值", @"芝麻信用",
@"天貓", @"生活繳費", @"螞蟻唄", @"世界那么大",
@"余額寶", @"安全快付", @"螞蟻聚寶", @"哈哈",@"紅包1", @"轉賬1", @"手機充值1", @"芝麻信用1",
@"天貓1", @"生活繳費1", @"螞蟻唄1", @"世界那么大1",
@"余額寶1", @"安全快付1", @"螞蟻聚寶1", @"哈哈1", nil];
創建CollectionView
- (UICollectionView *)xtCollectionView
{
if (!_xtCollectionView) {
_flowLayout = [[UICollectionViewFlowLayout alloc] init];
_flowLayout.minimumLineSpacing = 1;
_flowLayout.minimumInteritemSpacing = 1;
_xtCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, Screen_Width, Screen_Height - 20) collectionViewLayout:_flowLayout];
_xtCollectionView.dataSource = self;
_xtCollectionView.backgroundColor = [UIColor colorWithRed:0.8568 green:0.8568 blue:0.8568 alpha:1.0];
_xtCollectionView.delegate = self;
[_xtCollectionView registerClass:[XTCollectCell class] forCellWithReuseIdentifier:@"cellIdentiifer"];
}
return _xtCollectionView;
}
添加一個長按的手勢
_longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lonePressMoving:)];
[self.xtCollectionView addGestureRecognizer:_longPress];
手勢方法的實現
- (void)lonePressMoving:(UILongPressGestureRecognizer *)longPress
{
switch (_longPress.state) {
case UIGestureRecognizerStateBegan: {
{
NSIndexPath *selectIndexPath = [self.xtCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.xtCollectionView]];
// 找到當前的cell
XTCollectCell *cell = (XTCollectCell *)[self.xtCollectionView cellForItemAtIndexPath:selectIndexPath];
// 定義cell的時候btn是隱藏的, 在這里設置為NO
[cell.btnDelete setHidden:NO];
[_xtCollectionView beginInteractiveMovementForItemAtIndexPath:selectIndexPath];
}
break;
}
case UIGestureRecognizerStateChanged: {
[self.xtCollectionView updateInteractiveMovementTargetPosition:[longPress locationInView:_longPress.view]];
break;
}
case UIGestureRecognizerStateEnded: {
[self.xtCollectionView endInteractiveMovement];
break;
}
default: [self.xtCollectionView cancelInteractiveMovement];
break;
}
}
移動方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath
{
NSIndexPath *selectIndexPath = [self.xtCollectionView indexPathForItemAtPoint:[_longPress locationInView:self.xtCollectionView]];
// 找到當前的cell
XTCollectCell *cell = (XTCollectCell *)[self.xtCollectionView cellForItemAtIndexPath:selectIndexPath];
[cell.btnDelete setHidden:YES];
[self.array exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];
[self.xtCollectionView reloadData];
}
效果圖的解釋: collectionView的可編輯狀態是"假的", 只是對數據進行了處理
你可能想知道動畫的實現可以看我的另一篇博客iOS仿美團外賣餓了嗎App點餐動畫
iOS9之前可以參照這個
效果
Github上很早的項目了, 希望對有需要的同學有啟發的作用, 點我下載感謝作者
補充說明: LXReorderableCollectionViewFlowLayout 這個繼承于UICollectionViewFlowLayout So 對于cell的調整是比較隨意像系統的一樣.
比如調整cell的間距你可以這樣.
調整---cell間距
LXReorderableCollectionViewFlowLayout *flowLayout = [[LXReorderableCollectionViewFlowLayout alloc] init];
flowLayout.minimumLineSpacing = ...;
flowLayout.minimumInteritemSpacing = ...;
_collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, s_w, s_h) collectionViewLayout:flowLayout];
搭配下面這個方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(..., ...);
}
您可能想了解我的更多文章, 與我共同成長, 請關注我
帶你系統學習GCD[一]
帶你系統學習GCD[二]
Swift版本仿網易云音樂播放音樂動畫效果
三分鐘教你把代碼托管到Github
Swift 很強大的圖表庫-Charts使用
Swift版仿簡書App淘寶App很友好彈出view效果