- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的问题:
1. ScrollViewDelegate 没有被调用 一旦 UICollectionView 实现了 compositionalLayout。
使用 flowLayout 和 UICollectionViewDataSource 调用 scrollView 委托(delegate)。一旦我实现了 diffable 数据源和 CompositionalLayout,就不会再调用 scrollView 委托(delegate)了。
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
}
}
谢谢
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 个项目时,滚动委托(delegate)方法永远不会被调用。
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 多个项目。
最佳答案
答案是使用 visibleItemsInvalidationHandler
关闭 部分(NSCollectionLayoutSection
)。
每当事件导致动画时,此处理程序都会接收更新。它的操作类似于 scrollViewDidScroll
在水平滚动组上,它将传递所有项目、 ScrollView 偏移量和 NSCollectionLayoutEnvironment。
https://developer.apple.com/documentation/uikit/nscollectionlayoutsection/3199096-visibleitemsinvalidationhandler
例子:
section.visibleItemsInvalidationHandler = { [weak self] visibleItems, point, environment in
self?.currentIndex = visibleItems.last?.indexPath.row
}
关于swift - UICollectionView CompositionalLayout 不调用 UIScrollDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59674538/
我正在尝试使用CompositionalLayout创建电影海报的网格。 但是我在细胞高度上挣扎。 问题是当我设置时,我需要每行渲染3个海报。它会自动计算宽度,因此基本上宽度在不同设备上会有所不同。
这是我的问题: 1. ScrollViewDelegate 没有被调用 一旦 UICollectionView 实现了 compositionalLayout。 使用 flowLayout 和 UIC
我是一名优秀的程序员,十分优秀!