Skip to content

Commit 8c1c6d6

Browse files
committed
go on
1 parent 73a8052 commit 8c1c6d6

35 files changed

+2723
-0
lines changed

.gitignore

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Xcode
2+
#
3+
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
4+
5+
# Mac OS X Finder and whatnot
6+
.DS_Store
7+
8+
## Build generated
9+
build/
10+
DerivedData/
11+
12+
## Various settings
13+
*.pbxuser
14+
!default.pbxuser
15+
*.mode1v3
16+
!default.mode1v3
17+
*.mode2v3
18+
!default.mode2v3
19+
*.perspectivev3
20+
!default.perspectivev3
21+
xcuserdata/
22+
23+
## Other
24+
*.moved-aside
25+
*.xcuserstate
26+
*.xccheckout
27+
28+
## Obj-C/Swift specific
29+
*.hmap
30+
*.ipa
31+
*.dSYM.zip
32+
*.dSYM
33+
34+
#CocoaPods
35+
iOS Example/Pods/
36+

SwiftyCollectionViewFlowLayout.xcworkspace/contents.xcworkspacedata

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
//
2+
// Model.swift
3+
// SwiftyCollectionViewFlowLayout
4+
//
5+
// Created by dfsx6 on 2023/1/31.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
internal class BaseSectionModel {
12+
var headerLayoutAttributes: UICollectionViewLayoutAttributes?
13+
var footerLayoutAttributes: UICollectionViewLayoutAttributes?
14+
var itemLayoutAttributes: [UICollectionViewLayoutAttributes] = []
15+
16+
var sectionInset: UIEdgeInsets = .zero
17+
var lineSpacing: CGFloat = .zero
18+
var interitemSpacing: CGFloat = .zero
19+
var sectionInsetContainHeader: Bool = false
20+
var sectionInsetContainFooter: Bool = false
21+
22+
var sectionType: SwiftyCollectionViewSectionType = .normal
23+
}
24+
25+
extension BaseSectionModel {
26+
/// 当前Section中所有Item的总长度
27+
internal func allItemsLength(scrollDirection: UICollectionView.ScrollDirection) -> CGFloat {
28+
var length: CGFloat = .zero
29+
if scrollDirection == .vertical {
30+
if !itemLayoutAttributes.isEmpty {
31+
let allItemFrame = itemLayoutAttributes.reduce(itemLayoutAttributes.first!.frame) { partialResult, attr in
32+
return partialResult.union(attr.frame)
33+
}
34+
length += allItemFrame.height
35+
}
36+
} else {
37+
if !itemLayoutAttributes.isEmpty {
38+
let allItemFrame = itemLayoutAttributes.reduce(itemLayoutAttributes.first!.frame) { partialResult, attr in
39+
return partialResult.union(attr.frame)
40+
}
41+
length += allItemFrame.width
42+
}
43+
}
44+
return length
45+
}
46+
47+
/// 当前Section总长度
48+
internal func totalLength(scrollDirection: UICollectionView.ScrollDirection) -> CGFloat {
49+
var length: CGFloat = .zero
50+
if scrollDirection == .vertical {
51+
//
52+
length += sectionInset.top
53+
// header
54+
if let headerAttr = headerLayoutAttributes {
55+
length += headerAttr.frame.height
56+
}
57+
// items
58+
length += allItemsLength(scrollDirection: scrollDirection)
59+
//
60+
length += sectionInset.bottom
61+
// footer
62+
if let footerAttr = footerLayoutAttributes {
63+
length += footerAttr.frame.height
64+
}
65+
} else if scrollDirection == .horizontal {
66+
//
67+
length += sectionInset.left
68+
// header
69+
if let headerAttr = headerLayoutAttributes {
70+
length += headerAttr.frame.width
71+
}
72+
// items
73+
length += allItemsLength(scrollDirection: scrollDirection)
74+
//
75+
length += sectionInset.right
76+
// footer
77+
if let footerAttr = footerLayoutAttributes {
78+
length += footerAttr.frame.width
79+
}
80+
}
81+
return length
82+
}
83+
84+
// 当前Section的Body之前的长度(header + sectionInset.top)
85+
internal func bodyBeforeLength(scrollDirection: UICollectionView.ScrollDirection) -> CGFloat {
86+
var length: CGFloat = .zero
87+
if scrollDirection == .vertical {
88+
//
89+
length += sectionInset.top
90+
// header
91+
if let headerAttr = headerLayoutAttributes {
92+
length += headerAttr.frame.height
93+
}
94+
} else if scrollDirection == .horizontal {
95+
//
96+
length += sectionInset.left
97+
// header
98+
if let headerAttr = headerLayoutAttributes {
99+
length += headerAttr.frame.width
100+
}
101+
}
102+
return length
103+
}
104+
105+
/// 当前Section的Footer之前的长度(header + sectionInset.top + body + sectionInset.bottom)
106+
internal func footerBeforeLength(scrollDirection: UICollectionView.ScrollDirection) -> CGFloat {
107+
var length: CGFloat = .zero
108+
if scrollDirection == .vertical {
109+
//
110+
length += sectionInset.top
111+
// header
112+
if let headerAttr = headerLayoutAttributes {
113+
length += headerAttr.frame.height
114+
}
115+
// items
116+
length += allItemsLength(scrollDirection: scrollDirection)
117+
//
118+
length += sectionInset.bottom
119+
} else if scrollDirection == .horizontal {
120+
//
121+
length += sectionInset.left
122+
// header
123+
if let headerAttr = headerLayoutAttributes {
124+
length += headerAttr.frame.width
125+
}
126+
// items
127+
length += allItemsLength(scrollDirection: scrollDirection)
128+
//
129+
length += sectionInset.right
130+
}
131+
return length
132+
}
133+
}
134+
135+
136+
internal class NormalSectionModel: BaseSectionModel {
137+
138+
}
139+
140+
141+
internal class WaterFlowSectionModel: BaseSectionModel {
142+
var bodyColumnLengths: [CGFloat] = []
143+
144+
// /// 当前Section的Body总长度
145+
// var maxBodyLength: CGFloat {
146+
// var maxLength: CGFloat = .zero
147+
// for length in bodyColumnLengths.values {
148+
// if !length.isLessThanOrEqualTo(maxLength) {
149+
// maxLength = length
150+
// }
151+
// }
152+
// return maxLength
153+
// }
154+
}
155+
156+
internal class TagListSectionModel: BaseSectionModel {
157+
158+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// SwiftyCollectionViewDelegateFlowLayout.swift
3+
// SwiftyCollectionViewFlowLayout
4+
//
5+
// Created by dfsx6 on 2023/1/9.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
public protocol SwiftyCollectionViewDelegateFlowLayout: UICollectionViewDelegateFlowLayout {
12+
/// SectionType
13+
func collectionView(_ collectionView: UICollectionView,
14+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
15+
sectionType section: Int) -> SwiftyCollectionViewSectionType
16+
17+
func collectionView(_ collectionView: UICollectionView,
18+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
19+
sectionInsetContainHeader section: Int) -> Bool
20+
21+
func collectionView(_ collectionView: UICollectionView,
22+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
23+
sectionInsetContainFooter section: Int) -> Bool
24+
25+
/// The collection view calls this method when the collectionView contentSize change.
26+
func collectionView(_ collectionView: UICollectionView,
27+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
28+
contentSizeDidChange size: CGSize)
29+
}
30+
31+
extension SwiftyCollectionViewDelegateFlowLayout {
32+
public func collectionView(_ collectionView: UICollectionView,
33+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
34+
sectionInsetContainHeader section: Int) -> Bool {
35+
return false
36+
}
37+
38+
public func collectionView(_ collectionView: UICollectionView,
39+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
40+
sectionInsetContainFooter section: Int) -> Bool {
41+
return false
42+
}
43+
44+
public func collectionView(_ collectionView: UICollectionView,
45+
layout collectionViewLayout: SwiftyCollectionViewFlowLayout,
46+
contentSizeDidChange size: CGSize) { }
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// SwiftyCollectionViewFlowLayout+Normal.swift
3+
// SwiftyCollectionViewFlowLayout
4+
//
5+
// Created by dfsx6 on 2023/2/1.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
extension SwiftyCollectionViewFlowLayout {
12+
internal func _layoutNormalAttributesForItem(at indexPath: IndexPath) {
13+
guard let _ = collectionView else { return }
14+
guard let sectionModel = sectionModels[indexPath.section] else { return }
15+
guard let cellAttr = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes else { return }
16+
// Use system item frame
17+
// Update Section Model
18+
sectionModel.itemLayoutAttributes.append(cellAttr)
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//
2+
// SwiftyCollectionViewFlowLayout+Supplementary.swift
3+
// SwiftyCollectionViewFlowLayout
4+
//
5+
// Created by dfsx6 on 2023/2/1.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
extension SwiftyCollectionViewFlowLayout {
12+
internal func _layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) {
13+
guard let collectionView = collectionView else { return }
14+
guard let sectionModel = sectionModels[indexPath.section] else { return }
15+
16+
let supplementaryViewAttr = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: indexPath)
17+
18+
if elementKind == UICollectionView.elementKindSectionHeader {
19+
// Header
20+
let headerSize = mDelegate?.collectionView?(collectionView,
21+
layout: self,
22+
referenceSizeForHeaderInSection: indexPath.section) ?? .zero
23+
var header_x: CGFloat = .zero
24+
var header_y: CGFloat = .zero
25+
var header_width: CGFloat = .zero
26+
var header_height: CGFloat = .zero
27+
28+
if scrollDirection == .vertical {
29+
header_x = .zero
30+
header_y = .zero
31+
header_width = collectionView.frame.width
32+
header_height = headerSize.height
33+
} else if scrollDirection == .horizontal {
34+
header_x = .zero
35+
header_y = .zero
36+
header_width = headerSize.width
37+
header_height = collectionView.frame.height
38+
}
39+
supplementaryViewAttr.frame = CGRect(x: header_x,
40+
y: header_y,
41+
width: header_width,
42+
height: header_height)
43+
44+
sectionModel.headerLayoutAttributes = supplementaryViewAttr
45+
46+
} else if elementKind == UICollectionView.elementKindSectionFooter {
47+
// Footer
48+
let footerSize = mDelegate?.collectionView?(collectionView,
49+
layout: self,
50+
referenceSizeForFooterInSection: indexPath.section) ?? .zero
51+
var footer_x: CGFloat = .zero
52+
var footer_y: CGFloat = .zero
53+
var footer_width: CGFloat = .zero
54+
var footer_height: CGFloat = .zero
55+
56+
if scrollDirection == .vertical {
57+
footer_x = .zero
58+
footer_y = .zero
59+
footer_width = collectionView.frame.width
60+
footer_height = footerSize.height
61+
} else if scrollDirection == .horizontal {
62+
footer_x = .zero
63+
footer_y = .zero
64+
footer_width = footerSize.width
65+
footer_height = collectionView.frame.height
66+
}
67+
supplementaryViewAttr.frame = CGRect(x: footer_x,
68+
y: footer_y,
69+
width: footer_width,
70+
height: footer_height)
71+
sectionModel.footerLayoutAttributes = supplementaryViewAttr
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)