gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:29:03 27 4
gpt4 key购买 nike

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

尝试了一些简单的事情:

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

但这似乎只调用了第一个被触摸的单元格上的方法。

有什么想法吗?

最佳答案

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
}

关于ios - UICollectionView 将手指拖过单元格以选择它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27390728/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com