gpt4 book ai didi

ios - 设置 UICollectionViewCompositionalLayout 时未调用 UIScrollView 委托(delegate)方法

转载 作者:行者123 更新时间:2023-12-01 15:24:32 36 4
gpt4 key购买 nike

我目前有一个 UICollectionView使用 UICollectionViewCompositionalLayout .我想在滚动/滚动停止时为当前可见单元格中的一些 View 设置动画。

不幸的是,它似乎在设置 orthogonalScrollingBehavior除了 .none 之外的任何部分劫持UICollectionView随行 UIScrollView委托(delegate)方法。

想知道是否有任何当前的解决方法? 获取分页行为和UIScrollView代表?

设置布局

  enum Section {
case main
}

override func awakeFromNib() {
super.awakeFromNib()
collectionView.collectionViewLayout = createLayout()
collectionView.delegate = self
}

func configure() {
snapshot.appendSections([.main])
snapshot.appendItems(Array(0..<10))
dataSource.apply(snapshot, animatingDifferences: false)
}

private func createLayout() -> UICollectionViewLayout {
let leadingItem = NSCollectionLayoutItem(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0))
)

leadingItem.contentInsets = .zero

let containerGroup = NSCollectionLayoutGroup.horizontal(
layoutSize: NSCollectionLayoutSize(
widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalHeight(1.0)
),
subitems: [leadingItem])

let section = NSCollectionLayoutSection(group: containerGroup)
section.orthogonalScrollingBehavior = .groupPaging // WOULD LIKE PAGING & UISCROLLVIEW TO ALSO BE FIRED

let config = UICollectionViewCompositionalLayoutConfiguration()
config.scrollDirection = .horizontal

let layout = UICollectionViewCompositionalLayout(section: section, configuration: config)
return layout
}

UICollectionViewDelegate
extension SlidingCardView: UICollectionViewDelegate {

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// THIS IS FIRED BUT UISCROLLVIEW METHODS NOT
}

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print(111)
}


func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("1111111")
}
}

最佳答案

设置orthogonalScrollingBehavior到一个部分,嵌入一个内部 _UICollectionViewOrthogonalScrollerEmbeddedScrollView它处理部分中的滚动。这个内部 ScrollView 作为 subview 添加到您的 Collection View 中。

当您将自己设置为 delegate在您的收藏 View 中,您应该会收到 ScrollView 委托(delegate)回调 但仅限 对于主 Collection View ,它在部分之间滚动,而不是在部分中的项目之间滚动。由于内部 ScrollView (也可能是 collectionViews,不确定)是完全不同的实例,并且您没有将自己设置为它们的委托(delegate),因此您不会收到它们的回调。

据我所知,不应该有官方方式从处理部分滚动的内部 ScrollView 接收这些回调。

但是如果你很好奇并且想尝试一下,你可以使用这个'hacked' collectionView 类:

import UIKit

final class OrtogonalScrollingCollectionView: UICollectionView {

override var delegate: UICollectionViewDelegate? {
get { super.delegate }
set {
super.delegate = newValue
subviews.forEach { (view) in
guard String(describing: type(of: view)) == "_UICollectionViewOrthogonalScrollerEmbeddedScrollView" else { return }
guard let scrollView = view as? UIScrollView else { return }
scrollView.delegate = newValue
}
}
}
}

这会将您的委托(delegate)设置为正交部分附带的所有内部 ScrollView 。你不应该在生产环境中使用它,因为不能保证 Apple 会保持 Collection View 的内部工作方式相同,所以这个 hack 将来可能无法工作,另外你可能会因为在 UIKit 中使用私有(private) API 而被拒绝当您提交构建以供发布时。

关于ios - 设置 UICollectionViewCompositionalLayout 时未调用 UIScrollView 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58791121/

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