CNLiveVocationalCertifiteCollectionViewLayout.swift 4.27 KB
//
//  CNLiveVocationalCertifiteCollectionViewLayout.swift
//  metaCode
//
//  Created by zx on 2024/7/25.
//

import UIKit

class CNLiveVocationalCertifiteCollectionViewLayout: UICollectionViewFlowLayout {
    var cellWidth: CGFloat = 76.0
    var cellHeight: CGFloat = 90.0
    var bigCellWidth: CGFloat = 97.0
    var bigCellHeight: CGFloat = 114.0


//
//    override var collectionViewContentSize: CGSize {
//        return CGSize(width: cellWidth * CGFloat(collectionView?.numberOfItems(inSection: 0) ?? 0), height: cellHeight)
//    }

    override func prepare() {
        super.prepare()
        // 这里可以添加额外的准备工作
    }


    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
//    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
//        let collectionW = collectionView?.bounds.size.width ?? 0
//        guard let superAttrs = super.layoutAttributesForElements(in: rect) else { return nil }
//          
//        var adjustedAttrs = [UICollectionViewLayoutAttributes]()
//          
//        // 遍历所有元素属性
//        for attr in superAttrs {
//            // 复制当前属性以避免直接修改原始属性
//            let mutableAttr = attr.copy() as! UICollectionViewLayoutAttributes
//              
//            // 计算当前图片中心到集合视图最左侧的距离
//            let offsetX = mutableAttr.center.x - (collectionView?.contentOffset.x ?? 0) 
//              
//            // 检查是否是左侧第一个图片,并且距离小于或等于100点
//            if offsetX <= 100 && offsetX >= 0 { // 确保图片在集合视图内
//                // 计算放大比例(这里假设最大放大到1.3倍,可以根据需要调整)
//                let scaleFactor = 1 - (offsetX / 100.0) * (1 - 1.3)
//                let scale = max(scaleFactor, 1.0) // 确保不会缩小
//                mutableAttr.transform = CGAffineTransform(scaleX: scale, y: scale)
//                print("1111scale:\(scale)")
//
//            } else {
//                // 如果不是左侧第一个图片或距离太远,则不放大
//                mutableAttr.transform = CGAffineTransform.identity
//            }
//            print("1111offsetX:\(offsetX)")
//
//              
//            adjustedAttrs.append(mutableAttr)
//        }
//          
//        return adjustedAttrs
//    }
      
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
            let collectionW = collectionView?.bounds.size.width ?? 0
        guard let attrs = super.layoutAttributesForElements( in: collectionView?.bounds ?? .zero) else { return nil }
              
            for attr in attrs {
                let margin = abs((attr.center.x - (collectionView?.contentOffset.x ?? 0)))
                let bigScale:CGFloat = 114.0/90.0
                var scale:CGFloat = 1.0
                let maxOffX:CGFloat = 153
                let minOffX:CGFloat = 53
                if margin >= minOffX && margin <= maxOffX{
                    scale = 1+(bigScale-1)*((maxOffX-margin)/100.0)
                }else{
                    scale = 1.0
                }
                attr.transform = CGAffineTransform(scaleX: scale, y: scale)
            }
              
            return attrs
        }
          
        override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
            let collectionW = collectionView?.bounds.size.width ?? 0
            var targetP = proposedContentOffset
              
            guard let attrs = super.layoutAttributesForElements(in: CGRect(x: targetP.x, y: 0, width: collectionW, height: CGFloat.greatestFiniteMagnitude)) else {
                return targetP
            }
              
            var minSpacing: CGFloat = CGFloat.greatestFiniteMagnitude
            for attr in attrs {
                let centerOffsetX = attr.center.x - targetP.x - collectionW * 0.5
                if abs(centerOffsetX) < abs(minSpacing) {
                    minSpacing = centerOffsetX
                }
            }
              
            targetP.x += minSpacing
            return targetP
        }
}