iOS9 collectionView新特性

來源:互聯網
上載者:User

標籤:

  近日因為系統升級導致xcode6.系列版本出現bug,於是開始使用xcode7。在使用之餘突然想到collectionView在iOS9中發布了一個可以移動cell的新特性,就嘗試著將其實現,無奈api文檔介面無法查看,只有一些列的api放在那裡。於是上網尋找,發現國內沒有搜尋到此類文章,於是FQ繼續找,最終找到的竟然都是swift版本,於是將其轉換為oc版本以協助國內需要的朋友學習使用。下面是具體用法:

1.建立collectionView並設定代理

- (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;       //此處給其增加長按手勢,用此手勢觸發cell移動效果        UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];        [_collectionView addGestureRecognizer:longGesture];    }    return _collectionView;}

2.設定其資源

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

3.監聽手勢,並設定其允許移動cell和交換資源

- (void)handlelongGesture:(UILongPressGestureRecognizer *)longGesture {    //判斷手勢狀態    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:            //移動結束後關閉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和目標位置item的資源位置    [_dataSource exchangeObjectAtIndex:sourceIndexPath.item withObjectAtIndex:destinationIndexPath.item];} 

通過以上設定便可以成功移動cell了,下面奉上

至此collectionView的新特性使用方法展示完成,如其中有何錯誤之處望之處,謝謝!

 

iOS9 collectionView新特性

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.