我想要一个集合视图来遍历单元格并居中,但是显示上一个和下一个单元格的一部分,如下所示:
有大量的黑客攻击,但我想用UICollectionView
的原生分页属性实现这一点。使单元格成为集合视图的整个宽度不会显示上一个/下一个单元格,并且使单元格宽度更小不会在分页时捕捉到中心。
例如,是否可以使集合视图占屏幕宽度的80%,并让前一个/下一个单元格在边界外渗出(没有剪辑到边界)?
或者使用本机分页实现此目的的任何其他想法?
iOS Swift 4
使用以下两种方法来简化上一屏和下一屏。
private func calculateSectionInset() -> CGFloat {
let deviceIsIpad = UIDevice.current.userInterfaceIdiom == .pad
let deviceOrientationIsLandscape = UIDevice.current.orientation.isLandscape
let cellBodyViewIsExpended = deviceIsIpad || deviceOrientationIsLandscape
let cellBodyWidth: CGFloat = 236 + (cellBodyViewIsExpended ? 174 : 0)
let buttonWidth: CGFloat = 50
let inset = (collectionFlowLayout.collectionView!.frame.width - cellBodyWidth + buttonWidth) / 4
return inset
}
private func configureCollectionViewLayoutItemSize() {
let inset: CGFloat = calculateSectionInset() // This inset calculation is some magic so the next and the previous cells will peek from the sides. Don't worry about it
collectionFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: inset, bottom: 0, right: inset)
collectionFlowLayout.itemSize = CGSize(width: collectionFlowLayout.collectionView!.frame.size.width - inset * 2, height: collectionFlowLayout.collectionView!.frame.size.height)
}
不要忘记在你的configureCollectionViewLayoutItemSize()
的viewDidLayoutSubviews()
中调用UIViewController
方法。
有关更详细的参考Click Here