溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS9中collectionView新特性怎么用

發(fā)布時間:2021-12-24 15:15:48 來源:億速云 閱讀:137 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“iOS9中collectionView新特性怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“iOS9中collectionView新特性怎么用”這篇文章吧。

具體用法:

1.創(chuàng)建collectionView并設(shè)置代理

- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(50, 50);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = 10;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        _collectionView.backgroundColor = [UIColor cyanColor];
        _collectionView.dataSource = self;
       //此處給其增加長按手勢,用此手勢觸發(fā)cell移動效果
        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];
        [_collectionView addGestureRecognizer:longGesture];
    }
    return _collectionView;
}

2.設(shè)置其資源

_dataSource = [NSMutableArray array];
    for (int i = 1; i <= 50; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i];
        [_dataSource addObject:imageName];
    }

3.監(jiān)聽手勢,并設(shè)置其允許移動cell和交換資源

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {
    //判斷手勢狀態(tài)
    switch (longGesture.state) {
        case UIGestureRecognizerStateBegan:{
            //判斷手勢落點位置是否在路徑上
            NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:[longGesture locationInView:self.collectionView]];
            if (indexPath == nil) {
                break;
            }
            //在路徑上則開始移動該路徑上的cell
            [self.collectionView beginInteractiveMovementForItemAtIndexPath:indexPath];
        }
            break;
        case UIGestureRecognizerStateChanged:
            //移動過程當中隨時更新cell位置
            [self.collectionView updateInteractiveMovementTargetPosition:[longGesture locationInView:self.collectionView]];
            break;
        case UIGestureRecognizerStateEnded:
            //移動結(jié)束后關(guān)閉cell移動
            [self.collectionView endInteractiveMovement];
            break;
        default:
            [self.collectionView cancelInteractiveMovement];
            break;
    }
}

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath{
    //返回YES允許其item移動
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath {
    //取出源item數(shù)據(jù)
    id objc = [_dataSource objectAtIndex:sourceIndexPath.item];
    //從資源數(shù)組中移除該數(shù)據(jù)
    [_dataSource removeObject:objc];
    //將數(shù)據(jù)插入到資源數(shù)組中的目標位置上
    [_dataSource insertObject:objc atIndex:destinationIndexPath.item];
}

通過以上設(shè)置便可以成功移動cell了,下面奉上效果圖

iOS9中collectionView新特性怎么用

以上是“iOS9中collectionView新特性怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI