UICollectionView - 一次一个Cell的水平分页 [英] UICollectionView - Horizontal paging with one Cell at a time

查看:127
本文介绍了UICollectionView - 一次一个Cell的水平分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement a carousel like effect using collectionView. I implemented the UICollectionView and set it up to show cells horizontally.

唯一的问题是如何只允许一个单元格出现并将该单元格置于屏幕中心。

The only problem is how can I allow the appearance of one cell only and to center that cell on the screen.

注意:已启用分页。

我找到了此代码,尝试了但遗憾的是无法正常工作

I found this code, tried it but sadly didn't work

代码参考:使用Swift创建分页UICollectionView

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


推荐答案

对于上面的中心单元格代码是正确的,并且还实现了这两种方法:

For center cell above code is right and also implement these two methods:

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

这篇关于UICollectionView - 一次一个Cell的水平分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