gpt4 book ai didi

ios - 为什么 scrollViewWillEndDragging 会影响 UICollectionView 和 UITableView?

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:39 25 4
gpt4 key购买 nike

我在同一个 View Controller 上使用 UITableView 和 UICollectionView。

我想改变 UICollectionView 的滚动方式,所以我在扩展中添加了一个 scrollViewWillBeginDragging 和一个 scrollViewWillEndDragging 函数。

但是,scrollViewWillBeginDraggingscrollViewWillEndDragging 也会影响 UITableView,尽管它们不在同一个扩展中。

我该如何解决这个问题?有没有办法只选择 UICollectionView?

这是我的代码的简短版本:

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// This is where I put all of the UICollectionView code, including `scrollViewWillBeginDragging` and a `scrollViewWillEndDragging`

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
// Why is the code in here affecting the UITableView?

}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Same as with `scrollViewWillBeginDragging`

}

}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
// This is where I put all of the UITableView code, it's separate from the UICollectionView so why are `scrollViewWillBeginDragging` and `scrollViewWillEndDragging` affecting it?

}

最佳答案

这是因为

UITableViewDelegate and UICollectionViewDelegate both Inherits From UIScrollViewDelegate

那么你能做的就是

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
// This is where I put all of the UICollectionView code, including `scrollViewWillBeginDragging` and a `scrollViewWillEndDragging`

func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {

if scrollView is UICollectionView {
// Add code here for collectionView
}
}

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

if scrollView is UICollectionView {
// Add code here for collectionView
}
}

}

关于ios - 为什么 scrollViewWillEndDragging 会影响 UICollectionView 和 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55600574/

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