Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 133 additions & 14 deletions GridView/GridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import UIKit
func gridView(_ gridView: GridView, cellForRowAt indexPath: IndexPath) -> GridViewCell

@objc optional func numberOfColumns(in gridView: GridView) -> Int
@objc optional func numberOfColumnsPerPage(in gridView: GridView) -> Int
}

// MARK: -
Expand Down Expand Up @@ -42,7 +43,7 @@ open class GridView: UIScrollView {
override open class var layerClass : AnyClass {
return AnimatedLayer.self
}

/// Set `false` if you don't need to loop of view. Default is `true`.
open var isInfinitable = true
/// Set the vertical and horizontal minimum scales. Default for x and y are 1.
Expand Down Expand Up @@ -179,12 +180,11 @@ open class GridView: UIScrollView {
let inset = UIEdgeInsets(top: -frame.minY, left: -frame.minX, bottom: -superview.bounds.height + frame.maxY, right: -superview.bounds.width + frame.maxX)
super.scrollIndicatorInsets = inset + originScrollIndicatorInsets
}

stopScroll()


columnRow.removeAll()
currentInfo = ViewVisibleInfo()
currentMatrix = makeMatrix(.all(currentMatrix))
print("Matrix Reload = ", currentMatrix)

performWithoutDelegation {
contentSize = currentMatrix.contentSize
Expand All @@ -196,7 +196,6 @@ open class GridView: UIScrollView {
layoutToRemoveCells()

case .layout(let type):
stopScroll()

currentMatrix = makeMatrix(type)

Expand Down Expand Up @@ -233,6 +232,41 @@ open class GridView: UIScrollView {
layoutToRemoveCells()
}

case .appendVertical:
if let superview = superview {
let inset = UIEdgeInsets(top: -frame.minY, left: -frame.minX, bottom: -superview.bounds.height + frame.maxY, right: -superview.bounds.width + frame.maxX)
super.scrollIndicatorInsets = inset + originScrollIndicatorInsets
}

columnRow.removeAll()
currentInfo = ViewVisibleInfo()
currentMatrix = makeMatrix(.appendVertical(currentMatrix))
print("Matrix Append Vertical = ", currentMatrix)

performWithoutDelegation {
contentSize = currentMatrix.contentSize
}
contentOffset = currentMatrix.convert(lastValidityContentOffset, from: currentMatrix)
super.contentInset = currentMatrix.contentInset

infiniteIfNeeded()
layoutToRemoveCells()

case .appendHorizontal:

columnRow.removeAll()
currentInfo = ViewVisibleInfo()
currentMatrix = makeMatrix(.appendHorizontal(currentMatrix))
print("Matrix Append Horizontal = ", currentMatrix)

performWithoutDelegation {
contentSize = currentMatrix.contentSize
}
contentOffset = currentMatrix.convert(lastValidityContentOffset, from: currentMatrix)
super.contentInset = currentMatrix.contentInset

infiniteIfNeeded()
layoutToRemoveCells()
}

UIView.setAnimationsEnabled(areAnimationsEnabled)
Expand Down Expand Up @@ -267,6 +301,10 @@ open class GridView: UIScrollView {
}
}

fileprivate func columnCountPerPage() -> Int {
return dataSource?.numberOfColumnsPerPage?(in: self) ?? 0
}

fileprivate func rowCount(in column: Int) -> Int {
if let rowCount = columnRow[column] {
return rowCount
Expand Down Expand Up @@ -407,6 +445,16 @@ extension GridView {
layoutIfNeeded()
}

public func appendTime() {
setNeedsLayout(.appendVertical)
layoutIfNeeded()
}

public func appendChannel() {
setNeedsLayout(.appendHorizontal)
layoutIfNeeded()
}

public func invalidateContentSize() {
setNeedsLayout(.layout(.rotating(currentMatrix)))
}
Expand Down Expand Up @@ -769,18 +817,14 @@ private extension GridView {

(0..<count).forEach { column in
if let widthForColumn = gridViewDelegate?.gridView?(self, widthForColumn: column) {
let horizontal = Horizontal(x: size.width, width: widthForColumn)
columnHorizontals.append(horizontal)
Comment on lines -772 to -773
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unnecessary process?

size.width += widthForColumn
}

if case .all = type {
let columnVerticals = verticalsForRow(in: column)
columnRowVerticals.append(columnVerticals)

if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
size.height = vertical.maxY
}
let columnVerticals = verticalsForRow(in: column)
columnRowVerticals.append(columnVerticals)

if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
size.height = vertical.maxY
}
}

Expand All @@ -796,6 +840,81 @@ private extension GridView {
} else {
return ViewMatrix(horizontals: horizontals, verticals: columnRowVerticals, viewFrame: frame, contentHeight: size.height, superviewSize: superview?.bounds.size, scale: currentScale, inset: contentInset, isInfinitable: isInfinitable)
}

case .appendVertical(let matrix):
var copyMatrix = matrix
var size: CGSize = .zero
if let arrayHorizontal = copyMatrix.horizontals,
let lastItem = arrayHorizontal.last {
size.width = lastItem.maxX
}

let count = columnCount()
var columnHorizontals: [Horizontal] = []
var columnRowVerticals: [[Vertical?]] = []

(0..<count).forEach { column in
if let widthForColumn = gridViewDelegate?.gridView?(self, widthForColumn: column) {
size.width += widthForColumn
}

let columnVerticals = verticalsForRow(in: column)
columnRowVerticals.append(columnVerticals)

if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
size.height = vertical.maxY
}
}

let horizontals: [Horizontal]?
if columnHorizontals.count > 0 && columnHorizontals.count == count {
horizontals = columnHorizontals
} else {
horizontals = nil
}

copyMatrix.appendVerticals(horizontals: horizontals, verticals: columnRowVerticals, viewFrame: frame, contentHeight: size.height, superviewSize: superview?.bounds.size, scale: currentScale)

return copyMatrix

case .appendHorizontal(let matrix):

var copyMatrix = matrix
var size: CGSize = .zero
if let arrayHorizontal = copyMatrix.horizontals,
let lastItem = arrayHorizontal.last {
size.width = lastItem.maxX
}

let count = columnCount()
var columnHorizontals: [Horizontal] = []
var columnRowVerticals: [[Vertical?]] = []

(0..<count).forEach { column in
if let widthForColumn = gridViewDelegate?.gridView?(self, widthForColumn: column) {
let horizontal = Horizontal(x: size.width, width: widthForColumn)
columnHorizontals.append(horizontal)
size.width += widthForColumn
}

let columnVerticals = verticalsForRow(in: column)
columnRowVerticals.append(columnVerticals)

if let last = columnVerticals.last, let vertical = last, size.height < vertical.maxY {
size.height = vertical.maxY
}
}

let horizontals: [Horizontal]?
if columnHorizontals.count > 0 && columnHorizontals.count == count {
horizontals = columnHorizontals
} else {
horizontals = nil
}

copyMatrix.appendHorizontals(horizontals: horizontals, verticals: columnRowVerticals, viewFrame: frame, contentHeight: size.height, superviewSize: superview?.bounds.size, scale: currentScale)

return copyMatrix
}
}
}
Expand Down
18 changes: 15 additions & 3 deletions GridView/NeedsLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
//

