forked from michiamling/SpreadsheetView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpreadsheetView+CirclularScrolling.swift
112 lines (103 loc) · 4.63 KB
/
SpreadsheetView+CirclularScrolling.swift
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
//
// SpreadsheetView+CirclularScrolling.swift
// SpreadsheetView
//
// Created by Kishikawa Katsumi on 5/1/17.
// Copyright © 2017 Kishikawa Katsumi. All rights reserved.
//
import UIKit
extension SpreadsheetView {
func scrollToHorizontalCenter() {
rowHeaderView.state.contentOffset.x = centerOffset.x
tableView.state.contentOffset.x = centerOffset.x
}
func scrollToVerticalCenter() {
columnHeaderView.state.contentOffset.y = centerOffset.y
tableView.state.contentOffset.y = centerOffset.y
}
func recenterHorizontallyIfNecessary() {
let currentOffset = tableView.state.contentOffset
let distance = currentOffset.x - centerOffset.x
let threshold = tableView.state.contentSize.width / 4
if abs(distance) > threshold {
if distance > 0 {
rowHeaderView.state.contentOffset.x = distance
tableView.state.contentOffset.x = distance
} else {
let offset = centerOffset.x + (centerOffset.x - threshold)
rowHeaderView.state.contentOffset.x = offset
tableView.state.contentOffset.x = offset
}
}
}
func recenterVerticallyIfNecessary() {
let currentOffset = tableView.state.contentOffset
let distance = currentOffset.y - centerOffset.y
let threshold = tableView.state.contentSize.height / 4
if abs(distance) > threshold {
if distance > 0 {
columnHeaderView.state.contentOffset.y = distance
tableView.state.contentOffset.y = distance
} else {
let offset = centerOffset.y + (centerOffset.y - threshold)
columnHeaderView.state.contentOffset.y = offset
tableView.state.contentOffset.y = offset
}
}
}
func determineCircularScrollScalingFactor() -> (horizontal: Int, vertical: Int) {
return (determineHorizontalCircularScrollScalingFactor(), determineVerticalCircularScrollScalingFactor())
}
func determineHorizontalCircularScrollScalingFactor() -> Int {
guard circularScrollingOptions.direction.contains(.horizontally) else {
return 1
}
let tableContentWidth = layoutProperties.columnWidth - layoutProperties.frozenColumnWidth
let tableWidth = frame.width - layoutProperties.frozenColumnWidth
var scalingFactor = 3
while tableContentWidth > 0 && Int(tableContentWidth) * scalingFactor < Int(tableWidth) * 3 {
scalingFactor += 3
}
return scalingFactor
}
func determineVerticalCircularScrollScalingFactor() -> Int {
guard circularScrollingOptions.direction.contains(.vertically) else {
return 1
}
let tableContentHeight = layoutProperties.rowHeight - layoutProperties.frozenRowHeight
let tableHeight = frame.height - layoutProperties.frozenRowHeight
var scalingFactor = 3
while tableContentHeight > 0 && Int(tableContentHeight) * scalingFactor < Int(tableHeight) * 3 {
scalingFactor += 3
}
return scalingFactor
}
func calculateCenterOffset() -> CGPoint {
var centerOffset = CGPoint.zero
if circularScrollingOptions.direction.contains(.horizontally) {
for column in 0..<layoutProperties.numberOfColumns {
centerOffset.x += layoutProperties.columnWidthCache[column % numberOfColumns] + intercellSpacing.width
}
if circularScrollingOptions.tableStyle.contains(.columnHeaderNotRepeated) {
for column in 0..<layoutProperties.frozenColumns {
centerOffset.x -= layoutProperties.columnWidthCache[column]
}
centerOffset.x -= intercellSpacing.width * CGFloat(layoutProperties.frozenColumns)
}
centerOffset.x *= CGFloat(circularScrollScalingFactor.horizontal / 3)
}
if circularScrollingOptions.direction.contains(.vertically) {
for row in 0..<layoutProperties.numberOfRows {
centerOffset.y += layoutProperties.rowHeightCache[row % numberOfRows] + intercellSpacing.height
}
if circularScrollingOptions.tableStyle.contains(.rowHeaderNotRepeated) {
for column in 0..<layoutProperties.frozenRows {
centerOffset.y -= layoutProperties.rowHeightCache[column]
}
centerOffset.y -= intercellSpacing.height * CGFloat(layoutProperties.frozenRows)
}
centerOffset.y *= CGFloat(circularScrollScalingFactor.vertical / 3)
}
return centerOffset
}
}