Create a Paging UICollectionView with Swift

后端 未结 2 1433
北荒
北荒 2020-12-16 06:39

I\'m trying to create a UICollectionView with paging and that each item max width is 250 points, I\'ve managed to create it, but I have 2 problems: The first item start not

相关标签:
2条回答
  • 2020-12-16 06:43

    A UICollectionView inherits from UIScrollView. It is possible to achieve paging by setting isPagingEnabled to true.

    0 讨论(0)
  • So I figure out how to do it.

    First create a custom UICollectionViewFlowLayout and add this override this method:

    override func targetContentOffsetForProposedContentOffset(proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
    
        if let cv = self.collectionView {
    
            let cvBounds = cv.bounds
            let halfWidth = cvBounds.size.width * 0.5;
            let proposedContentOffsetCenterX = proposedContentOffset.x + halfWidth;
    
            if let attributesForVisibleCells = self.layoutAttributesForElementsInRect(cvBounds) as? [UICollectionViewLayoutAttributes] {
    
                var candidateAttributes : UICollectionViewLayoutAttributes?
                for attributes in attributesForVisibleCells {
    
                    // == Skip comparison with non-cell items (headers and footers) == //
                    if attributes.representedElementCategory != UICollectionElementCategory.Cell {
                        continue
                    }
    
                    if let candAttrs = candidateAttributes {
    
                        let a = attributes.center.x - proposedContentOffsetCenterX
                        let b = candAttrs.center.x - proposedContentOffsetCenterX
    
                        if fabsf(Float(a)) < fabsf(Float(b)) {
                            candidateAttributes = attributes;
                        }
    
                    }
                    else { // == First time in the loop == //
    
                        candidateAttributes = attributes;
                        continue;
                    }
    
    
                }
    
                return CGPoint(x : candidateAttributes!.center.x - halfWidth, y : proposedContentOffset.y);
    
            }
    
        }
    
        // Fallback
        return super.targetContentOffsetForProposedContentOffset(proposedContentOffset)
    }
    

    Then on the class that you implement the UICollectionView do like that:

        let collectionViewLayout: CenterCellCollectionViewFlowLayout = CenterCellCollectionViewFlowLayout()
        collectionViewLayout.itemSize = CGSizeMake(self.itemSize, self.itemSize)
        collectionViewLayout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        collectionViewLayout.minimumInteritemSpacing = 0
        collectionViewLayout.minimumLineSpacing = self.itemSpacing
        collectionViewLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
    
        var collectionView: UICollectionView = UICollectionView(frame: self.collectionContainer.bounds, collectionViewLayout: collectionViewLayout)
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = UIColor.redColor()
    
        collectionView.registerClass(LevelsCustomCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        collectionView.registerNib(UINib(nibName: reuseIdentifier, bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
    
        self.collectionContainer.addSubview(collectionView)
    

    Thats about it, works like a charm.

    0 讨论(0)
提交回复
热议问题