iOS UICollectionView高級用法(長按自由移動cell)-新

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效果


更多效果點我

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,251評論 4 61
  • 一 起初想起寫這篇文章的原因,是因為看到了一則關于岳云鵬的采訪視頻。那是個在演播室里進行的...
    安公閱讀 2,484評論 31 42
  • 在連續熱了大半個月之后,雨總算想起眷顧這座城市。熱,是過去二十多天里唯一的感受。夜里在風扇呼啦啦的聲音里沉睡,早上...
    分分洋閱讀 210評論 0 0
  • 有一件事令我感到沮喪:我發現自己對文字的欲望正在消亡。 在此之前,每逢年末,我都能洋洋灑灑地揮就一篇自我總結,悉數...
    XDash閱讀 711評論 0 15
  • 前陣子沸沸揚揚的校園欺凌事件,在網絡上發酵的異常厲害。輿論的壓力幾乎一邊倒,矛頭指向欺凌者,被欺凌者是被同情的對象...
    木木晨光閱讀 534評論 5 1