炫酷的UIcollectionlayout

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

炫酷的UIcollectionlayout_第1张图片
![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
    }

你可能感兴趣的:(炫酷的UIcollectionlayout)