Skip to content

Commit 03cf313

Browse files
author
*OC2sC7ylDj
committed
Append
1 parent 90034b8 commit 03cf313

File tree

3 files changed

+202
-26
lines changed

3 files changed

+202
-26
lines changed

GridView/GridView.swift

Lines changed: 133 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import UIKit
1414
func gridView(_ gridView: GridView, cellForRowAt indexPath: IndexPath) -> GridViewCell
1515

1616
@objc optional func numberOfColumns(in gridView: GridView) -> Int
17+
@objc optional func numberOfColumnsPerPage(in gridView: GridView) -> Int
1718
}
1819

1920
// MARK: -
@@ -42,7 +43,7 @@ open class GridView: UIScrollView {
4243
override open class var layerClass : AnyClass {
4344
return AnimatedLayer.self
4445
}
45-
46+
4647
/// Set `false` if you don't need to loop of view. Default is `true`.
4748
open var isInfinitable = true
4849
/// Set the vertical and horizontal minimum scales. Default for x and y are 1.
@@ -179,12 +180,11 @@ open class GridView: UIScrollView {
179180
let inset = UIEdgeInsets(top: -frame.minY, left: -frame.minX, bottom: -superview.bounds.height + frame.maxY, right: -superview.bounds.width + frame.maxX)
180181
super.scrollIndicatorInsets = inset + originScrollIndicatorInsets
181182
}
182-
183-
stopScroll()
184-
183+
185184
columnRow.removeAll()
186185
currentInfo = ViewVisibleInfo()
187186
currentMatrix = makeMatrix(.all(currentMatrix))
187+
print("Matrix Reload = ", currentMatrix)
188188

189189
performWithoutDelegation {
190190
contentSize = currentMatrix.contentSize
@@ -196,7 +196,6 @@ open class GridView: UIScrollView {
196196
layoutToRemoveCells()
197197

198198
case .layout(let type):
199-
stopScroll()
200199

201200
currentMatrix = makeMatrix(type)
202201

@@ -233,6 +232,41 @@ open class GridView: UIScrollView {
233232
layoutToRemoveCells()
234233
}
235234

235+
case .appendVertical:
236+
if let superview = superview {
237+
let inset = UIEdgeInsets(top: -frame.minY, left: -frame.minX, bottom: -superview.bounds.height + frame.maxY, right: -superview.bounds.width + frame.maxX)
238+
super.scrollIndicatorInsets = inset + originScrollIndicatorInsets
239+
}
240+
241+
columnRow.removeAll()
242+
currentInfo = ViewVisibleInfo()
243+
currentMatrix = makeMatrix(.appendVertical(currentMatrix))
244+
print("Matrix Append Vertical = ", currentMatrix)
245+
246+
performWithoutDelegation {
247+
contentSize = currentMatrix.contentSize
248+
}
249+
contentOffset = currentMatrix.convert(lastValidityContentOffset, from: currentMatrix)
250+
super.contentInset = currentMatrix.contentInset
251+
252+
infiniteIfNeeded()
253+
layoutToRemoveCells()
254+
255+
case .appendHorizontal:
256+
257+
columnRow.removeAll()
258+
currentInfo = ViewVisibleInfo()
259+
currentMatrix = makeMatrix(.appendHorizontal(currentMatrix))
260+
print("Matrix Append Horizontal = ", currentMatrix)
261+
262+
performWithoutDelegation {
263+
contentSize = currentMatrix.contentSize
264+
}
265+
contentOffset = currentMatrix.convert(lastValidityContentOffset, from: currentMatrix)
266+
super.contentInset = currentMatrix.contentInset
267+
268+
infiniteIfNeeded()
269+
layoutToRemoveCells()
236270
}
237271

238272
UIView.setAnimationsEnabled(areAnimationsEnabled)
@@ -267,6 +301,10 @@ open class GridView: UIScrollView {
267301
}
268302
}
269303

304+
fileprivate func columnCountPerPage() -> Int {
305+
return dataSource?.numberOfColumnsPerPage?(in: self) ?? 0
306+
}
307+
270308
fileprivate func rowCount(in column: Int) -> Int {
271309
if let rowCount = columnRow[column] {
272310
return rowCount
@@ -407,6 +445,16 @@ extension GridView {
407445
layoutIfNeeded()
408446
}
409447

448+
public func appendTime() {
449+
setNeedsLayout(.appendVertical)
450+
layoutIfNeeded()
451+
}
452+
453+
public func appendChannel() {
454+
setNeedsLayout(.appendHorizontal)
455+
layoutIfNeeded()
456+
}
457+
410458
public func invalidateContentSize() {
411459
setNeedsLayout(.layout(.rotating(currentMatrix)))
412460
}
@@ -769,18 +817,14 @@ private extension GridView {
769817

770818
(0..<count).forEach { column in
771819
if let widthForColumn = gridViewDelegate?.gridView?(self, widthForColumn: column) {
772-
let horizontal = Horizontal(x: size.width, width: widthForColumn)
773-
columnHorizontals.append(horizontal)
774820
size.width += widthForColumn
775821
}
776822

777-
if case .all = type {
778-
let columnVerticals = verticalsForRow(in: column)
779-
columnRowVerticals.append(columnVerticals)
780-
781-
if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
782-
size.height = vertical.maxY
783-
}
823+
let columnVerticals = verticalsForRow(in: column)
824+
columnRowVerticals.append(columnVerticals)
825+
826+
if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
827+
size.height = vertical.maxY
784828
}
785829
}
786830

@@ -796,6 +840,81 @@ private extension GridView {
796840
} else {
797841
return ViewMatrix(horizontals: horizontals, verticals: columnRowVerticals, viewFrame: frame, contentHeight: size.height, superviewSize: superview?.bounds.size, scale: currentScale, inset: contentInset, isInfinitable: isInfinitable)
798842
}
843+
844+
case .appendVertical(let matrix):
845+
var copyMatrix = matrix
846+
var size: CGSize = .zero
847+
if let arrayHorizontal = copyMatrix.horizontals,
848+
let lastItem = arrayHorizontal.last {
849+
size.width = lastItem.maxX
850+
}
851+
852+
let count = columnCount()
853+
var columnHorizontals: [Horizontal] = []
854+
var columnRowVerticals: [[Vertical?]] = []
855+
856+
(0..<count).forEach { column in
857+
if let widthForColumn = gridViewDelegate?.gridView?(self, widthForColumn: column) {
858+
size.width += widthForColumn
859+
}
860+
861+
let columnVerticals = verticalsForRow(in: column)
862+
columnRowVerticals.append(columnVerticals)
863+
864+
if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
865+
size.height = vertical.maxY
866+
}
867+
}
868+
869+
let horizontals: [Horizontal]?
870+
if columnHorizontals.count > 0 && columnHorizontals.count == count {
871+
horizontals = columnHorizontals
872+
} else {
873+
horizontals = nil
874+
}
875+
876+
copyMatrix.appendVerticals(horizontals: horizontals, verticals: columnRowVerticals, viewFrame: frame, contentHeight: size.height, superviewSize: superview?.bounds.size, scale: currentScale)
877+
878+
return copyMatrix
879+
880+
case .appendHorizontal(let matrix):
881+
882+
var copyMatrix = matrix
883+
var size: CGSize = .zero
884+
if let arrayHorizontal = copyMatrix.horizontals,
885+
let lastItem = arrayHorizontal.last {
886+
size.width = lastItem.maxX
887+
}
888+
889+
let count = columnCount()
890+
var columnHorizontals: [Horizontal] = []
891+
var columnRowVerticals: [[Vertical?]] = []
892+
893+
(0..<count).forEach { column in
894+
if let widthForColumn = gridViewDelegate?.gridView?(self, widthForColumn: column) {
895+
let horizontal = Horizontal(x: size.width, width: widthForColumn)
896+
columnHorizontals.append(horizontal)
897+
size.width += widthForColumn
898+
}
899+
900+
let columnVerticals = verticalsForRow(in: column)
901+
columnRowVerticals.append(columnVerticals)
902+
903+
if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
904+
size.height = vertical.maxY
905+
}
906+
}
907+
908+
let horizontals: [Horizontal]?
909+
if columnHorizontals.count > 0 && columnHorizontals.count == count {
910+
horizontals = columnHorizontals
911+
} else {
912+
horizontals = nil
913+
}
914+
915+
copyMatrix.appendHorizontals(horizontals: horizontals, verticals: columnRowVerticals, viewFrame: frame, contentHeight: size.height, superviewSize: superview?.bounds.size, scale: currentScale)
916+
917+
return copyMatrix
799918
}
800919
}
801920
}

