/
RAMAnimatedTabBarController.swift
334 lines (273 loc) · 12.2 KB
/
RAMAnimatedTabBarController.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
// RAMAnimatedTabBarController.swift
//
// Copyright (c) 11/10/14 Ramotion Inc. (http://ramotion.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
/// UITabBarController with item animations
open class RAMAnimatedTabBarController: UITabBarController {
/**
The animated items displayed by the tab bar.
**/
open var animatedItems: [RAMAnimatedTabBarItem] {
return tabBar.items as? [RAMAnimatedTabBarItem] ?? []
}
/**
Show bottom line for indicating selected item, default value is false
**/
open var isBottomLineShow: Bool = false {
didSet {
if isBottomLineShow {
if bottomLine == nil { createBottomLine() }
} else {
if bottomLine != nil { removeBottomLine() }
}
}
}
/**
Bottom line color
**/
open var bottomLineColor: UIColor = .black {
didSet {
bottomLine?.backgroundColor = bottomLineColor
}
}
/**
Bottom line height
**/
open var bottomLineHeight: CGFloat = 2 {
didSet {
if bottomLineHeight > 0 {
updateBottomLineHeight(to: bottomLineHeight)
}
}
}
/**
Bottom line time of animations duration
**/
open var bottomLineMoveDuration: TimeInterval = 0.3
private(set) var containers: [UIView] = []
open override var viewControllers: [UIViewController]? {
didSet {
initializeContainers()
}
}
open override func setViewControllers(_ viewControllers: [UIViewController]?, animated: Bool) {
super.setViewControllers(viewControllers, animated: animated)
initializeContainers()
}
open override var selectedIndex: Int {
didSet {
self.setBottomLinePosition(index: selectedIndex)
}
}
open override var selectedViewController: UIViewController? {
willSet {
guard let vc = newValue,
let index = viewControllers?.firstIndex(of: vc) else { return }
handleSelection(index: index)
}
}
var lineHeightConstraint: NSLayoutConstraint?
var lineLeadingConstraint: NSLayoutConstraint?
var bottomLine: UIView?
var arrBottomAnchor:[NSLayoutConstraint] = []
var arrViews: [UIView] = []
/**
Hide UITabBar
- parameter isHidden: A Boolean indicating whether the UITabBarController is displayed
*/
@available(*, deprecated, message: "Now you can use UITabBar isHidden")
open func animationTabBarHidden(_ isHidden: Bool) {
tabBar.isHidden = isHidden
}
// MARK: life circle
open override func viewDidLoad() {
super.viewDidLoad()
initializeContainers()
}
override open func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (transitionCoordinatorContext) -> Void in
self.layoutContainers()
}, completion: { (transitionCoordinatorContext) -> Void in
//refresh view once rotation is completed not in will transition as it returns incorrect frame size.Refresh here
})
super.viewWillTransition(to: size, with: coordinator)
}
// MARK: create methods
private func initializeContainers() {
containers.forEach { $0.removeFromSuperview() }
containers.removeAll()
guard let items = tabBar.items else { return }
guard items.count <= 5 else { fatalError("More button not supported") }
for index in 0 ..< items.count {
let viewContainer = UIView()
viewContainer.isExclusiveTouch = true
viewContainer.tag = index
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(itemTap))
viewContainer.addGestureRecognizer(tapGesture)
tabBar.addSubview(viewContainer)
containers.append(viewContainer)
}
if !containers.isEmpty {
createCustomIcons(containers: containers)
}
layoutContainers()
}
private func layoutContainers() {
let itemWidth = tabBar.bounds.width / CGFloat(containers.count)
let isRTL = tabBar.userInterfaceLayoutDirection == .rightToLeft
for (index, container) in containers.enumerated() {
let i = isRTL ? (containers.count - 1 - index) : index
let frame = CGRect(x: itemWidth * CGFloat(i), y: 0, width: itemWidth, height: Theme.tabBarHeight)
container.frame = frame
if let item = tabBar.items?.at(index) as? RAMAnimatedTabBarItem {
let iconView = item.iconView?.icon
let iconSize = iconView?.image?.size ?? CGSize(width: 30, height: 30)
let iconX = (container.frame.width - iconSize.width) / 2 + item.titlePositionAdjustment.horizontal
let iconY = (container.frame.height - iconSize.height) / 2 + Theme.defaultIconVerticalOffset + item.titlePositionAdjustment.vertical
iconView?.frame = CGRect(x: iconX, y: iconY, width: iconSize.width, height: iconSize.height)
let label = item.iconView?.textLabel
let labelSize = label?.sizeThatFits(CGSize.zero) ?? CGSize(width: tabBar.frame.size.width / CGFloat(containers.count), height: 20)
let labelX = (container.frame.width - labelSize.width) / 2 + item.titlePositionAdjustment.horizontal
let labelY = (container.frame.height) / 2 + Theme.defaultTitleVerticalOffset + item.titlePositionAdjustment.vertical
label?.frame = CGRect(x: labelX, y: labelY, width: labelSize.width, height: labelSize.height)
}
}
}
private func createCustomIcons(containers: [UIView]) {
guard let items = tabBar.items as? [RAMAnimatedTabBarItem] else {
fatalError("items must inherit RAMAnimatedTabBarItem")
}
for (index, item) in items.enumerated() {
let container = containers[index]
let renderMode = item.iconColor.cgColor.alpha == 0 ? UIImage.RenderingMode.alwaysOriginal :
UIImage.RenderingMode.alwaysTemplate
let iconImage = item.image ?? item.iconView?.icon.image
let icon = UIImageView(image: iconImage?.withRenderingMode(renderMode))
icon.tintColor = item.iconColor
icon.highlightedImage = item.selectedImage?.withRenderingMode(renderMode)
container.addSubview(icon)
let textLabel = UILabel()
if let title = item.title, !title.isEmpty {
textLabel.text = title
} else {
textLabel.text = item.iconView?.textLabel.text
}
textLabel.backgroundColor = UIColor.clear
textLabel.textColor = item.textColor
textLabel.font = UIFont.systemFont(ofSize: item.textFontSize)
textLabel.textAlignment = NSTextAlignment.center
container.addSubview(textLabel)
container.backgroundColor = (items as [RAMAnimatedTabBarItem])[index].bgDefaultColor
if item.isEnabled == false {
icon.alpha = 0.5
textLabel.alpha = 0.5
}
item.iconView = (icon: icon, textLabel: textLabel)
if 0 == index { // selected first elemet
item.selectedState()
container.backgroundColor = (items as [RAMAnimatedTabBarItem])[index].bgSelectedColor
} else {
item.deselectedState()
container.backgroundColor = (items as [RAMAnimatedTabBarItem])[index].bgDefaultColor
}
item.image = nil
item.title = ""
}
}
// MARK: actions
@objc private func itemTap(gesture: UITapGestureRecognizer) {
guard let index = gesture.view?.tag else { return }
handleSelection(index: index)
}
private func handleSelection(index: Int) {
guard let items = tabBar.items as? [RAMAnimatedTabBarItem] else { return }
let currentIndex = index
if items[currentIndex].isEnabled == false { return }
let controller = children[currentIndex]
if let shouldSelect = delegate?.tabBarController?(self, shouldSelect: controller)
, !shouldSelect {
return
}
if selectedIndex != currentIndex {
let previousItem = items.at(selectedIndex)
let previousContainer: UIView? = previousItem?.iconView?.icon.superview
previousContainer?.backgroundColor = items[selectedIndex].bgDefaultColor
previousItem?.deselectAnimation()
let currentItem: RAMAnimatedTabBarItem = items[currentIndex]
currentItem.playAnimation()
let currentContainer: UIView? = currentItem.iconView?.icon.superview
currentContainer?.backgroundColor = items[currentIndex].bgSelectedColor
selectedIndex = index
} else {
if let navVC = viewControllers?[selectedIndex] as? UINavigationController {
navVC.popToRootViewController(animated: true)
}
}
delegate?.tabBarController?(self, didSelect: controller)
}
}
extension RAMAnimatedTabBarController {
/**
Change selected color for each UITabBarItem
- parameter textSelectedColor: set new color for text
- parameter iconSelectedColor: set new color for icon
*/
open func changeSelectedColor(_ textSelectedColor: UIColor, iconSelectedColor: UIColor) {
let items = tabBar.items as! [RAMAnimatedTabBarItem]
for index in 0 ..< items.count {
let item = items[index]
item.animation.textSelectedColor = textSelectedColor
item.animation.iconSelectedColor = iconSelectedColor
if item == tabBar.selectedItem {
item.selectedState()
}
}
}
/**
Selected UITabBarItem with animaton
- parameter from: Index for unselected animation
- parameter to: Index for selected animation
*/
open func setSelectIndex(from: Int, to: Int) {
selectedIndex = to
guard let items = tabBar.items as? [RAMAnimatedTabBarItem] else {
fatalError("items must inherit RAMAnimatedTabBarItem")
}
let containerFrom = items[from].iconView?.icon.superview
containerFrom?.backgroundColor = items[from].bgDefaultColor
items[from].deselectAnimation()
let containerTo = items[to].iconView?.icon.superview
containerTo?.backgroundColor = items[to].bgSelectedColor
items[to].playAnimation()
}
}
extension RAMAnimatedTabBarController {
enum Theme {
public static let tabBarHeight: CGFloat = 49
public static let defaultTitleVerticalOffset: CGFloat = 10
public static let defaultIconVerticalOffset: CGFloat = -5
}
}
extension UIView {
var userInterfaceLayoutDirection: UIUserInterfaceLayoutDirection {
return UIView.userInterfaceLayoutDirection(for: self.semanticContentAttribute)
}
}