杨锴
2024-08-28 611f271e03e9ff2b5c32a9bbb2e3eb719c178df5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
//  EqualCellSpaceFlowLayout.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/27.
//
 
import UIKit
enum AlignType : NSInteger {
                case left = 0
                case center = 1
                case right = 2
}
 
 
class EqualCellSpaceFlowLayout: UICollectionViewFlowLayout {
                //两个Cell之间的距离
                private var horizontalSpace : CGFloat{
                                didSet{
                                                self.minimumInteritemSpacing = horizontalSpace
                                }
                }
                //cell对齐方式
                private var alignType : AlignType = AlignType.center
                //在居中对齐的时候需要知道这行所有cell的宽度总和
                var cellWidthInLine : CGFloat = 0.0
 
                override init() {
                                horizontalSpace = 5.0
                                super.init()
                                scrollDirection = UICollectionView.ScrollDirection.vertical
                                minimumLineSpacing = 5
                                sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
                }
                convenience init(_ cellType:AlignType){
                                self.init()
                                self.alignType = cellType
                }
                convenience init(_ cellType: AlignType, _ horizontalSpace: CGFloat){
                                self.init()
                                self.alignType = cellType
                                self.horizontalSpace = horizontalSpace
                }
 
                required init?(coder aDecoder: NSCoder) {
                                horizontalSpace = 5.0
                                super.init(coder: aDecoder)
                                scrollDirection = UICollectionView.ScrollDirection.vertical
                                minimumLineSpacing = 5
                                sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
                }
 
                override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
 
                                let layoutAttributes_super : [UICollectionViewLayoutAttributes] = super.layoutAttributesForElements(in: rect) ?? [UICollectionViewLayoutAttributes]()
                                let layoutAttributes:[UICollectionViewLayoutAttributes] = NSArray(array: layoutAttributes_super, copyItems:true)as! [UICollectionViewLayoutAttributes]
                                var layoutAttributes_t : [UICollectionViewLayoutAttributes] = [UICollectionViewLayoutAttributes]()
                                for index in 0..<layoutAttributes.count{
 
                                                print("index in 0..<layoutAttributes.count ==============")
 
                                                let currentAttr = layoutAttributes[index]
                                                let previousAttr = index == 0 ? nil : layoutAttributes[index-1]
                                                let nextAttr = index + 1 == layoutAttributes.count ?
                                                nil : layoutAttributes[index+1]
 
                                                layoutAttributes_t.append(currentAttr)
                                                cellWidthInLine += currentAttr.frame.size.width
 
                                                let previousY :CGFloat = previousAttr == nil ? 0 : previousAttr!.frame.maxY
                                                let currentY :CGFloat = currentAttr.frame.maxY
                                                let nextY:CGFloat = nextAttr == nil ? 0 : nextAttr!.frame.maxY
 
                                                if currentY != previousY && currentY != nextY{
                                                                if currentAttr.representedElementKind == UICollectionView.elementKindSectionHeader{
                                                                                layoutAttributes_t.removeAll()
                                                                                cellWidthInLine = 0.0
                                                                                print("currentAttr.representedElementKind == UICollectionView.elementKindSectionHeader =========== Header")
                                                                }else if currentAttr.representedElementKind == UICollectionView.elementKindSectionFooter{
                                                                                layoutAttributes_t.removeAll()
                                                                                cellWidthInLine = 0.0
                                                                                print("currentAttr.representedElementKind == UICollectionView.elementKindSectionFooter ============ Footer")
                                                                }else{
                                                                                self.setCellFrame(with: layoutAttributes_t)
                                                                                layoutAttributes_t.removeAll()
                                                                                cellWidthInLine = 0.0
                                                                                print("currentY != previousY && currentY != nextY ============== Item")
                                                                }
                                                } else if currentY != nextY { //这里currentY == previousY 说明和上一个项目在同一行,currentY != nextY说明下一个项目要换行了,这种情况直接计算本行的对齐方式
                                                                self.setCellFrame(with: layoutAttributes_t)
                                                                layoutAttributes_t.removeAll()
                                                                cellWidthInLine = 0.0
                                                                print("currentY != nextY ======== Else")
                                                }
                                }
                                return layoutAttributes
                }
 
                /// 调整Cell的Frame
                ///
                /// - Parameter layoutAttributes: layoutAttribute 数组
                func setCellFrame(with layoutAttributes : [UICollectionViewLayoutAttributes]){
                                var nowWidth : CGFloat = 0.0
                                switch alignType {
                                                case AlignType.left:
                                                                nowWidth = self.sectionInset.left
                                                                for attributes in layoutAttributes {
                                                                                var nowFrame = attributes.frame
                                                                                nowFrame.origin.x = nowWidth
                                                                                attributes.frame = nowFrame
                                                                                nowWidth += nowFrame.size.width + self.horizontalSpace
                                                                }
                                                                break;
                                                case AlignType.center:
                                                                nowWidth = (self.collectionView!.frame.size.width - cellWidthInLine - (CGFloat(layoutAttributes.count - 1) * horizontalSpace)) / 2
                                                                for attributes in layoutAttributes{
                                                                                var nowFrame = attributes.frame
                                                                                nowFrame.origin.x = nowWidth
                                                                                attributes.frame = nowFrame
                                                                                nowWidth += nowFrame.size.width + self.horizontalSpace
                                                                }
                                                                break;
                                                case AlignType.right:
                                                                nowWidth = self.collectionView!.frame.size.width - self.sectionInset.right
                                                                for var index in 0 ..< layoutAttributes.count{
                                                                                index = layoutAttributes.count - 1 - index
                                                                                let attributes = layoutAttributes[index]
                                                                                var nowFrame = attributes.frame
                                                                                nowFrame.origin.x = nowWidth - nowFrame.size.width
                                                                                attributes.frame = nowFrame
                                                                                nowWidth = nowWidth - nowFrame.size.width - horizontalSpace
                                                                }
                                                                break;
                                }
                }
}