2017-12-04 2 views
0

longPressジェスチャーをcollectionViewに追加し、システムAPIを使用してコレクションの並べ替えを実装します。ここに私のコードです:セルを一番下または上にドラッグすると、 `collectionView`の並べ替えが中止されます。

CGPoint point = [sender locationInView:_collectionView]; 
NSIndexPath *indexPath = [_collectionView indexPathForItemAtPoint:point]; 
if (!indexPath) { 
    return; 
} 
DragableCollectionViewCell *tempCell = (DragableCollectionViewCell *)[_collectionView cellForItemAtIndexPath:indexPath]; 

switch (sender.state) { 
    case UIGestureRecognizerStateBegan: { 

     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); 

     [tempCell showDeletButton]; 
     [tempCell.deleteButton zkj_addEventHandler:^(id sender) { 
      [tempCell hideDeleteButton]; 
      [self.dataSource[indexPath.section] removeObjectAtIndex:indexPath.item]; 
      [self.collectionView deleteItemsAtIndexPaths:@[indexPath]]; 
     } withControlEvents:UIControlEventTouchUpInside]; 

     [_collectionView beginInteractiveMovementForItemAtIndexPath:indexPath]; 
    } 
     break; 
    case UIGestureRecognizerStateChanged: { 
     [_collectionView updateInteractiveMovementTargetPosition:point]; 
    } 
     break; 
    case UIGestureRecognizerStateEnded: { 
     [_collectionView endInteractiveMovement]; 
    } 
     break; 

    default: 
     [_collectionView cancelInteractiveMovement]; 
     break; 
} 

と私は正しくcollectionViewのデータソースとデリゲートを実装しました。その後、それは動作します、私はドラッグセルを移動し、並べ替えcollectionViewを並べ替えることができます。しかし、セルを下または上にドラッグすると、セルは下または上に停止し、collectionViewは自動的に並べ替えられません。 IOS-11

答えて

0

あなたはドラッグを使用してUICollectionViewを並べ替えるため APIをドロップすることができます。 カスタム実装よりもはるかに柔軟で実装が簡単です。 self.installsStandardGestureForInteractiveMovement = false:あなたはUICollectionViewControllerを使用する場合は、カスタムジェスチャー認識の設定を可能にしなければならない、あなたのジェスチャー認識を確認してください https://medium.com/@p.gpt10/drag-it-drop-it-in-collection-table-ios-11-6bd28795b313

+0

私はあなたのリンクを開くことができません。 「UICollectionViewで並べ替えのためのドラッグ&ドロップAPI」の詳細を知る方法を教えてください。私はカスタム実装を使用していませんでした.iOS-9 @PGDevでAPIを使用しています –

+0

リンクはうまく動作しています。あなたはリンクをクリックした後何を得ていますか? – PGDev

+0

https://medium.com/@p.gpt10/drag-it-drop-it-in-collection-table-ios-11-6bd28795b313 – PGDev

関連する問題