-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTCustomTabBar.swift
More file actions
276 lines (241 loc) · 10.5 KB
/
Copy pathSTCustomTabBar.swift
File metadata and controls
276 lines (241 loc) · 10.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
//
// STCustomTabBar.swift
// STBaseProject
//
// Created by 寒江孤影 on 2024/01/01.
//
import UIKit
public protocol STCustomTabBarDelegate: AnyObject {
func customTabBar(_ tabBar: STCustomTabBar, didSelectItemAt index: Int)
}
public class STCustomTabBar: UIView {
public weak var delegate: STCustomTabBarDelegate?
public var preferredLayoutHeight: CGFloat { self.config.height }
@IBInspectable public var isLiquidGlassEnabled: Bool = false {
didSet {
self.updateLiquidGlassBackground()
}
}
@IBInspectable public var liquidGlassTintColor: UIColor = UIColor.white.withAlphaComponent(0.18) {
didSet {
self.updateLiquidGlassBackground()
}
}
@IBInspectable public var liquidGlassHighlightOpacity: Float = 0.35 {
didSet {
self.updateLiquidGlassBackground()
}
}
@IBInspectable public var liquidGlassBorderColor: UIColor = UIColor.white.withAlphaComponent(0.35) {
didSet {
self.updateLiquidGlassBackground()
}
}
private var selectedIndex: Int = 0
private var itemViews: [STTabBarItemView] = []
private var itemModels: [STTabBarItemModel] = []
private var config = STTabBarConfig()
private var heightConstraint: NSLayoutConstraint?
private var topBorderHeightConstraint: NSLayoutConstraint?
override public init(frame: CGRect) {
super.init(frame: frame)
self.setupUI()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
self.setupUI()
}
private func setupUI() {
self.clipsToBounds = false
self.addSubview(self.backgroundImageView)
self.backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.backgroundImageView.topAnchor.constraint(equalTo: topAnchor),
self.backgroundImageView.leadingAnchor.constraint(equalTo: leadingAnchor),
self.backgroundImageView.trailingAnchor.constraint(equalTo: trailingAnchor),
self.backgroundImageView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
self.addSubview(self.contentView)
self.contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.contentView.topAnchor.constraint(equalTo: topAnchor),
self.contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
self.contentView.trailingAnchor.constraint(equalTo: trailingAnchor),
self.contentView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
self.addSubview(self.topBorderView)
self.topBorderView.translatesAutoresizingMaskIntoConstraints = false
let borderHeight = self.topBorderView.heightAnchor.constraint(equalToConstant: self.config.topBorderWidth)
self.topBorderHeightConstraint = borderHeight
NSLayoutConstraint.activate([
self.topBorderView.topAnchor.constraint(equalTo: topAnchor),
self.topBorderView.leadingAnchor.constraint(equalTo: leadingAnchor),
self.topBorderView.trailingAnchor.constraint(equalTo: trailingAnchor),
borderHeight
])
self.updateAppearance()
}
/// 配置 TabBar
public func configure(items: [STTabBarItemModel], config: STTabBarConfig = STTabBarConfig()) {
self.itemModels = items
self.config = config
self.updateAppearance()
self.clampSelectedIndexForCurrentItems()
self.setupItems()
}
public func setSelectedIndex(_ index: Int) {
guard index >= 0, index < self.itemViews.count else { return }
if self.selectedIndex < self.itemViews.count {
self.itemViews[self.selectedIndex].updateSelection(.deselected)
}
self.selectedIndex = index
self.itemViews[self.selectedIndex].updateSelection(.selected)
}
public func updateBadgeCount(at index: Int, count: Int) {
guard index >= 0, index < self.itemViews.count else { return }
self.itemViews[index].updateBadgeCount(count)
}
public func updateConfig(_ config: STTabBarConfig) {
self.config = config
self.updateAppearance()
self.itemViews.forEach { $0.reapplyTabBarConfig(config) }
}
public func st_setLiquidGlassBackground(
tintColor: UIColor = UIColor.white.withAlphaComponent(0.18),
highlightOpacity: Float = 0.35,
borderColor: UIColor = UIColor.white.withAlphaComponent(0.35)
) {
self.liquidGlassTintColor = tintColor
self.liquidGlassHighlightOpacity = highlightOpacity
self.liquidGlassBorderColor = borderColor
self.isLiquidGlassEnabled = true
self.updateLiquidGlassBackground()
}
public func getSelectedIndex() -> Int { self.selectedIndex }
public func getItemCount() -> Int { self.itemModels.count }
private func clampSelectedIndexForCurrentItems() {
if self.itemModels.isEmpty {
self.selectedIndex = 0
} else {
self.selectedIndex = min(self.selectedIndex, self.itemModels.count - 1)
}
}
private func setupItems() {
self.itemViews.forEach { $0.removeFromSuperview() }
self.itemViews.removeAll()
for (index, model) in self.itemModels.enumerated() {
let itemView = STTabBarItemView()
itemView.configure(with: model, config: self.config, selection: index == self.selectedIndex ? .selected : .deselected) { [weak self] in
self?.handleItemTap(at: index)
}
contentView.addSubview(itemView)
itemViews.append(itemView)
}
self.setupItemConstraints()
}
private func setupItemConstraints() {
guard !self.itemViews.isEmpty else { return }
for (index, itemView) in self.itemViews.enumerated() {
let model = self.itemModels[index]
itemView.translatesAutoresizingMaskIntoConstraints = false
if model.isIrregular {
let protrusion = model.irregular?.protrusionHeight ?? STTabBarIrregularStyle.standard.protrusionHeight
NSLayoutConstraint.activate([
itemView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor, constant: -protrusion / 2),
itemView.heightAnchor.constraint(equalToConstant: self.config.height + protrusion)
])
} else {
NSLayoutConstraint.activate([
itemView.topAnchor.constraint(equalTo: self.contentView.topAnchor),
itemView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor)
])
}
if index == 0 {
itemView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
} else {
NSLayoutConstraint.activate([
itemView.leadingAnchor.constraint(equalTo: self.itemViews[index - 1].trailingAnchor),
itemView.widthAnchor.constraint(equalTo: self.itemViews[index - 1].widthAnchor)
])
}
if index == self.itemViews.count - 1 {
itemView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor).isActive = true
}
}
}
private func updateAppearance() {
self.alpha = 1.0
self.backgroundColor = self.config.backgroundColor
if let image = self.config.backgroundImage {
self.backgroundImageView.image = image
self.backgroundImageView.isHidden = false
} else {
self.backgroundImageView.image = nil
self.backgroundImageView.isHidden = true
}
if let existing = self.heightConstraint {
existing.constant = self.config.height
} else {
let c = heightAnchor.constraint(equalToConstant: self.config.height)
self.heightConstraint = c
c.isActive = true
}
self.topBorderView.isHidden = !self.config.showTopBorder
self.topBorderView.backgroundColor = self.config.topBorderColor
self.topBorderHeightConstraint?.constant = self.config.topBorderWidth
if self.config.showShadow {
self.layer.shadowColor = self.config.shadowColor.cgColor
self.layer.shadowOffset = self.config.shadowOffset
self.layer.shadowRadius = self.config.shadowRadius
self.layer.shadowOpacity = self.config.shadowOpacity
self.layer.masksToBounds = false
} else {
self.layer.shadowOpacity = 0
}
self.updateLiquidGlassBackground()
}
private func updateLiquidGlassBackground() {
guard self.isLiquidGlassEnabled else {
self.st_disableLiquidGlassBackground()
return
}
let glassView = self.st_enableLiquidGlassBackground(
tintColor: self.liquidGlassTintColor,
highlightOpacity: self.liquidGlassHighlightOpacity,
borderColor: self.liquidGlassBorderColor
)
self.insertSubview(glassView, aboveSubview: self.backgroundImageView)
}
private func handleItemTap(at index: Int) {
guard index != self.selectedIndex else { return }
self.delegate?.customTabBar(self, didSelectItemAt: index)
}
private lazy var backgroundImageView: UIImageView = {
let iv = UIImageView()
iv.clipsToBounds = true
iv.contentMode = .scaleToFill
iv.isHidden = true
return iv
}()
private lazy var contentView: UIView = {
let view = UIView()
view.clipsToBounds = false
return view
}()
private lazy var topBorderView = UIView()
}
extension STCustomTabBar {
public static func createDefault(with items: [STTabBarItemModel]) -> STCustomTabBar {
let tabBar = STCustomTabBar()
tabBar.configure(items: items)
return tabBar
}
public static func createSimple(titles: [String], normalImages: [UIImage?], selectedImages: [UIImage?]) -> STCustomTabBar {
var items: [STTabBarItemModel] = []
for i in 0..<titles.count {
let model = STTabBarItemModel(title: titles[i], normalImage: i < normalImages.count ? normalImages[i] : nil, selectedImage: i < selectedImages.count ? selectedImages[i] : nil)
items.append(model)
}
return createDefault(with: items)
}
}