GridView/NeedsLayout.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
//
88

99
enum NeedsLayout: CustomDebugStringConvertible {
10-
case none, reload, layout(LayoutType)
10+
case none, reload, layout(LayoutType), appendVertical, appendHorizontal
1111
var debugDescription: String {
1212
switch self {
1313
case .none: return ".none"
1414
case .reload: return ".reload"
1515
case .layout(let type): return ".layout(\(type.debugDescription))"
16+
case .appendVertical: return ".appendVertical"
17+
case .appendHorizontal: return ".appendHorizontal"
1618
}
1719
}
1820

1921
enum LayoutType: CustomDebugStringConvertible {
20-
case all(ViewMatrix), horizontally(ViewMatrix), rotating(ViewMatrix), scaling(ViewMatrix), pinching(ViewMatrix)
22+
case all(ViewMatrix), horizontally(ViewMatrix), rotating(ViewMatrix), scaling(ViewMatrix), pinching(ViewMatrix), appendVertical(ViewMatrix), appendHorizontal(ViewMatrix)
2123
var isScaling: Bool {
2224
switch self {
2325
case .scaling, .pinching:
@@ -34,6 +36,8 @@ enum NeedsLayout: CustomDebugStringConvertible {
3436
case .rotating: return ".rotating"
3537
case .scaling: return ".scaling"
3638
case .pinching: return ".pinching"
39+
case .appendVertical: return ".appendVertical"
40+
case .appendHorizontal: return ".appendHorizontal"
3741
}
3842
}
3943
}
@@ -70,14 +74,18 @@ extension NeedsLayout: Comparable {
7074
}
7175
case .none:
7276
return false
77+
case .appendVertical:
78+
return true
79+
case .appendHorizontal:
80+
return true
7381
}
7482
}
7583
}
7684