enum NeedsLayout: CustomDebugStringConvertible {
case none, reload, layout(LayoutType)
case none, reload, layout(LayoutType), appendVertical, appendHorizontal
var debugDescription: String {
switch self {
case .none: return ".none"
case .reload: return ".reload"
case .layout(let type): return ".layout(\(type.debugDescription))"
case .appendVertical: return ".appendVertical"
case .appendHorizontal: return ".appendHorizontal"
}
}

enum LayoutType: CustomDebugStringConvertible {
case all(ViewMatrix), horizontally(ViewMatrix), rotating(ViewMatrix), scaling(ViewMatrix), pinching(ViewMatrix)
case all(ViewMatrix), horizontally(ViewMatrix), rotating(ViewMatrix), scaling(ViewMatrix), pinching(ViewMatrix), appendVertical(ViewMatrix), appendHorizontal(ViewMatrix)
var isScaling: Bool {
switch self {
case .scaling, .pinching:
Expand All @@ -34,6 +36,8 @@ enum NeedsLayout: CustomDebugStringConvertible {
case .rotating: return ".rotating"
case .scaling: return ".scaling"
case .pinching: return ".pinching"
case .appendVertical: return ".appendVertical"
case .appendHorizontal: return ".appendHorizontal"
}
}
}
Expand Down Expand Up @@ -70,14 +74,18 @@ extension NeedsLayout: Comparable {
}
case .none:
return false
case .appendVertical:
return true
case .appendHorizontal:
return true
}
}
}

