【发布时间】:2020-05-01 00:38:00
【问题描述】:
我看到很多帖子都解决了这个问题,但是,我在实现垂直集合视图的解决方案时遇到了麻烦。
我的集合视图的单元格填充了集合视图的整个宽度,但没有填满集合视图的整个高度,因此无法正常分页。我试图在使用自定义UICollectionViewFlowLayout 滚动时将单元格中心捕捉到屏幕中心。
(类似于 Instagram 提要,但没有“免费”滚动并且帖子垂直居中)
class FeedLayout: UICollectionViewFlowLayout {
private var previousOffset: CGFloat = 0
private var currentPage: Int = 0
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView else {
return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
}
let itemsCount = collectionView.numberOfItems(inSection: 0)
if previousOffset > collectionView.contentOffset.y {
currentPage = max(currentPage - 1, 0)
} else if previousOffset < collectionView.contentOffset.y {
currentPage = min(currentPage + 1, itemsCount - 1)
}
let updatedOffset = ((collectionView.frame.height * 0.75) + minimumLineSpacing) * CGFloat(currentPage)
previousOffset = updatedOffset
return CGPoint(x: proposedContentOffset.x, y: updatedOffset)
}
}
【问题讨论】:
标签: ios swift uicollectionview uikit uicollectionviewlayout