This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
SwipeUpView.swift
368 lines (269 loc) · 12.8 KB
/
SwipeUpView.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//
// SwipeUpView.swift
// Pods-SwipeUpView_Example
//
// Created by cinaryusuf on 28.06.2018.
//
import UIKit
public class SwipeUpView: UIView {
private var isHeaderButtonDirectionToTop = true
private var activeIndex = 0
private var isOpen = false
private weak var mainView : UIView?
@objc public weak var delegate: SwipeUpViewDelegate?
@objc public weak var datasource : SwipeUpViewDatasource?
@objc public init(frame: CGRect, mainView: UIView){
super.init(frame: frame)
self.isUserInteractionEnabled = true
self.backgroundColor = .clear
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onClickedHeaderButton)))
self.mainView = mainView
self.frame = CGRect(x: 0, y: mainView.frame.height , width: mainView.frame.width, height: 0);
}
@objc public var swipeContentView : UIView? {
didSet {
addItemContainerView()
}
}
@objc public lazy var headerContainerButton : UIButton = {
let button = UIButton(type: UIButtonType.system)
button.setTitle("", for: UIControlState.normal)
button.clipsToBounds = true
if let datasource = self.datasource {
button.backgroundColor = datasource.colorOfHeaderButton(self)
}else{
button.backgroundColor = UIColor.darkGray
}
button.layer.cornerRadius = 4
button.addTarget(self, action: #selector(onClickedHeaderButton), for: .touchUpInside)
return button
}()
lazy var headerButtonPanGesture : UIPanGestureRecognizer = {
return UIPanGestureRecognizer(target: self, action: #selector(handleButtonPanGesture(recognizer:)))
}()
@objc func handleButtonPanGesture(recognizer : UIPanGestureRecognizer){
guard let mainView = self.mainView else { return }
if recognizer.state == .ended {
let index = findNewHeightPercentageIndex()
adjustHeaderButtonDirection(index: index)
self.adjustMyHeight(heigthIndex: index);
}
//mainView.bringSubview(toFront: self)
let translationRec = recognizer.translation(in: mainView)
self.center = CGPoint(x: self.center.x , y: self.center.y + translationRec.y)
var f = self.frame;
f.size.height = mainView.frame.height - f.origin.y;
self.frame = f;
recognizer.setTranslation(.zero, in: mainView)
}
lazy var swipeGestureRecognizerDown : UISwipeGestureRecognizer = {
let sg = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGestureDown(sender :)))
sg.direction = .down
return sg
}()
lazy var swipeGestureRecognizerUp : UISwipeGestureRecognizer = {
let sg = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipeGestureDown(sender :)))
sg.direction = .down
sg.numberOfTouchesRequired = 1
return sg
}()
@objc func handleSwipeGestureDown(sender : UISwipeGestureRecognizer) {
adjustMyHeight(heigthIndex: setupNewIndexForIndex(index: 0))
isHeaderButtonDirectionToTop = true
}
@objc func handleSwipeGestureUp(sender : UISwipeGestureRecognizer) {
guard let datasource = self.datasource else { return }
let stateHeightPercentages = datasource.heightPercentages(self)
adjustMyHeight(heigthIndex: setupNewIndexForIndex(index: stateHeightPercentages.count - 1))
isHeaderButtonDirectionToTop = false
}
private func findNewHeightPercentageIndex() -> Int{
guard let mainView = self.mainView, let datasource = self.datasource else {
return 0
}
if(datasource.heights(self).count > 0){
return findNewHeightIndex();
}
let ratio = self.frame.height / mainView.frame.height;
let stateHeightPercentages = datasource.heightPercentages(self)
let activeRatio = stateHeightPercentages[activeIndex]
if(ratio < activeRatio && activeIndex > 0){
for index in (0 ... activeIndex-1).reversed() {
if stateHeightPercentages[index] < ratio{
return index
}
}
return 0
}
if(ratio > activeRatio && activeIndex < stateHeightPercentages.count - 1){
for index in activeIndex+1 ..< stateHeightPercentages.count{
if stateHeightPercentages[index] > ratio{
return index
}
}
return stateHeightPercentages.count - 1
}
return activeIndex
}
private func findNewHeightIndex() -> Int{
guard let datasource = self.datasource else {
return 0
}
let currentHight = self.frame.height;
let stateHeights = datasource.heights(self)
let activeHeight = stateHeights[activeIndex]
if(currentHight < activeHeight && activeIndex > 0){
for index in (0 ... activeIndex-1).reversed() {
if stateHeights[index] < currentHight{
return index
}
}
return 0
}
if(currentHight > activeHeight && activeIndex < stateHeights.count - 1){
for index in activeIndex+1 ..< stateHeights.count{
if stateHeights[index] > currentHight{
return index
}
}
return stateHeights.count - 1
}
return activeIndex
}
@objc func onClickedHeaderButton(){
let index = findNewHeightPercentageIndex()
adjustHeaderButtonDirection(index: index)
adjustMyHeight(heigthIndex: setupNewIndexForIndex(index: index))
}
private func adjustMyHeight(heigthIndex : Int){
guard let mainView = self.mainView, let datasource = self.datasource else { return }
self.delegate?.swipeUpViewStateWillChange(self, stateIndex: heigthIndex)
activeIndex = heigthIndex
let heightPercentages = datasource.heightPercentages(self)
let heights = datasource.heights(self)
var newHeight: CGFloat = 0.0;
if(heights.count > 0){
newHeight = heights[heigthIndex];
}else{
newHeight = mainView.frame.size.height * heightPercentages[heigthIndex];
}
UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseInOut, animations: {
var frame = self.frame
frame.origin.y = mainView.frame.size.height - newHeight
frame.size.height = newHeight
self.frame = frame
self.layoutIfNeeded()
}, completion: { (_) in
self.delegate?.swipeUpViewStateDidChange(self, stateIndex: heigthIndex)
if(self.isOpen == false){
self.isOpen = true
self.delegate?.swipeUpViewDidOpen(self)
}
});
}
private func setupNewIndexForIndex(index : Int) -> Int {
return isHeaderButtonDirectionToTop ? index + 1 : index - 1
}
private func addItemContainerView() {
guard let swipeContentView = self.swipeContentView , let mainView = self.mainView else { return }
let width = mainView.frame.size.width
swipeContentView.layer.cornerRadius = 6
self.addGestureRecognizer(swipeGestureRecognizerUp)
self.addGestureRecognizer(swipeGestureRecognizerDown)
self.addSubview(headerContainerButton)
self.addSubview(swipeContentView)
self.addGestureRecognizer(self.headerButtonPanGesture)
frameItemContainerView(width: width)
}
private func frameItemContainerView(width : CGFloat) {
guard let datasource = self.datasource , let swipeContentView = self.swipeContentView else { return }
var contentX : CGFloat = 0.0
if datasource.hideHeaderButton(self) == false {
contentX = datasource.heightOfHeaderButton(self) + datasource.marginOfHeaderButton(self)
self.headerContainerButton.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints([
NSLayoutConstraint(item: self.headerContainerButton, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: self.headerContainerButton, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: datasource.heightOfHeaderButton(self)),
NSLayoutConstraint(item: self.headerContainerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: datasource.widthOfHeaderButton(self)),
NSLayoutConstraint(item: self.headerContainerButton, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0)
])
}
swipeContentView.frame.size.width = self.frame.size.width
swipeContentView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints([
NSLayoutConstraint(item: swipeContentView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: contentX),
NSLayoutConstraint(item: swipeContentView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: swipeContentView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: swipeContentView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0),
])
}
@objc public func openViewPage() {
guard let mainView = self.mainView, let datasource = self.datasource else {
return
}
self.delegate?.swipeUpViewWillOpen(self)
mainView.addSubview(self)
self.frame = CGRect(x: 0, y: mainView.frame.height , width: mainView.frame.width, height: 0);
adjustMyHeight(heigthIndex: datasource.firstOpenHeightIndex(self))
}
@objc public func closeViewPage() {
guard let mainView = self.mainView else { return }
self.delegate?.swipeUpViewWillClose(self)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.8, options: .curveEaseInOut, animations: {
self.frame = CGRect(x: 0, y: mainView.frame.height , width: mainView.frame.width, height: mainView.frame.height);
}) { (_) in
self.delegate?.swipeUpViewDidClose(self)
self.isOpen = false
self.removeFromSuperview()
}
}
@objc func heightCount() -> Int {
guard let datasource = self.datasource else {
return 0
}
let stateHeightPercentages = datasource.heightPercentages(self)
let stateHeights = datasource.heights(self)
let hCount = stateHeights.count
if(hCount > 0) {
return hCount
}
return stateHeightPercentages.count
}
func adjustHeaderButtonDirection(index : Int){
let hCount = heightCount();
if index == hCount - 1 {
self.isHeaderButtonDirectionToTop = false
}
if index == 0 {
self.isHeaderButtonDirectionToTop = true
}
}
@objc public func goToUp(stepCount : Int){
let hCount = heightCount();
var sCount = stepCount
if(activeIndex + sCount >= hCount){
sCount = hCount - activeIndex - 1
}
if(sCount < 0){
return
}
self.adjustMyHeight(heigthIndex: activeIndex + sCount);
}
@objc public func goToTop(){
let hCount = heightCount();
self.adjustMyHeight(heigthIndex: hCount - 1);
}
@objc public func goToDown(stepCount : Int){
var sCount = stepCount
if(activeIndex - sCount < 0){
sCount = activeIndex
}
self.adjustMyHeight(heigthIndex: activeIndex - sCount);
}
@objc public func goToBottom(){
self.adjustMyHeight(heigthIndex: 0);
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}