炫酷的UIcollectionlayout

原文// github: https://github.com/jasnig
// 简书: http://www.jianshu.com/p/b84f4dd96d0c

![48BA7E311AC1141EBF92C6BE38007BF3.jpg](http://upload-images.jianshu.io/upload_images/1402122-b15c3da976aac86e.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

实现代码

class LineLayout: UICollectionViewFlowLayout {
    // 用来缓存布局
    private var layoutAttributes: [UICollectionViewLayoutAttributes] = []
    var contentOffsetBeginX:CGFloat = 0.0
    
    override func prepare() {
        super.prepare()
        scrollDirection = .horizontal
        self.minimumLineSpacing = 10
        self.minimumInteritemSpacing = 10
        let Insetwith = UIScreen.main.bounds.size.width / 2 - itemSize.width / 2
        
        self.sectionInset = UIEdgeInsets.init(top: 0, left: Insetwith, bottom: 0, right: Insetwith)
        
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        layoutAttributes = []
        let superLayoutAttributes = super.layoutAttributesForElements(in: rect)!
        
        let collectionViewCenterX = collectionView!.bounds.width * 0.5
        
        superLayoutAttributes.forEach { (attributes) in
    
            let copyLayout = attributes.copy() as! UICollectionViewLayoutAttributes
            // 和中心点的横向距离差
            let deltaX = fabs( copyLayout.center.x - collectionView!.contentOffset.x - collectionViewCenterX)
            // 计算屏幕内的cell的transform
                let precent = 1.0 -  (deltaX * 0.1) / collectionViewCenterX
                copyLayout.alpha = precent
                var transform = CATransform3DIdentity
                transform = CATransform3DScale(transform, precent, precent, 1)
                copyLayout.transform3D = transform
                copyLayout.alpha = 1 - (deltaX * 0.5)/collectionViewCenterX
                copyLayout.zIndex = 1
            layoutAttributes.append(copyLayout)
        }
        return layoutAttributes
    }
    /** 返回true将会标记collectionView的data为 'dirty'
     * collectionView检测到 'dirty'标记时会在下一个周期中更新布局
     * 滚动的时候实时更新布局
     */
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
    
    // 此方法已经在父类UICollectionViewFlowLayout中
    // 已经实现了,即,父类中这个方法已经能够产生出一堆
    // 决定item如何显示的attributes对象了
    override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
        let distance = proposedContentOffset.x - contentOffsetBeginX
        var contentFrame:CGRect = CGRect.init()
        if distance > self.itemSize.width / 2 {
            contentFrame.origin = CGPoint.init(x: contentOffsetBeginX + self.itemSize.width, y: proposedContentOffset.y)
        }else if distance < -self.itemSize.width / 2{
            contentFrame.origin = CGPoint.init(x: contentOffsetBeginX - self.itemSize.width, y: proposedContentOffset.y)
        }else{
            contentFrame.origin = CGPoint.init(x: contentOffsetBeginX  , y: proposedContentOffset.y)
        }
        contentFrame.size = (self.collectionView?.frame.size)!
        let array = self.layoutAttributesForElements(in: contentFrame)
        var maiCenterX = CGFloat.greatestFiniteMagnitude
        let collectionViewCenterX = contentFrame.origin.x + (self.collectionView?.frame.size.width)! * 0.5
        array?.forEach({ (attrs) in
            if abs(attrs.center.x - collectionViewCenterX) < abs(maiCenterX){
                maiCenterX = attrs.center.x - collectionViewCenterX
            }
        })
        return  CGPoint.init(x: contentFrame.origin.x + maiCenterX, y: proposedContentOffset.y)
    }
}

简单使用

  let layout = LineLayout()
   layout.itemSize = CGSize(width: 250.0, height: 400.0)
   collectionView?.collectionViewLayout = layout

  override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        layout.contentOffsetBeginX = scrollView.contentOffset.x
    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,002评论 25 709
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,603评论 2 45
  • 浮动概述 CSS浮动由属性值float来体现,通过浮动可以实现文字环绕的效果 浮动特点 先看下面示例 图片未设置浮...
    田田kyle阅读 433评论 0 0
  • 【同读一本书】 2016-12-28-115 《管理的觉醒》 【原文】 稽核造就的是一种企业文化,企业氛围,它靠氛...
    杨平的阅读 347评论 2 0
  • 天色渐渐暗下来 耳机里的声音依旧 心情低落就像下雨淋了水 没有穿透迷雾的阳光 不断去切换聊天的界面 依旧是什么都没...
    萌龙在天阅读 116评论 0 0