[![collectionview example] [1]] [1]
我试图使用集合视图模拟此行为.我已经开始使用这种方法,它让我更接近.虽然当我在水平分页CollectionView上进一步向右滑动时,内容会进一步向左移动.电池之间的间距也是关闭的.我认为它需要自定义布局,但我不确定.
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width - 20, height: collectionView.frame.height - 20)
}
Run Code Online (Sandbox Code Playgroud) 我在我的集合视图上启用了分页,并遇到了每次滑动的第一个问题,不是在不同的单元格上停止,而是在页面上停止。
然后我尝试在 ScrollViewWillEndDecelerating 中输入代码并手动处理分页。但是我的问题是更改集合视图 ContentOffset 或滚动到 Point 都不起作用,除非在 LayoutSubiews 中调用。这是它们影响 CollectionView 的唯一地方
我的集合视图中的每个单元格都是全屏的。我处理布局子视图中的单元格大小。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if let layout = customCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
let itemWidth = view.frame.width
let itemHeight = view.frame.height
layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
layout.invalidateLayout()
}
let ip = IndexPath(row: 2, section: 0)
view.layoutIfNeeded()
customCollectionView.scrollToItem(at: ip, at: UICollectionViewScrollPosition.centeredVertically, animated: true)
let point = CGPoint(x: 50, y: 600)
customCollectionView.setContentOffset(point, animated: true)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let pageHeight = …Run Code Online (Sandbox Code Playgroud)