【问题标题】:UICollectionView's scrollToItem not calling targetContentOffsetUICollectionView 的 scrollToItem 没有调用 targetContentOffset
【发布时间】:2017-03-16 05:47:15
【问题描述】:
我有一个实现分页的集合视图,这就是为什么我在自定义 UICollectionViewFlowLayout 中覆盖 targetContentOffset 来处理它。当通过用户交互滚动集合视图并且它起作用时,它会被调用。但是,在使用 scrollToItem 或滚动到可见矩形时不会调用它。以编程方式滚动到集合视图的最佳方法是什么,肯定会通过方法targetContentOffset?
【问题讨论】:
标签:
ios
swift
uicollectionview
uicollectionviewlayout
【解决方案1】:
在我的例子中,我使用以下方法以编程方式维护分页和滚动。
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView.contentOffset.y > -topHeaderView.frame.size.height && scrollView.contentOffset.y < -20 - 50 {
let aHeaderHeight = topHeaderView.frame.size.height
if velocity.y <= 0 {
targetContentOffset.memory = CGPoint(x: 0, y: -aHeaderHeight)
} else {
targetContentOffset.memory = CGPoint(x: 0, y: 20 + 50)
}
}
}
希望这会对你有所帮助。