我的问题是:
1.一旦ScrollViewDelegate实现了compositionalLayout,UICollectionView就不会被称为compositionalLayout。
使用flowLayout和UICollectionViewDataSource,scrollView委托将被调用。一旦实现了不同的数据源和CompositionalLayout,就不再调用scrollView委托了。
2. collectionView.decelerationRate = .fast在实现CompositionalLayout时被忽略了
我的理解是,UICollectionViewDelegate应该打电话给UIScrollViewDelegate,我看上去很广很远,但没有运气。有人能指出我错过了什么吗?我是不是把compositionalLayout集成错了?
这是我的代码:
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UIScrollViewDelegate {
enum Section {
case weekdayHeader
}
@IBOutlet weak var collectionView: UICollectionView!
let weekdays = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
var weekdaysArr = [Weekdays]()
var dataSource: UICollectionViewDiffableDataSource<Section, Weekdays>! = nil
private let cellReuseIdentifier = "myCell"
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.delegate = self
self.collectionView.register(UINib(nibName: "MyCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cellReuseIdentifier)
self.collectionView.decelerationRate = .normal
configureCollectionView()
configureDataSource()
configureWeekdays()
updateCollectionView()
// Do any additional setup after loading the view.
}
func configureCollectionView(){
self.collectionView.delegate = self
self.collectionView.collectionViewLayout = generateLayout()
}
func generateLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(
widthDimension: .estimated(10),
heightDimension: .fractionalHeight(1))
let weekdayItem = NSCollectionLayoutItem(layoutSize: itemSize)
// weekdayItem.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 50, bottom: 5, trailing: 50)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.3),
heightDimension: .fractionalHeight(1.0))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [weekdayItem])
// let spacing = CGFloat(10)
// group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = CGFloat(20)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 500)
section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}
func configureDataSource() {
dataSource = UICollectionViewDiffableDataSource
<Section, Weekdays>(collectionView: collectionView)
{ (collectionView: UICollectionView, indexPath: IndexPath, weekday: Weekdays) -> UICollectionViewCell? in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: self.cellReuseIdentifier, for: indexPath) as? MyCollectionViewCell else {
fatalError("Cannot create a new cell") }
cell.titleLabel.text = weekday.name
print("weekday.name = \(weekday.name)")
return cell
}
// let snapshot = snapshotForCurrentState()
// dataSource.apply(snapshot, animatingDifferences: false)
}
func configureWeekdays(){
weekdaysArr.append(Weekdays(name: "Monday"))
weekdaysArr.append(Weekdays(name: "Tuesday"))
weekdaysArr.append(Weekdays(name: "Wednesday"))
weekdaysArr.append(Weekdays(name: "Thursday"))
weekdaysArr.append(Weekdays(name: "Friday"))
weekdaysArr.append(Weekdays(name: "Saturday"))
weekdaysArr.append(Weekdays(name: "Sunday"))
}
func updateCollectionView(){
var snapshot = NSDiffableDataSourceSnapshot<Section, Weekdays>()
snapshot.appendSections([.weekdayHeader])
snapshot.appendItems(weekdaysArr, toSection: .weekdayHeader)
dataSource.apply(snapshot, animatingDifferences: false)
}
//MARK: Scroll View Delegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
print("ScrollView decel rate: \(scrollView.decelerationRate)")
}
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
print("Begin")
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
print("End")
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
print("END")
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("Did select a cell here")
}
}
struct Weekdays: Hashable {
let identifier: UUID = UUID()
let name: String
func hash(into hasher: inout Hasher){
return hasher.combine(identifier)
}
static func == (lhs: Weekdays, rhs: Weekdays) -> Bool {
return lhs.identifier == rhs.identifier
}
}谢谢
编辑:更新代码-
编辑:与来自WWDC的苹果示例代码相比,我发现了这种行为.
如果我使用示例代码网格布局
func gridLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.2),
heightDimension: .fractionalHeight(1.0))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1.0),
heightDimension: .fractionalWidth(0.2))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,
subitems: [item])
let section = NSCollectionLayoutSection(group: group)
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}当只有7个条目时,Scroll方法永远不会被调用。
不过,我仍然可以滚动,所期望的行为应该是,滚动委托方法确实会被调用,因为我有垂直弹出。
但是,当有大约40个条目并且内容明显超出屏幕大小时,滚动委托就会被调用。
现在,当加载我自己的布局时,很有趣:
func generateLayout() -> UICollectionViewLayout {
let itemSize = NSCollectionLayoutSize(
widthDimension: .estimated(10),
heightDimension: .fractionalHeight(1))
let weekdayItem = NSCollectionLayoutItem(layoutSize: itemSize)
// weekdayItem.contentInsets = NSDirectionalEdgeInsets(top: 5, leading: 50, bottom: 5, trailing: 50)
let groupSize = NSCollectionLayoutSize(
widthDimension: .fractionalWidth(0.3),
heightDimension: .fractionalHeight(1.0))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [weekdayItem])
// let spacing = CGFloat(10)
// group.interItemSpacing = .fixed(spacing)
let section = NSCollectionLayoutSection(group: group)
section.interGroupSpacing = CGFloat(20)
section.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 500)
section.orthogonalScrollingBehavior = .continuousGroupLeadingBoundary
let layout = UICollectionViewCompositionalLayout(section: section)
return layout
}卷轴代表们从来没有接到过电话。即使使用40+项。
对于如何强迫UICollectionView与其ScrollViewDelegates通信,有什么想法吗?
发布于 2020-01-21 22:42:10
答案是在 (NSCollectionLayoutSection)节上使用 visibleItemsInvalidationHandler 闭包。
每当事件导致动画时,此处理程序将接收更新。它的操作类似于scrollViewDidScroll,并在水平滚动组上传递所有项、滚动视图偏移量和NSCollectionLayoutEnvironment。
示例:
section.visibleItemsInvalidationHandler = { [weak self] visibleItems, point, environment in
self?.currentIndex = visibleItems.last?.indexPath.row
}发布于 2020-05-20 11:28:52
代码片段:
section.visibleItemsInvalidationHandler = { [weak self] visibleItems, point, environment in
self?.pager.currentPage = visibleItems.last!.indexPath.row
}发布于 2020-01-10 10:00:33
似乎是UICollectionView组合布局中水平滚动组的意外行为。
以防其他人遇到同样的问题。这似乎是一个横向滚动组合布局组的问题。
只有垂直滚动组才会触发UIScrollViewDelegates。
不幸的是,这也意味着decelerationrate.fast似乎不能应用于水平滚动。这只会被忽视。
复制问题和分析的步骤
这种行为可以通过在wwdc示例代码"Advancements Layout“中实现UIScrollViewDelegate方法来重新创建,在这里:类中的https://developer.apple.com/videos/play/wwdc2019/215/:我们可以找到水平和垂直滚动组。
结论
UIScrollViewDelegate只与垂直滚动组交互。水平滚动组不与集合视图的滚动委托通信。
如果需要水平滚动组中的滚动委托方法,则仍然需要使用嵌套的CollectionViews和FlowLayout /自定义布局的传统方法。
备注
如果有人能告诉我,我错过了什么东西,我会非常感激,直到那时,这将作为我上述问题的答案。
感谢所有发表评论的人。
https://stackoverflow.com/questions/59674538
复制相似问题