Nic*_*ard 7 objective-c uikit uigesturerecognizer ios uicollectionview
使用UICollectionView,是否可以通过将手指拖过其中几个来选择多个单元格?例如,如果您将手指拖过6行,然后向下拖动到下一行,则会选择所有这些行.
尝试过简单的事情:
UISwipeGestureRecognizer *swipeGuesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.collectionView addGestureRecognizer:swipeGuesture];
Run Code Online (Sandbox Code Playgroud)
但这似乎只是在触及的第一个细胞上调用该方法.
有任何想法吗?
小智 8
迅速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
}
Run Code Online (Sandbox Code Playgroud)
小智 3
您可以使用 UIPanGestureRecognizer。并根据平移事件的位置,跟踪经过哪些单元格。当手势结束时,您将拥有一组选定的单元格。
确保将其cancelsTouchesInView设置为NO。您需要设置委托并gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:实施gestureRecognizerShouldBegin以确保 CollectionView 仍然可以滚动
| 归档时间: |
|
| 查看次数: |
4030 次 |
| 最近记录: |