栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Swift创建一个分页UICollectionView

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Swift创建一个分页UICollectionView

所以我想出了办法。

首先创建一个自定义UICollectionViewFlowLayout并添加此重写此方法:

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

然后在实现UICollectionView的类上执行以下操作:

    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)

就是这样,就像一个魅力。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/399615.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号