-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.swift
More file actions
241 lines (193 loc) · 9.92 KB
/
View.swift
File metadata and controls
241 lines (193 loc) · 9.92 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// View.swift
// planner
//
// Created by 김영훈 on 7/8/24.
//
import UIKit
class View: UIView {
var scrollContentViewTrailing: NSLayoutConstraint!
var scrollContentViewLeading: NSLayoutConstraint!
var delegate: SwipableTableViewCellDelegate?
var scrollDelegate: SwipableTableViewCellScrollDelegate?
var scrollView: ScrollView!
lazy var scrollContentView: UIView = {
let view = UIView()
self.scrollView.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
scrollContentViewTrailing = scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
scrollContentViewLeading = scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor)
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
view.heightAnchor.constraint(equalTo: scrollView.heightAnchor),
view.topAnchor.constraint(equalTo: scrollView.topAnchor),
view.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
scrollContentViewLeading,
scrollContentViewTrailing
])
return view
}()
var contentView: UIView!
private var trailingButtons = [UIButton]()
private var leadingButtons = [UIButton]()
var buttonTrailing: NSLayoutConstraint?
var buttonLeading: NSLayoutConstraint?
var buttonWidth: CGFloat = 48
var isScrolled: Bool = false
public required init?(coder: NSCoder) {
super.init(coder: coder)
scrollView = ScrollView()
addSubview(scrollView)
scrollView.delegate = self
contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.backgroundColor = .green
scrollView.addSubview(contentView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = .lightGray
scrollContentViewLeading = contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor)
scrollContentViewTrailing = scrollView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor)
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
scrollView.topAnchor.constraint(equalTo: topAnchor),
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
contentView.heightAnchor.constraint(equalTo: scrollView.heightAnchor),
contentView.topAnchor.constraint(equalTo: scrollView.topAnchor),
contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
scrollContentViewLeading, scrollContentViewTrailing
])
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
}
open func add(button: UIButton, to: ButtonPosition) {
scrollView.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
switch to {
case .leading:
if let first = leadingButtons.first {
first.leadingAnchor.constraint(equalTo: button.trailingAnchor).isActive = true
button.widthAnchor.constraint(equalTo: first.widthAnchor).isActive = true
} else {
let constraint = button.trailingAnchor.constraint(equalTo: contentView.leadingAnchor)
constraint.priority = UILayoutPriority(rawValue: 751)
constraint.isActive = true
}
button.widthAnchor.constraint(greaterThanOrEqualToConstant: buttonWidth).isActive = true
button.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
buttonLeading?.isActive = false
buttonLeading = scrollView.superview?.leadingAnchor.constraint(equalTo: button.leadingAnchor)
buttonLeading?.priority = UILayoutPriority(rawValue: 500)
buttonLeading?.isActive = true
leadingButtons.append(button)
scrollContentViewLeading.constant = CGFloat(leadingButtons.count) * buttonWidth
break
case .trailing:
if let last = trailingButtons.last {
last.trailingAnchor.constraint(equalTo: button.leadingAnchor).isActive = true
button.widthAnchor.constraint(equalTo: last.widthAnchor).isActive = true
} else {
let constraint = button.leadingAnchor.constraint(equalTo: contentView.trailingAnchor)
constraint.priority = UILayoutPriority(rawValue: 751)
constraint.isActive = true
}
button.widthAnchor.constraint(greaterThanOrEqualToConstant: buttonWidth).isActive = true
button.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
buttonTrailing?.isActive = false
buttonTrailing = scrollView.superview?.trailingAnchor.constraint(equalTo: button.trailingAnchor)
buttonTrailing?.priority = UILayoutPriority(rawValue: 500)
buttonTrailing?.isActive = true
trailingButtons.append(button)
scrollContentViewTrailing.constant = CGFloat(trailingButtons.count) * buttonWidth
break
}
}
func reset(animated: Bool = false) {
scrollContentViewTrailing?.constant = CGFloat(trailingButtons.count) * buttonWidth
scrollContentViewLeading?.constant = CGFloat(leadingButtons.count) * buttonWidth
scrollView.isScrollEnabled = false
scrollView.layoutIfNeeded()
scrollView.setContentOffset(CGPoint(x: scrollContentViewLeading?.constant ?? 0, y: 0), animated: animated)
scrollView.isScrollEnabled = true
}
}
extension View: UIScrollViewDelegate {
public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let trailing: CGFloat = scrollContentViewTrailing.constant
let leading: CGFloat = scrollContentViewLeading.constant
if targetContentOffset.pointee.x > leading {
if velocity.x > 0 {
targetContentOffset.pointee = CGPoint(x: leading + trailing, y: 0)
} else if velocity.x < 0 {
print("2")
targetContentOffset.pointee = CGPoint(x: leading, y: 0)
} else if targetContentOffset.pointee.x > (leading + (trailing / 2)) {
print("3", leading, trailing)
targetContentOffset.pointee = CGPoint(x: leading + trailing, y: 0)
} else {
print("4")
targetContentOffset.pointee = CGPoint(x: leading, y: 0)
}
} else {
if velocity.x > 0 {
print("5")
targetContentOffset.pointee = CGPoint(x: leading, y: 0)
} else if velocity.x < 0 {
print("6")
targetContentOffset.pointee = CGPoint(x: 0, y: 0)
} else if targetContentOffset.pointee.x > ((leading) / 2) {
print("7")
targetContentOffset.pointee = CGPoint(x: leading, y: 0)
} else {
print("8")
targetContentOffset.pointee = CGPoint(x: 0, y: 0)
}
}
// scrollDelegate?.swipableTableViewCellScrollViewDidScroll(self, scrollView: scrollView)
}
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x <= CGFloat(leadingButtons.count) * buttonWidth {
if scrollContentViewLeading.constant == 0 {
DispatchQueue.main.async {
self.scrollContentViewLeading.constant = CGFloat(self.leadingButtons.count) * self.buttonWidth
self.scrollView.contentOffset.x = CGFloat(self.leadingButtons.count) * self.buttonWidth
self.leadingButtons.forEach({ $0.isHidden = false })
self.scrollView.layoutIfNeeded()
}
}
}
if scrollView.contentOffset.x > 0 {
DispatchQueue.main.async {
self.scrollContentViewTrailing.constant = CGFloat(self.trailingButtons.count) * self.buttonWidth
self.trailingButtons.forEach({ $0.isHidden = false })
self.scrollView.layoutIfNeeded()
}
}
}
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.x == scrollContentViewLeading.constant {
isScrolled = false
} else if scrollContentViewLeading.constant != 0 && scrollView.contentOffset.x <= scrollContentViewLeading.constant / 2 {
isScrolled = true
} else if scrollContentViewTrailing.constant != 0 && scrollView.contentOffset.x >= scrollContentViewTrailing.constant / 2 {
isScrolled = true
}
var r: CGFloat
if scrollContentViewLeading.constant > 0 && scrollView.contentOffset.x < scrollContentViewLeading.constant {
r = 1 - (scrollView.contentOffset.x / scrollContentViewLeading.constant)
} else if scrollView.contentOffset.x > scrollContentViewLeading.constant {
r = (scrollView.contentOffset.x - scrollContentViewLeading.constant) / scrollContentViewTrailing.constant
} else {
r = 0
}
if r > 1 {
r = 1
} else if r < 0 {
r = 0
}
backgroundColor = tintColor.withAlphaComponent(r * 0.12)
}
}