使中心单元处于分页模式

时间:2017-03-08 09:08:10

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我使用UICollectionView创建了horizontal并启用了paging模式。现在我想让屏幕上的每个单元格能够看到下一个/上一张幻灯片的边缘。

这是我的UICollectionView

let collectionViewCardsSlider: UICollectionView = {

        let collectionMargin = CGFloat(16)
        let itemSpacing = CGFloat(10)
        let itemHeight = CGFloat(168)
        var itemWidth =  UIScreen.main.bounds.width - collectionMargin * 2.0

        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = itemSpacing
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
        layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0)
        layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0)

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.decelerationRate = UIScrollViewDecelerationRateFast
        collectionView.backgroundColor = .clear
        return collectionView
    }()

这是预览:

enter image description here

正如您只能看到第一个,最后一个项目位于中心。但我想要的东西是这样的: enter image description here

我如何实现这一点以及我的代码中出了什么问题?

非常感谢。

1 个答案:

答案 0 :(得分:1)

尝试更改FlowLayout的类并禁用分页:

let collectionViewCardsSlider: UICollectionView = {

    let collectionMargin = CGFloat(16)
    let itemSpacing = CGFloat(10)
    let itemHeight = CGFloat(168)
    var itemWidth =  UIScreen.main.bounds.width - collectionMargin * 2.0

    let layout = CenterCellCollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumLineSpacing = itemSpacing
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.itemSize = CGSize(width: itemWidth, height: itemHeight)
    layout.headerReferenceSize = CGSize(width: collectionMargin, height: 0)
    layout.footerReferenceSize = CGSize(width: collectionMargin, height: 0)

    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.isPagingEnabled = false
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.decelerationRate = UIScrollViewDecelerationRateFast
    collectionView.backgroundColor = .clear
    return collectionView
}()

class CenterCellCollectionViewFlowLayout: UICollectionViewFlowLayout {

    var mostRecentOffset : CGPoint = CGPoint()

    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {

        if velocity.x == 0 {
            return mostRecentOffset
        }

        if let cv = self.collectionView {

            let cvBounds = cv.bounds
            let halfWidth = cvBounds.size.width * 0.5;


            if let attributesForVisibleCells = self.layoutAttributesForElements(in: cvBounds) {

                var candidateAttributes : UICollectionViewLayoutAttributes?
                for attributes in attributesForVisibleCells {

                    // == Skip comparison with non-cell items (headers and footers) == //
                    if attributes.representedElementCategory != UICollectionElementCategory.cell {
                        continue
                    }

                    if (attributes.center.x == 0) || (attributes.center.x > (cv.contentOffset.x + halfWidth) && velocity.x < 0) {
                        continue
                    }
                    candidateAttributes = attributes 
                }

                // Beautification step , I don't know why it works!
                if(proposedContentOffset.x == -(cv.contentInset.left)) {
                    return proposedContentOffset
                }

                guard let _ = candidateAttributes else {
                    return mostRecentOffset
                }
                mostRecentOffset = CGPoint(x: floor(candidateAttributes!.center.x - halfWidth), y: proposedContentOffset.y)
                return mostRecentOffset

            }
        }

        // fallback
        mostRecentOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
        return mostRecentOffset
    }


}