我有一个水平 Collection View 。本质上,第一个显示的项目是“事件”项目。滑动即可捕捉到每个项目。
我遇到的问题是我需要能够滚动最后一个项目以一直捕捉到前面。目前,当拖动到末尾时,它不再捕捉到第一个单元格。
抱歉,如果我没有任何意义。本质上,每个单元格最左边的框架应该捕捉到 collectionView 的 0,0。目前对于前 2 个单元来说效果非常好。但在那之后它就达到了界限并且不允许它折断。我希望能够拖动并最终的单元格捕捉到 0,0
谢谢!
在这里查看问题:
这是我的布局子类:
class UserFlowLayout: UICollectionViewFlowLayout {
required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) }
override init() {
super.init()
scrollDirection = .horizontal
itemSize = CGSize(width: 70, height: 90)
minimumInteritemSpacing = 15.0
minimumLineSpacing = 15.0
}
override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
guard let collectionView = collectionView else { return super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity) }
var offsetAdjustment = CGFloat.greatestFiniteMagnitude
let horizontalOffset = proposedContentOffset.x + collectionView.contentInset.left
let targetRect = CGRect(x: proposedContentOffset.x, y: 0, width: collectionView.bounds.size.width, height: collectionView.bounds.size.height)
let layoutAttributesArray = super.layoutAttributesForElements(in: targetRect)
layoutAttributesArray?.forEach({ (layoutAttributes) in
let itemOffset = layoutAttributes.frame.origin.x
if fabsf(Float(itemOffset - horizontalOffset)) < fabsf(Float(offsetAdjustment)) {
offsetAdjustment = itemOffset - horizontalOffset
}
})
return CGPoint(x: proposedContentOffset.x + offsetAdjustment, y: proposedContentOffset.y)
}
}
最佳答案
在我看来,它正试图向右滚动得更远。您需要扩大 ScrollView 以包含右侧的空白区域。
您当前的屏幕宽度似乎是 4 个单元格 + X,因此您需要在最后一个单元格后留出 +X 个空白空间。
关于ios - UICollectionView 捕捉到 0,0 - 滚动超过边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46823900/