extension NeedsLayout.LayoutType {
var matrix: ViewMatrix {
switch self {
case .all(let m), .horizontally(let m), .rotating(let m), .scaling(let m), .pinching(let m):
case .all(let m), .horizontally(let m), .rotating(let m), .scaling(let m), .pinching(let m), .appendVertical(let m), .appendHorizontal(let m):
return m
}
}
Expand Down Expand Up @@ -128,6 +136,10 @@ extension NeedsLayout.LayoutType: Comparable {
}
case .pinching:
return false
case .appendVertical:
return true
case .appendHorizontal:
return true
}
}
}
63 changes: 54 additions & 9 deletions GridView/ViewMatrix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import UIKit

struct ViewMatrix: Countable {
private let isInfinitable: Bool
private let horizontals: [Horizontal]?
private let verticals: [[Vertical?]]
private let visibleSize: CGSize?
private let viewFrame: CGRect
public var horizontals: [Horizontal]?
private var verticals: [[Vertical?]]
private var visibleSize: CGSize?
private var viewFrame: CGRect
private let scale: Scale
private let inset: UIEdgeInsets
private let aroundInset: AroundInsets
private let contentHeight: CGFloat
private var aroundInset: AroundInsets
private var contentHeight: CGFloat

let validityContentRect: CGRect
let contentSize: CGSize
let contentInset: UIEdgeInsets
var validityContentRect: CGRect
var contentSize: CGSize
var contentInset: UIEdgeInsets
var count: Int {
return verticals.count
}
Expand Down Expand Up @@ -279,6 +279,51 @@ struct ViewMatrix: Countable {
}
}

extension ViewMatrix {
mutating func appendVerticals(horizontals: [Horizontal]?, verticals: [[Vertical?]], viewFrame: CGRect, contentHeight: CGFloat, superviewSize: CGSize?, scale: Scale) {

var contentSize: CGSize = .zero
contentSize.width = (horizontals?.last?.maxX ?? viewFrame.width * CGFloat(verticals.endIndex)) * scale.x
if contentHeight == 0 {
contentSize.height = viewFrame.height * CGFloat(verticals.first?.endIndex ?? 0) * scale.y
} else {
contentSize.height = contentHeight * scale.y
}

self.verticals += verticals
self.viewFrame = viewFrame
self.visibleSize = superviewSize
self.contentHeight = contentHeight

self.aroundInset = .zero
self.validityContentRect = CGRect(origin: .zero, size: contentSize)
self.contentSize = contentSize
}

mutating func appendHorizontals(horizontals: [Horizontal]?, verticals: [[Vertical?]], viewFrame: CGRect, contentHeight: CGFloat, superviewSize: CGSize?, scale: Scale) {

var contentSize: CGSize = .zero
contentSize.width = (horizontals?.last?.maxX ?? viewFrame.width * CGFloat(verticals.endIndex)) * scale.x
if contentHeight == 0 {
contentSize.height = viewFrame.height * CGFloat(verticals.first?.endIndex ?? 0) * scale.y
} else {
contentSize.height = contentHeight * scale.y
}
if var selfHorizontals = horizontals, let arrayHorizontals = horizontals {
selfHorizontals += arrayHorizontals
self.horizontals = selfHorizontals
}
self.verticals += verticals
self.viewFrame = viewFrame
self.visibleSize = superviewSize
self.contentHeight = contentHeight

self.aroundInset = .zero
self.validityContentRect = CGRect(origin: .zero, size: contentSize)
self.contentSize = contentSize
}
}

#if DEBUG
extension ViewMatrix {
func debugVerticalsForColumn(_ column: Int) -> [Vertical?] {
Expand Down