长按并拖动UICollectionView多项选择

时间:2015-09-14 19:06:46

标签: ios objective-c uicollectionview uigesturerecognizer uicollectionviewcell

我试图通过长按单元格在UICollectionView中选择多个单元格,然后拖动到其他单元格来选择它们。

我已经成功地使用Pan Gesture Recognizer来定位单元并调用方法来选择它:

- (void) didPanToSelectCells:(UIPanGestureRecognizer*) panGesture{
    if (!_collectionView.selectionMode){
        [self.collectionView setScrollEnabled:YES];
        return;
    }else{
        if (panGesture.state == UIGestureRecognizerStateBegan){
            [self.collectionView setUserInteractionEnabled:NO];
            [self.collectionView setScrollEnabled:NO];

        }else if (panGesture.state == UIGestureRecognizerStateChanged){
            CGPoint location = [panGesture locationInView:self.collectionView];

            NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:location];

            if (![_collectionView.selectedIndexes containsObject:@(indexPath.row)]){
                [self.collectionView selectCell:[self.collectionView cellForItemAtIndexPath:indexPath] atIndexPath:indexPath selected:YES];
            }
        }else if (panGesture.state == UIGestureRecognizerStateEnded){
            [self.collectionView setScrollEnabled:YES];
            [self.collectionView setUserInteractionEnabled:YES];
        }
    }
}

然而,当我打电话时:

[self.collectionView selectCell:[self.collectionView cellForItemAtIndexPath:indexPath] atIndexPath:indexPath selected:YES];

什么都没发生! 我可以用另一种方法调用它并且它可以精细,但是当从手势识别器调用时它不会做任何事情

我添加了以下手势识别器代理方法,但无济于事:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

[gestureRecognizer setCancelsTouchesInView:NO];

是否有人知道正确执行此操作的方法?

谢谢!

0 个答案:

没有答案