-
Notifications
You must be signed in to change notification settings - Fork 20
/
SGTabbedPager.swift
276 lines (246 loc) · 11.7 KB
/
SGTabbedPager.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
//
// SGTabbedPager.swift
// SGViewPager
//
// Copyright (c) 2012-2015 Simon Grätzer
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import UIKit
public protocol SGTabbedPagerDatasource {
func numberOfViewControllers() -> Int
func viewController(page:Int) -> UIViewController
func viewControllerTitle(page:Int) -> String
}
public protocol SGTabbedPagerDelegate {
//func willShowViewController(page:Int) -> Void
func didShowViewController(page:Int) -> Void
}
public class SGTabbedPager: UIViewController, UIScrollViewDelegate {
public var datasource : SGTabbedPagerDatasource? = nil
public var delegate : SGTabbedPagerDelegate? = nil
private let tabHeight = CGFloat(44);
private var titleScrollView, contentScrollView : UIScrollView!
private var viewControllers = [UIViewController]()
private var viewControllerCount : Int = 0
private var tabButtons = [UIButton]()
private var bottomLine, tabIndicator : UIView!
private var selectedIndex : Int = 0
private var enableParallex = true
public var selectedViewController : UIViewController {
get {
return viewControllers[selectedIndex]
}
}
public var tabColor : UIColor = UIColor(red: 0, green: 0.329, blue: 0.624, alpha: 1) {
didSet {
if bottomLine != nil {
bottomLine.backgroundColor = tabColor
}
if tabIndicator != nil {
tabIndicator.backgroundColor = tabColor
}
}
}
// MARK: View Controller state restauration
public override func encodeRestorableStateWithCoder(coder: NSCoder) {
super.encodeRestorableStateWithCoder(coder)
coder.encodeInteger(selectedIndex, forKey: "selectedIndex")
}
public override func decodeRestorableStateWithCoder(coder: NSCoder) {
super.decodeRestorableStateWithCoder(coder)
selectedIndex = coder.decodeIntegerForKey("selectedIndex")
}
public override func loadView() {
super.loadView()
titleScrollView = UIScrollView(frame: CGRectZero)
titleScrollView.translatesAutoresizingMaskIntoConstraints = false
titleScrollView.autoresizingMask = [.FlexibleWidth, .FlexibleBottomMargin]
titleScrollView.backgroundColor = UIColor.whiteColor()
titleScrollView.canCancelContentTouches = false
titleScrollView.showsHorizontalScrollIndicator = false
titleScrollView.bounces = false
titleScrollView.delegate = self
self.view.addSubview(titleScrollView)
bottomLine = UIView(frame: CGRectZero)
bottomLine.backgroundColor = tabColor
titleScrollView.addSubview(bottomLine)
tabIndicator = UIView(frame: CGRectZero)
tabIndicator.backgroundColor = tabColor
titleScrollView.addSubview(tabIndicator)
contentScrollView = UIScrollView(frame: CGRectZero)
contentScrollView.translatesAutoresizingMaskIntoConstraints = false
contentScrollView.autoresizingMask = [.FlexibleWidth, .FlexibleBottomMargin]
contentScrollView.backgroundColor = UIColor.whiteColor()
contentScrollView.delaysContentTouches = false
contentScrollView.showsHorizontalScrollIndicator = false
contentScrollView.pagingEnabled = true
contentScrollView.scrollEnabled = true
contentScrollView.delegate = self
self.view.addSubview(contentScrollView)
}
public override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.reloadData()
}
public override func viewWillLayoutSubviews() {
self.layout()
}
public override func willTransitionToTraitCollection(newCollection: UITraitCollection, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if titleScrollView != nil {
titleScrollView.delegate = nil
contentScrollView.delegate = nil
coordinator.animateAlongsideTransition(nil, completion: {_ -> Void in
self.titleScrollView.delegate = self
self.contentScrollView.delegate = self
self.switchPage(self.selectedIndex, animated: false)
})
}
}
// MARK: Public methods
public func reloadData() {
for vc in viewControllers {
vc.willMoveToParentViewController(nil)
vc.view.removeFromSuperview()
vc.removeFromParentViewController()
}
viewControllers.removeAll(keepCapacity: true)
if let cc = datasource?.numberOfViewControllers() {
self.viewControllerCount = cc
for i in 0..<viewControllerCount {
let vc = datasource!.viewController(i)
addChildViewController(vc)
let size = contentScrollView.frame.size
vc.view.frame = CGRectMake(size.width * CGFloat(i), 0, size.width, size.height)
contentScrollView.addSubview(vc.view)
vc.didMoveToParentViewController(self)
viewControllers.append(vc)
}
generateTabs()
layout()
// Sanity check for restored selectedIndex values
selectedIndex = min(viewControllerCount-1, selectedIndex)
if selectedIndex > 0 {// Happens for example in case of a restore
switchPage(selectedIndex, animated: false)
}
}
}
public func switchPage(index :Int, animated : Bool) {
let frame = CGRectMake(contentScrollView.frame.size.width * CGFloat(index), 0,
contentScrollView.frame.size.width, contentScrollView.frame.size.height) ;
if frame.origin.x < contentScrollView.contentSize.width {
// It doesn't look good if the tab's jump back and then gets animated back
// by the code inside 'scrollViewDidScroll', but we only need to
// disable parallax if scrollViewDidEndScrollingAnimation is gonna be called afterwards
enableParallex = !animated
var point = tabButtons[index].frame.origin
point.x -= (titleScrollView.bounds.size.width - tabButtons[index].frame.size.width)/2
titleScrollView.setContentOffset(point, animated: animated)
contentScrollView.scrollRectToVisible(frame, animated: animated)
}
}
// MARK: Helpers methods
/// Generate the fitting UILabel's
private func generateTabs() {
for label in self.tabButtons {
label.removeFromSuperview()
}
self.tabButtons.removeAll(keepCapacity: true)
let font = UIFont(name: "HelveticaNeue-Thin", size: 20)
for i in 0..<self.viewControllerCount {
let button = UIButton(type: .Custom)
button.setTitle(self.datasource?.viewControllerTitle(i), forState: .Normal)
button.setTitleColor(UIColor.blackColor(), forState: .Normal)
button.titleLabel?.font = font
button.titleLabel?.textAlignment = .Center
button.sizeToFit()
button.addTarget(self, action: #selector(SGTabbedPager.receivedButtonTab(_:)), forControlEvents: .TouchUpInside)
self.tabButtons.append(button)
self.titleScrollView.addSubview(button)
}
}
/// Action method to move the pager in the right direction
public func receivedButtonTab(sender :UIButton) {
if let i = tabButtons.indexOf(sender) {
switchPage(i, animated:true)
}
}
private func layout() {
var size = self.view.bounds.size
titleScrollView.frame = CGRectMake(0, 0, size.width, tabHeight)
contentScrollView.frame = CGRectMake(0, tabHeight,
self.view.bounds.size.width, self.view.bounds.size.height - tabHeight)
var currentX : CGFloat = 0
size = contentScrollView.frame.size
for i in 0..<self.viewControllerCount {
let label = tabButtons[i]
if i == 0 {
currentX += (size.width - label.frame.size.width)/2
}
label.frame = CGRectMake(currentX, 0.0, label.frame.size.width, tabHeight)
if i == viewControllerCount-1 {
currentX += (size.width - label.frame.size.width)/2 + label.frame.size.width
} else {
currentX += label.frame.size.width + 30
}
let vc = viewControllers[i]
vc.view.frame = CGRectMake(size.width * CGFloat(i), 0, size.width, size.height)
}
titleScrollView.contentSize = CGSizeMake(currentX, tabHeight)
contentScrollView.contentSize = CGSizeMake(size.width * CGFloat(viewControllerCount), size.height)
bottomLine.frame = CGRectMake(0, tabHeight-1, titleScrollView.contentSize.width, 1)
layoutTabIndicator()
}
/// Repositions the indication marker below the tab
func layoutTabIndicator() {
let labelF = tabButtons[selectedIndex].frame
tabIndicator.frame = CGRectMake(labelF.origin.x, labelF.size.height-4, labelF.size.width, 4)
}
// MARK: UIScrollViewDelegate
public func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == contentScrollView {
let pageWidth = scrollView.frame.size.width;
var page = (scrollView.contentOffset.x - pageWidth / 2) / pageWidth + 1;
let next = Int(floor(page))
if next != selectedIndex {
selectedIndex = next
// Don't animate unless we wan't the parallax effect
UIView.animateWithDuration(enableParallex ? 0.3 : 0,
animations:layoutTabIndicator,
completion:{_ in
self.delegate?.didShowViewController(self.selectedIndex)
})
}
var ignored : Double = 0.0
page = scrollView.contentOffset.x / pageWidth;// Current page index with fractions
let index = Int(page)
if enableParallex && index + 1 < viewControllerCount {
// We are using the difference from one label to the other to implement varying speeds
// for our custom parallax effect, so every title is exactly centered if you are on a page
let diff = tabButtons[index+1].frame.origin.x - tabButtons[index].frame.origin.x
let centering1 = (titleScrollView.bounds.size.width - tabButtons[index].frame.size.width)/2
let centering2 = (titleScrollView.bounds.size.width - tabButtons[index+1].frame.size.width)/2
let frac = CGFloat(modf(Double(page), &ignored))// Only fraction part remains, Eg 3.4344 -> 0.4344
let newXOff = tabButtons[index].frame.origin.x + diff * frac - centering1 * (1-frac) - centering2 * frac;
titleScrollView.contentOffset = CGPointMake(fmax(0, newXOff), 0)
}
}
}
/// Since it looks better, we disable the parralel movement effect on the title scrollview
/// before we do a scroll animation
public func scrollViewDidEndScrollingAnimation(scrollView: UIScrollView) {
if scrollView == contentScrollView {
enableParallex = true// Always enable after an animation
}
}
}