在启用本机分页的UICollectionView中查看上一个/下一个单元格?

问题描述 投票:2回答:2

我想要一个集合视图来遍历单元格并居中,但是显示上一个和下一个单元格的一部分,如下所示:

enter image description here

有大量的黑客攻击,但我想用UICollectionView的原生分页属性实现这一点。使单元格成为集合视图的整个宽度不会显示上一个/下一个单元格,并且使单元格宽度更小不会在分页时捕捉到中心。

例如,是否可以使集合视图占屏幕宽度的80%,并让前一个/下一个单元格在边界外渗出(没有剪辑到边界)?

enter image description here

或者使用本机分页实现此目的的任何其他想法?

ios uicollectionview uicollectionviewlayout
2个回答
0
投票

我认为在启用本机分页的情况下,有一种简单的方法可以做到这一点。但是,您可以使用scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)轻松进行自定义分页,以设置所需的目标。通过添加一些逻辑,您可以创建分页。你可以找到herehere的例子


1
投票

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方法。

Screenshot

有关更详细的参考Click Here

© www.soinside.com 2019 - 2024. All rights reserved.