日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS UICollectionViewCell 的拖动

發布時間:2025/7/14 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS UICollectionViewCell 的拖动 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.長按cell的情況下實現拖動,所以理應想到用長按手勢。

2.既然實現移動cell,就要看看UICollectionView 有沒有方法或者協議可以移動的。通過查看UICollectionView的協議方法,可以在UICollectionViewDataSource中看到有兩個方法。

- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath; - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;

?3.OK 知道了以上兩個步驟,那么就可以開始寫代碼了。

// 設置手勢 - (void)setupGesture {UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handlelongGesture:)];[self.collectionView addGestureRecognizer:longPress]; } - (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數據id objc = [_dataSource objectAtIndex:sourceIndexPath.item];//從資源數組中移除該數據 [_dataSource removeObject:objc];//將數據插入到資源數組中的目標位置上 [_dataSource insertObject:objc atIndex:destinationIndexPath.item]; }

注:_dataSource 是你的數據源。

?

轉載于:https://www.cnblogs.com/peaker-wu/p/5359510.html

總結

以上是生活随笔為你收集整理的iOS UICollectionViewCell 的拖动的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。