【问题标题】:UICollectionView - Horizontal paging with one Cell at a timeUICollectionView - 一次一个单元格的水平分页
【发布时间】:2016-08-11 11:15:00
【问题描述】:

我正在尝试使用 collectionView 实现类似轮播的效果。我实现了UICollectionView 并将其设置为水平显示单元格

唯一的问题是我怎样才能只允许一个单元格的出现并将该单元格在屏幕上居中。

注意:分页已启用。

我找到了这段代码,试过了,可惜没用

代码参考:Create a Paging UICollectionView with Swift

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) {

                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)
    }

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewlayout


    【解决方案1】:

    对于center cell,上面的代码是对的,也实现了这两个方法:

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let currentIndex:CGFloat = self.GridCollectionView.contentOffset.x / self.GridCollectionView.frame.size.width
        pageControl.currentPage = Int(currentIndex)
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {            
        return CGSize(width: collectionView.frame.size.width, height: collectionView.frame.size.width+40)
    }
    

    【讨论】:

    • 当单元格宽度与collectionView 宽度相同时,您的答案完美运行,否则当单元格宽度与collectionView 单元格不同时会出现问题
    • 在情节提要中设置单元格宽度。
    • 我做到了,第一个和最后一个单元格没有居中,为什么?
    • 没有看故事板和代码我是怎么理解的,所以看我的代码和用户界面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多