Repository navigation
Expand file tree
/
Copy pathRGPageViewController3.swift
More file actions
1169 lines (860 loc) · 38.5 KB
/
Copy pathRGPageViewController3.swift
File metadata and controls
1169 lines (860 loc) · 38.5 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// RGPageViewController.swift
// RGViewPager
//
// Created by Ronny Gerasch on 08.11.14.
// Copyright (c) 2014 Ronny Gerasch. All rights reserved.
//
import Foundation
import UIKit
enum RGTabbarStyle {
case Solid
case Blurred
}
enum RGTabStyle {
case None
case InactiveFaded
}
enum RGTabbarPosition {
case Top
case Bottom
case Left
case Right
}
class RGPageViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate, UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIToolbarDelegate {
// MARK: - Protocols
weak var datasource: RGPageViewControllerDataSource?
weak var delegate: RGPageViewControllerDelegate?
var pageViewScrollDelegate: UIScrollViewDelegate?
// MARK: - Variables
var animatingToTab: Bool = false
var needsSetup: Bool = true
var needsLayoutSubviews = true
// MARK: - Pager
var pageCount: Int! = 0
var currentPageIndex: Int = 0
var pager: UIPageViewController!
var pagerOrientation: UIPageViewControllerNavigationOrientation {
get {
return .horizontal
}
}
var pagerScrollView: UIScrollView!
var pageViewControllers: NSMutableArray = NSMutableArray()
// MARK: - Tabs
var currentTabIndex: Int = 0
var tabWidth: CGFloat = UIScreen.main.bounds.size.width / 3.0
var tabbarWidth: CGFloat {
get {
return 100.0
}
}
var tabbarHeight: CGFloat {
get {
return 38.0
}
}
var tabIndicatorWidthOrHeight: CGFloat {
get {
return 2.0
}
}
var tabIndicatorColor: UIColor {
get {
return UIColor.lightGray
}
}
var tabMargin: CGFloat {
get {
return 0.0
}
}
var tabStyle: RGTabStyle {
get {
return .None
}
}
// MARK: - Tabbar
var tabbarHidden: Bool {
get {
return false
}
}
var tabbarStyle: RGTabbarStyle {
get {
return .Blurred
}
}
var tabbarPosition: RGTabbarPosition {
get {
return .Top
}
}
var tabbarTop: CGFloat {
get {
return 0
}
}
var tabbar: UIView!
var barTintColor: UIColor? {
get {
return nil
}
}
var tabScrollView: UICollectionView!
// MARK: - Constructors
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
// MARK: - ViewController life cycle
override func loadView() {
super.loadView()
// init pager
pager = UIPageViewController(transitionStyle: UIPageViewControllerTransitionStyle.scroll, navigationOrientation: pagerOrientation, options: nil)
addChildViewController(pager)
pagerScrollView = pager.view.subviews[0] as! UIScrollView
pageViewScrollDelegate = pagerScrollView.delegate
pagerScrollView.scrollsToTop = false
pagerScrollView.delegate = self
// init tabbar
switch tabbarStyle {
case .Blurred:
tabbar = UIToolbar()
if let bar = tabbar as? UIToolbar {
bar.barTintColor = barTintColor
bar.isTranslucent = true
bar.delegate = self
}
case .Solid:
tabbar = UIView()
tabbar.backgroundColor = barTintColor
}
tabbar.isHidden = tabbarHidden
// layout
switch tabbarPosition {
case .Top:
layoutTabbarTop()
break
case .Bottom:
layoutTabbarBottom()
break
case .Left:
layoutTabbarLeft()
break
case .Right:
layoutTabbarRight()
break
}
tabScrollView.backgroundColor = UIColor.clear
tabScrollView.scrollsToTop = false
tabScrollView.isOpaque = false
tabScrollView.showsHorizontalScrollIndicator = false
tabScrollView.showsVerticalScrollIndicator = false
tabScrollView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "rgTabCell")
tabbar.addSubview(tabScrollView)
view.addSubview(pager.view)
view.addSubview(tabbar)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if needsSetup {
setupSelf()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Functions
private func layoutTabbarTop() {
tabbar.autoresizingMask = .flexibleWidth
pager.view.autoresizingMask = ([UIViewAutoresizing.flexibleHeight, UIViewAutoresizing.flexibleWidth])
var barTop: CGFloat = tabbarTop
// remove hairline image in navigation bar if attached to top
if let navController = navigationController , !navController.navigationBar.isHidden {
barTop = tabbarTop
navController.navigationBar.hideHairline()
}
let tabbarFrame = CGRect(x: 0, y: barTop, width: view.bounds.size.width, height: tabbarHidden ? 0 : tabbarHeight)
tabbar.frame = tabbarFrame
let tabScrollerFrame = CGRect(x: 0, y: 0, width: tabbarFrame.size.width, height: tabbarFrame.size.height)
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
tabScrollView = UICollectionView(frame: tabScrollerFrame, collectionViewLayout: flowLayout)
tabScrollView.autoresizingMask = .flexibleWidth
let pagerFrame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height)
pager.view.frame = pagerFrame
}
private func layoutTabbarBottom() {
tabbar.autoresizingMask = .flexibleWidth
pager.view.autoresizingMask = ([UIViewAutoresizing.flexibleHeight, UIViewAutoresizing.flexibleWidth])
let tabbarFrame = CGRect(x: 0, y: view.bounds.size.height - tabbarHeight, width: view.bounds.size.width, height: tabbarHidden ? 0 : tabbarHeight)
tabbar.frame = tabbarFrame
let tabScrollerFrame = CGRect(x: 0, y: 0, width: tabbarFrame.size.width, height: tabbarFrame.size.height)
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
tabScrollView = UICollectionView(frame: tabScrollerFrame, collectionViewLayout: flowLayout)
tabScrollView.autoresizingMask = .flexibleWidth
let pagerFrame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height)
pager.view.frame = pagerFrame
}
private func layoutTabbarLeft() {
tabbar.autoresizingMask = .flexibleHeight
pager.view.autoresizingMask = ([.flexibleHeight, .flexibleWidth])
var barTop: CGFloat = 0
// scroll tabbar under topbar if using solid style
if tabbarStyle != .Solid {
barTop = tabbarTop
if let navController = navigationController , !navController.navigationBar.isHidden {
barTop = tabbarTop
}
}
let tabbarFrame = CGRect(x: 0, y: barTop, width: tabbarHidden ? 0 : tabbarWidth, height: view.bounds.size.height - barTop)
tabbar.frame = tabbarFrame
let tabScrollerFrame = CGRect(x: 0, y: 0, width: tabbarFrame.size.width, height: tabbarFrame.size.height)
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
tabScrollView = UICollectionView(frame: tabScrollerFrame, collectionViewLayout: flowLayout)
tabScrollView.autoresizingMask = .flexibleHeight
if tabbarStyle == .Solid {
var scrollTop: CGFloat = tabbarTop
if let navController = navigationController , !navController.navigationBar.isHidden {
scrollTop = tabbarTop
}
var edgeInsets: UIEdgeInsets = tabScrollView.contentInset
edgeInsets.top = scrollTop
edgeInsets.bottom = 0
tabScrollView.contentInset = edgeInsets
tabScrollView.scrollIndicatorInsets = edgeInsets
}
let pagerFrame = CGRect(x: tabbarHidden ? 0 : tabbarWidth, y: 0, width: view.bounds.size.width - (tabbarHidden ? 0 : tabbarWidth), height: view.bounds.size.height)
pager.view.frame = pagerFrame
}
private func layoutTabbarRight() {
tabbar.autoresizingMask = .flexibleHeight
pager.view.autoresizingMask = ([.flexibleHeight, .flexibleWidth])
var barTop: CGFloat = 0
// scroll tabbar under topbar if using solid style
if tabbarStyle != .Solid {
barTop = tabbarTop
if let navController = self.navigationController , !navController.navigationBar.isHidden {
barTop = tabbarTop
}
}
let tabbarFrame = CGRect(x: view.bounds.size.width - tabbarWidth, y: barTop, width: tabbarHidden ? 0 : tabbarWidth, height: view.bounds.size.height - barTop)
tabbar.frame = tabbarFrame
let tabScrollerFrame = CGRect(x: 0, y: 0, width: tabbarFrame.size.width, height: tabbarFrame.size.height)
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .vertical
tabScrollView = UICollectionView(frame: tabScrollerFrame, collectionViewLayout: flowLayout)
tabScrollView.autoresizingMask = .flexibleHeight
if tabbarStyle == .Solid {
var scrollTop: CGFloat = tabbarTop
if let navController = navigationController , !navController.navigationBar.isHidden {
scrollTop = tabbarTop
}
var edgeInsets: UIEdgeInsets = tabScrollView.contentInset
edgeInsets.top = scrollTop
edgeInsets.bottom = 0
tabScrollView.contentInset = edgeInsets
tabScrollView.scrollIndicatorInsets = edgeInsets
}
let pagerFrame = CGRect(x: 0, y: 0, width: view.bounds.size.width - (tabbarHidden ? 0 : tabbarWidth), height: view.bounds.size.height)
pager.view.frame = pagerFrame
}
private func setupSelf() {
if let theSource = datasource {
pageCount = theSource.numberOfPagesForViewController(pageViewController: self)
}
pageViewControllers.removeAllObjects()
pageViewControllers = NSMutableArray(capacity: pageCount)
for _ in 0 ..< pageCount {
pageViewControllers.add(NSNull())
}
pager.dataSource = self
pager.delegate = self
tabScrollView.dataSource = self
tabScrollView.delegate = self
selectTabAtIndex(index: currentTabIndex, updatePage: true)
needsSetup = false
}
private func tabViewAtIndex(index: Int) -> RGTabView? {
if let tabViewContent: UIView = datasource?.tabViewForPageAtIndex(pageViewController: self, index: index) {
var tabView: RGTabView
switch tabbarPosition {
case .Top, .Bottom:
if let theWidth: CGFloat = delegate?.widthForTabAtIndex?(index: index) {
tabView = RGTabView(frame: CGRect(x: 0.0, y: 0.0, width: theWidth, height: tabbarHeight), indicatorColor: tabIndicatorColor, indicatorHW: tabIndicatorWidthOrHeight, style: tabStyle, orientation: .Horizontal)
} else {
tabView = RGTabView(frame:CGRect(x: 0.0, y: 0.0, width: tabWidth, height: tabbarHeight), indicatorColor: tabIndicatorColor, indicatorHW: tabIndicatorWidthOrHeight, style: tabStyle, orientation: .Horizontal)
}
break
case .Left:
if let theHeight: CGFloat = delegate?.heightForTabAtIndex?(index: index) {
tabView = RGTabView(frame:
CGRect(x: 0.0, y: 0.0, width: tabbarWidth, height: theHeight), indicatorColor: tabIndicatorColor, indicatorHW: tabIndicatorWidthOrHeight, style: tabStyle, orientation: .VerticalLeft)
} else {
tabView = RGTabView(frame: CGRect(x: 0.0, y: 0.0, width: tabbarWidth, height: tabbarWidth), indicatorColor: tabIndicatorColor, indicatorHW: tabIndicatorWidthOrHeight, style: tabStyle, orientation: .VerticalLeft)
}
break
case .Right:
if let theHeight: CGFloat = delegate?.heightForTabAtIndex?(index: index) {
tabView = RGTabView(frame: CGRect(x: 0.0, y: 0.0, width: tabbarWidth, height: theHeight), indicatorColor: tabIndicatorColor, indicatorHW: tabIndicatorWidthOrHeight, style: tabStyle, orientation: .VerticalRight)
} else {
tabView = RGTabView(frame:
CGRect(x: 0.0, y: 0.0, width: tabbarWidth, height: tabbarWidth), indicatorColor: tabIndicatorColor, indicatorHW: tabIndicatorWidthOrHeight, style: tabStyle, orientation: .VerticalRight)
}
break
}
tabView.addSubview(tabViewContent)
tabView.clipsToBounds = true
tabViewContent.center = tabView.center
return tabView
}
return nil
}
func selectTabAtIndex(index: Int, updatePage: Bool) {
if index >= pageCount {
return
}
animatingToTab = true
updateTabIndex(index: index, animated: true)
if updatePage {
updatePager(index: index)
} else {
currentPageIndex = index
}
delegate?.didChangePageToIndex?(index: index)
}
private func updateTabIndex(index: Int, animated: Bool) {
if let currentTabCell = tabScrollView.cellForItem(at: IndexPath(row: currentTabIndex, section: 0)) {
(currentTabCell.contentView.subviews.first as! RGTabView).selected = false
}
if let newTabCell = tabScrollView.cellForItem(at: IndexPath(row: index, section: 0)) {
var newTabRect = newTabCell.frame
switch tabbarPosition {
case .Top, .Bottom:
newTabRect.origin.x -= (index == 0 ? tabMargin : tabMargin / 2.0)
newTabRect.size.width += tabMargin
case .Left, .Right:
newTabRect.origin.y -= tabMargin / 2.0
newTabRect.size.height += tabMargin
}
let rect = tabScrollView.convert(newTabRect, to: tabScrollView.superview)
let newTabVisible = tabScrollView.frame.contains(rect)
if !newTabVisible {
var scrollPosition: UICollectionViewScrollPosition = []
if index > currentTabIndex {
switch tabbarPosition {
case .Top, .Bottom:
scrollPosition = .right
case .Left, .Right:
scrollPosition = .bottom
}
} else {
switch tabbarPosition {
case .Top, .Bottom:
scrollPosition = .left
case .Left, .Right:
scrollPosition = .top
}
}
tabScrollView.selectItem(at: IndexPath(row: index, section: 0), animated: animated, scrollPosition: scrollPosition)
tabScrollView.scrollRectToVisible(newTabRect, animated: animated)
}
(newTabCell.contentView.subviews.first as! RGTabView).selected = true
} else {
let newTabCell = collectionView(tabScrollView, cellForItemAt: IndexPath(row: index, section: 0))
var newTabRect = newTabCell.frame
switch tabbarPosition {
case .Top, .Bottom:
newTabRect.origin.x -= (index == 0 ? tabMargin : tabMargin / 2.0)
newTabRect.size.width += tabMargin
case .Left, .Right:
newTabRect.origin.y -= tabMargin / 2.0
newTabRect.size.height += tabMargin
}
let rect = tabScrollView.convert(newTabRect, to: tabScrollView.superview)
let newTabVisible = tabScrollView.frame.contains(rect)
if !newTabVisible {
var scrollPosition: UICollectionViewScrollPosition = []
if index > currentTabIndex {
switch tabbarPosition {
case .Top, .Bottom:
scrollPosition = .right
case .Left, .Right:
scrollPosition = .bottom
}
} else {
switch tabbarPosition {
case .Top, .Bottom:
scrollPosition = .left
case .Left, .Right:
scrollPosition = .top
}
}
tabScrollView.selectItem( at: IndexPath(row: index, section: 0), animated: animated, scrollPosition: scrollPosition)
tabScrollView.scrollRectToVisible(newTabRect, animated: animated)
}
(newTabCell.contentView.subviews.first as! RGTabView).selected = true
}
currentTabIndex = index
}
private func updatePager(index: Int) {
if let vc: UIViewController = viewControllerAtIndex(index: index) {
if index == currentPageIndex {
pager.setViewControllers([vc], direction: .forward, animated: false, completion: { [weak self] (Bool) -> Void in
self?.animatingToTab = false
})
} else if !(index + 1 == currentPageIndex || index - 1 == currentPageIndex) {
pager.setViewControllers([vc], direction: index < currentPageIndex ? .reverse : .forward, animated: true, completion: { [unowned self] (Bool) -> Void in
self.animatingToTab = false
DispatchQueue.main.async(execute: { [unowned self] in
self.pager.setViewControllers([vc], direction: index < self.currentPageIndex ? .reverse : .forward, animated: false, completion: nil)
})
})
} else {
pager.setViewControllers([vc], direction: index < currentPageIndex ? .reverse : .forward, animated: true, completion: { [weak self] (Bool) -> Void in
self?.animatingToTab = false
})
}
currentPageIndex = index
}
}
private func indexForViewController(vc: UIViewController) -> (Int) {
return pageViewControllers.index(of: vc)
}
func reloadData() {
if let theSource = datasource {
pageCount = theSource.numberOfPagesForViewController(pageViewController: self)
}
pageViewControllers.removeAllObjects()
pageViewControllers = NSMutableArray(capacity: pageCount)
for _ in 0 ..< pageCount {
pageViewControllers.add(NSNull())
}
tabScrollView.reloadData()
selectTabAtIndex(index: currentTabIndex, updatePage: true)
}
// MARK: - UIToolbarDelegate
func position(for bar: UIBarPositioning) -> UIBarPosition {
var position: UIBarPosition = UIBarPosition.top
switch tabbarPosition {
case .Top:
position = UIBarPosition.top
case .Bottom:
position = UIBarPosition.bottom
case .Left, .Right:
position = UIBarPosition.any
}
return position
}
// MARK: - PageViewController Data Source
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
var index: Int = indexForViewController(vc: viewController)
if index == 0 {
return nil
}
index -= 1
return viewControllerAtIndex(index: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
var index: Int = indexForViewController(vc: viewController)
if index == pageCount - 1 {
return nil
}
index += 1
return viewControllerAtIndex(index: index)
}
private func viewControllerAtIndex(index: Int) -> UIViewController? {
if index >= pageCount {
return nil
}
if (pageViewControllers.object(at: index) as AnyObject).isEqual(NSNull()), let vc: UIViewController = datasource?.viewControllerForPageAtIndex(pageViewController: self, index: index) {
let view: UIView = vc.view.subviews[0]
if view is UIScrollView {
let scrollView = (view as! UIScrollView)
var edgeInsets: UIEdgeInsets = scrollView.contentInset
if tabbarPosition == .Top {
edgeInsets.top = tabbar.frame.origin.y + tabbarHeight
} else if tabbarPosition == .Bottom {
edgeInsets.top = tabbar.frame.origin.y
edgeInsets.bottom = tabbarHeight
} else {
edgeInsets.top = tabbar.frame.origin.y
edgeInsets.bottom = 0
}
scrollView.contentInset = edgeInsets
scrollView.scrollIndicatorInsets = edgeInsets
}
pageViewControllers.replaceObject(at: index, with: vc)
}
return pageViewControllers.object(at: index) as? UIViewController
}
// MARK: - PageViewController Delegate
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
if finished && completed, let vc: UIViewController = pager.viewControllers?.first as UIViewController? {
let index: Int = indexForViewController(vc: vc)
selectTabAtIndex(index: index, updatePage: false)
}
}
// MARK: - UICollectionViewDataSource
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return pageCount
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "rgTabCell", for: indexPath)
cell.tag = indexPath.row
if let tabView = tabViewAtIndex(index: indexPath.row) {
for view in cell.contentView.subviews {
view.removeFromSuperview()
}
if indexPath.row == currentTabIndex {
tabView.selected = true
} else {
tabView.selected = false
}
cell.contentView.addSubview(tabView)
}
return cell
}
// MARK: - UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if currentTabIndex != indexPath.row {
selectTabAtIndex(index: indexPath.row, updatePage: true)
}
}
// func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
// <#code#>
// }
//
// func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// <#code#>
// }
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
switch tabbarPosition {
case .Top, .Bottom:
if let theWidth: CGFloat = delegate?.widthForTabAtIndex?(index: indexPath.row) {
return CGSize(width: theWidth, height: tabbarHeight)
} else {
return CGSize(width: tabWidth, height: tabbarHeight)
}
case .Left, .Right:
if let theHeight: CGFloat = delegate?.heightForTabAtIndex?(index: indexPath.row) {
return CGSize(width: tabbarWidth, height: theHeight)
} else {
return CGSize(width: tabbarWidth, height: tabbarWidth)
}
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return tabMargin
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return tabMargin
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
switch tabbarPosition {
case .Top, .Bottom:
return UIEdgeInsets(top: 0, left: tabMargin / 2, bottom: 0, right: tabMargin / 2)
case .Left, .Right:
return UIEdgeInsets(top: tabMargin / 2, left: 0, bottom: tabMargin / 2, right: 0)
}
}
// MARK: - UIScrollView Delegate
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewDidScroll?(scrollView)
}
func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
if scrollView == tabScrollView {
return false
}
if let shouldScroll = pageViewScrollDelegate?.scrollViewShouldScrollToTop?(scrollView) {
return shouldScroll
}
return false
}
func scrollViewDidScrollToTop(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewDidScrollToTop?(scrollView)
}
func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewDidEndScrollingAnimation?(scrollView)
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewWillBeginDragging?(scrollView)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewWillEndDragging?(scrollView, withVelocity: velocity, targetContentOffset: targetContentOffset)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate)
}
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewWillBeginDecelerating?(scrollView)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewDidEndDecelerating?(scrollView)
}
func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
if scrollView == tabScrollView {
return
}
// pageViewScrollDelegate?.scrollViewWillBeginZooming?(scrollView, withView: view)
pageViewScrollDelegate!.scrollViewWillBeginZooming!(scrollView, with: view)
}
func scrollViewDidZoom(_ scrollView: UIScrollView) {
if scrollView == tabScrollView {
return
}
pageViewScrollDelegate?.scrollViewDidZoom?(scrollView)
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
if scrollView == tabScrollView {
return
}
// pageViewScrollDelegate?.scrollViewDidEndZooming?(scrollView, withView: view, atScale: scale)
pageViewScrollDelegate!.scrollViewDidEndZooming!(scrollView, with: view, atScale: scale)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
if scrollView == tabScrollView {
return nil
}
if let view: UIView = pageViewScrollDelegate?.viewForZooming?(in: scrollView) {
return view
}
return nil
}
}
// MARK: - RGTabView
private class RGTabView: UIView {
enum RGTabOrientation {
case Horizontal
case VerticalLeft
case VerticalRight
}
// variables
var selected: Bool = false {
didSet {
if subviews[0] is RGTabBarItem {
(subviews[0] as! RGTabBarItem).selected = selected
} else {
if style == .InactiveFaded {
if selected {
alpha = 1.0
} else {
alpha = 0.566
}
}
setNeedsDisplay()
}
}
}
var indicatorHW: CGFloat = 2.0
var indicatorColor: UIColor = UIColor.lightGray
var orientation: RGTabOrientation = .Horizontal
var style: RGTabStyle = .None
init(frame: CGRect, indicatorColor: UIColor, indicatorHW: CGFloat, style: RGTabStyle, orientation: RGTabOrientation) {
super.init(frame: frame)
self.indicatorColor = indicatorColor
self.orientation = orientation
self.indicatorHW = indicatorHW
self.style = style
initSelf()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initSelf()
}
func initSelf() {
backgroundColor = UIColor.clear
if style == .InactiveFaded {
alpha = 0.566
}
}
private override func draw(_ rect: CGRect) {
super.draw(rect)
if selected {
if !(subviews[0] is RGTabBarItem) {
let bezierPath: UIBezierPath = UIBezierPath()
switch orientation {
case .Horizontal:
bezierPath.move(to: CGPoint(x: 0.0, y: rect.height - indicatorHW / 2.0))
bezierPath.addLine(to: CGPoint(x: rect.width, y: rect.height - indicatorHW / 2.0))
bezierPath.lineWidth = indicatorHW
case .VerticalLeft:
bezierPath.move(to: CGPoint(x: indicatorHW / 2.0, y: 0.0))
bezierPath.addLine(to: CGPoint(x: indicatorHW / 2.0, y: rect.height))
bezierPath.lineWidth = indicatorHW
case .VerticalRight:
bezierPath.move(to: CGPoint(x: rect.width - (indicatorHW / 2.0), y: 0.0))
bezierPath.addLine(to: CGPoint(x: rect.width - (indicatorHW / 2.0), y: rect.height))
bezierPath.lineWidth = indicatorHW
}
indicatorColor.setStroke()