UICollectionView将手指拖过单元格以选择它们

时间:2014-12-09 23:20:55

标签: ios objective-c uikit uicollectionview uigesturerecognizer

使用UICollectionView,是否可以通过将手指拖过其中几个来选择多个单元格?例如,如果您将手指拖过6行,然后向下拖动到下一行,它将选择所有这些。

尝试了一些简单的事情:

UISwipeGestureRecognizer *swipeGuesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.collectionView addGestureRecognizer:swipeGuesture];

但这似乎只是在第一个被触摸的细胞上调用该方法。

有什么想法吗?

3 个答案:

答案 0 :(得分:5)

Swift 3:滑动以选择自动滚动和工作滚动。

var selectMode = false
var lastSelectedCell = IndexPath()

func setupCollectionView() {
    collectionView.canCancelContentTouches = false
    collectionView.allowsMultipleSelection = true
    let longpressGesture = UILongPressGestureRecognizer(target: self, action: #selector(didLongpress))
    longpressGesture.minimumPressDuration = 0.15
    longpressGesture.delaysTouchesBegan = true
    longpressGesture.delegate = self
    collectionView.addGestureRecognizer(longpressGesture)

    let panGesture = UIPanGestureRecognizer(target: self, action: #selector(didPan(toSelectCells:)))
    panGesture.delegate = self
    collectionView.addGestureRecognizer(panGesture)
}

func selectCell(_ indexPath: IndexPath, selected: Bool) {
    if let cell = collectionView.cellForItem(at: indexPath) {
        if cell.isSelected {
            collectionView.deselectItem(at: indexPath, animated: true)
            collectionView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.centeredVertically, animated: true)
        } else {
            collectionView.selectItem(at: indexPath, animated: true, scrollPosition: UICollectionViewScrollPosition.centeredVertically)
        }
        if let numberOfSelections = collectionView.indexPathsForSelectedItems?.count {
            title = "\(numberOfSelections) items selected"
        }
    }
}

func didPan(toSelectCells panGesture: UIPanGestureRecognizer) {
    if !selectMode {
        collectionView?.isScrollEnabled = true
        return
    } else {
        if panGesture.state == .began {
            collectionView?.isUserInteractionEnabled = false
            collectionView?.isScrollEnabled = false
        }
        else if panGesture.state == .changed {
            let location: CGPoint = panGesture.location(in: collectionView)
            if let indexPath: IndexPath = collectionView?.indexPathForItem(at: location) {
                if indexPath != lastSelectedCell {
                    self.selectCell(indexPath, selected: true)
                    lastSelectedCell = indexPath
                }
            }
        } else if panGesture.state == .ended {
            collectionView?.isScrollEnabled = true
            collectionView?.isUserInteractionEnabled = true
            swipeSelect = false
        }
    }
}

func didLongpress() {
    swipeSelect = true
}

答案 1 :(得分:2)

您可以使用UIPanGestureRecognizer。并根据平移事件的位置,跟踪通过的细胞。当手势结束时,您将拥有一系列选定的单元格。

确保cancelsTouchesInView设置为NO。您需要设置gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:gestureRecognizerShouldBegin实施的委托,以确保CollectionView仍然可以滚动

答案 2 :(得分:2)

在 iOS 13 及更高版本中,您可以简单地使用内置的多选手势。见Selecting Multiple Items with a Two-Finger Pan Gesture。确保将 collectionView.allowsMultipleSelectionDuringEditing 设置为 true