gpt4 book ai didi

ios - UISwipeGestureRecognizer 和 CollectionView 滚动到下一个项目 indexPath

转载 作者:行者123 更新时间:2023-11-28 06:24:53 24 4
gpt4 key购买 nike

所以我有一个卡片 Collection View ,我想要的是当我向左滑动时我希望下一个项目在中间居中,这不适用于启用分页

enter image description here

因此,当我向左滑动时,我希望下一张卡片居中,这不适用于正常行为,因为我只想滑动一张卡片。

所以我添加了 UISwipeGestureRecognizer在 Collection View 上禁用滚动

类 ViewController: UIViewController,UICollectionViewDataSource {

@IBOutlet var joke_cards: UICollectionView!

override func viewDidLoad() {

super.viewDidLoad();


//Add gestures
let leftSwipeGest = UISwipeGestureRecognizer(target: self, action: #selector(funcForGesture))
leftSwipeGest.direction = .left
joke_cards.addGestureRecognizer(leftSwipeGest)

}

func funcForGesture(sender: UISwipeGestureRecognizer){
if sender.direction == .left {
//scroll to next item
}
}

现在我的问题是如何滚动到下一个项目?因为我不知道 indexPath?因为如果我想使用这个 self.joke_cards.scrollToItem(at: indexPath, at: .centeredHorizo​​ntally, animated: true),我需要索引路径,所以我想我需要弄清楚 View 上的当前 indexPath 并在用户滑动时添加一个

有什么建议吗?

//已更新所以我设法让它工作,但它只在我第一次滑动时才有效:

func funcForGesture(sender: UISwipeGestureRecognizer){
if sender.direction == .left {
//scroll to next item
let cellItems = self.joke_cards.indexPathsForVisibleItems
let next = cellItems[0] as! IndexPath

self.joke_cards.scrollToItem(at: next, at: .centeredHorizontally, animated: true)
}
}

最佳答案

一个解决方案:

1:将 NSIndexPath 属性添加到您的单元格中,并将 indexPath 设置为您的 cellForItem 方法中的属性。

2: 获取 array of visible cells.

3:Get the rect of the visible单元格并计算它们在屏幕上的位置,您还可以从单元格属性中获取正确的 indexPath。

第二种方案:

1:添加一个UILongPressGestureRecognizer到您的单元格内容查看

2:设置minimumPressDuration到 0。

3:添加委托(delegate)方法来触发 acitons or this.

第三种方案:

1:保持滑动手势。

2:And use this method (与选项 2 相同)indexPathForItemAtPoint:

关于ios - UISwipeGestureRecognizer 和 CollectionView 滚动到下一个项目 indexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42246876/

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