7785
extension NeedsLayout.LayoutType {
7886
var matrix: ViewMatrix {
7987
switch self {
80-
case .all(let m), .horizontally(let m), .rotating(let m), .scaling(let m), .pinching(let m):
88+
case .all(let m), .horizontally(let m), .rotating(let m), .scaling(let m), .pinching(let m), .appendVertical(let m), .appendHorizontal(let m):
8189
return m
8290
}
8391
}
@@ -128,6 +136,10 @@ extension NeedsLayout.LayoutType: Comparable {
128136
}
129137
case .pinching:
130138
return false
139+
case .appendVertical:
140+
return true
141+
case .appendHorizontal:
142+
return true
131143
}
132144
}
133145
}

GridView/ViewMatrix.swift

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ import UIKit
1010

1111
struct ViewMatrix: Countable {
1212
private let isInfinitable: Bool
13-
private let horizontals: [Horizontal]?
14-
private let verticals: [[Vertical?]]
15-
private let visibleSize: CGSize?
16-
private let viewFrame: CGRect
13+
public var horizontals: [Horizontal]?
14+
private var verticals: [[Vertical?]]
15+
private var visibleSize: CGSize?
16+
private var viewFrame: CGRect
1717
private let scale: Scale
1818
private let inset: UIEdgeInsets
19-
private let aroundInset: AroundInsets
20-
private let contentHeight: CGFloat
19+
private var aroundInset: AroundInsets
20+
private var contentHeight: CGFloat
2121

22-
let validityContentRect: CGRect
23-
let contentSize: CGSize
24-
let contentInset: UIEdgeInsets
22+
var validityContentRect: CGRect
23+
var contentSize: CGSize
24+
var contentInset: UIEdgeInsets
2525
var count: Int {
2626
return verticals.count
2727
}
@@ -279,6 +279,51 @@ struct ViewMatrix: Countable {
279279
}
280280
}
281281

282+
extension ViewMatrix {
283+
mutating func appendVerticals(horizontals: [Horizontal]?, verticals: [[Vertical?]], viewFrame: CGRect, contentHeight: CGFloat, superviewSize: CGSize?, scale: Scale) {
284+
285+
var contentSize: CGSize = .zero
286+
contentSize.width = (horizontals?.last?.maxX ?? viewFrame.width * CGFloat(verticals.endIndex)) * scale.x
287+
if contentHeight == 0 {
288+
contentSize.height = viewFrame.height * CGFloat(verticals.first?.endIndex ?? 0) * scale.y
289+
} else {
290+
contentSize.height = contentHeight * scale.y
291+
}
292+
293+
self.verticals += verticals
294+
self.viewFrame = viewFrame
295+
self.visibleSize = superviewSize
296+
self.contentHeight = contentHeight
297+
298+
self.aroundInset = .zero
299+
self.validityContentRect = CGRect(origin: .zero, size: contentSize)
300+
self.contentSize = contentSize
301+
}
302+
303+
mutating func appendHorizontals(horizontals: [Horizontal]?, verticals: [[Vertical?]], viewFrame: CGRect, contentHeight: CGFloat, superviewSize: CGSize?, scale: Scale) {
304+
305+
var contentSize: CGSize = .zero
306+
contentSize.width = (horizontals?.last?.maxX ?? viewFrame.width * CGFloat(verticals.endIndex)) * scale.x
307+
if contentHeight == 0 {
308+
contentSize.height = viewFrame.height * CGFloat(verticals.first?.endIndex ?? 0) * scale.y
309+
} else {
310+
contentSize.height = contentHeight * scale.y
311+
}
312+
if var selfHorizontals = horizontals, let arrayHorizontals = horizontals {
313+
selfHorizontals += arrayHorizontals
314+
self.horizontals = selfHorizontals
315+
}
316+
self.verticals += verticals
317+
self.viewFrame = viewFrame
318+
self.visibleSize = superviewSize
319+
self.contentHeight = contentHeight
320+
321+
self.aroundInset = .zero
322+
self.validityContentRect = CGRect(origin: .zero, size: contentSize)
323+
self.contentSize = contentSize
324+
}
325+
}
326+
282327
#if DEBUG
283328
extension ViewMatrix {
284329
func debugVerticalsForColumn(_ column: Int) -> [Vertical?] {

0 commit comments

Comments
 (0)