LTPageTitleView.swift
23.1 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
//
// LTPageTitleView.swift
// LTScrollView_Example
//
// Created by 高刘通 on 2018/9/6.
// Copyright © 2018年 CocoaPods. All rights reserved.
//
import UIKit
typealias scrollIndexHandle = () -> Int
public typealias LTCreateViewControllerHandle = (Int) -> Void
public typealias LTDidSelectTitleViewHandle = (Int) -> Void
@objc public class LTPageTitleView: UIView {
//传递进来的scrollView
var mainScrollView: UIScrollView?
//标题数组
private var titles: [String]
//设置默认布局,具体设置请查看LTLayout类
private var layout: LTLayout
//存储文本宽度数组
private lazy var glt_textWidths: [CGFloat] = []
//存储线的宽度数组
private lazy var glt_lineWidths: [CGFloat] = []
//存储所有的button
private lazy var glt_buttons: [LTPageTitleItemView] = []
//当前的位置
private lazy var glt_currentIndex: Int = 0
//标记是否是点击
private lazy var isClick: Bool = false
//起始偏移量
private lazy var glt_startOffsetX: CGFloat = 0.0
//是否有点击动画
private lazy var glt_isClickScrollAnimation = false
//是否有点击动画 其他类使用
final lazy var isClickScrollAnimation = false
//是否是第一次加载
private lazy var isFirstLoad: Bool = true
//选中某个位置的回调
var scrollIndexHandle: scrollIndexHandle?
//创建子控制器的回调
var glt_createViewControllerHandle: LTCreateViewControllerHandle?
//选中标题view回调
var glt_didSelectTitleViewHandle: LTDidSelectTitleViewHandle?
//设置pageView的代理
weak var delegate: LTPageViewDelegate?
//返回所有的数组
func allItemViews() -> [LTPageTitleItemView] {
return glt_buttons
}
private lazy var glt_titleRGBlColor = getRGBWithColor(layout.titleColor ?? NORMAL_BASE_COLOR)
private lazy var glt_selectTitleRGBlColor = getRGBWithColor(layout.titleSelectColor ?? SELECT_BASE_COLOR)
private lazy var sliderScrollView: UIScrollView = {
let sliderScrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: bounds.width, height: bounds.height))
sliderScrollView.showsHorizontalScrollIndicator = false
sliderScrollView.bounces = false
if #available(iOS 11.0, *) {
sliderScrollView.contentInsetAdjustmentBehavior = .never
}
return sliderScrollView
}()
private lazy var pageBottomLineView: UIView = {
let pageBottomLineView = UIView(frame: CGRect(x: 0, y: bounds.height - layout.pageBottomLineHeight, width: bounds.width, height: layout.pageBottomLineHeight))
pageBottomLineView.backgroundColor = layout.pageBottomLineColor
return pageBottomLineView
}()
private lazy var sliderLineView: UIImageView = {
let sliderLineView = UIImageView(frame: CGRect(x: layout.lrMargin, y: 0, width: 0, height: 20))
sliderLineView.image = UIImage(named: "page_paw")
// sliderLineView.backgroundColor = .yellow
sliderLineView.isHidden = layout.isHiddenSlider
return sliderLineView
}()
@objc public init(frame: CGRect, titles: [String], layout: LTLayout) {
self.titles = titles
self.layout = layout
super.init(frame: frame)
backgroundColor = layout.titleViewBgColor
setupSubViews()
}
/* 滚动到某个位置 */
@objc public func scrollToIndex(index: Int) {
var index = index
glt_setupScrollToIndex(&index)
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension LTPageTitleView {
func setupSubViews() {
addSubview(sliderScrollView)
// addSubview(pageBottomLineView)
setupButtonsLayout()
sliderScrollView.addSubview(sliderLineView)
sliderScrollView.sendSubviewToBack(sliderLineView)
if titles.count == 3 {
sliderLineView.center.x = self.center.x/2.0 + 10
}
}
}
extension LTPageTitleView {
private func setupButtonsLayout() {
guard titles.count != 0 else { return }
//是否自定义了item的宽
let _isLayoutWidth = layout.layoutItemWidths.count > 0
// 将所有的宽度计算出来放入数组
for text in titles {
if layout.isAverage {
let textAverageW = (glt_width - layout.lrMargin * 2.0 - layout.titleMargin * CGFloat(titles.count - 1)) / CGFloat(titles.count)
if !_isLayoutWidth {
glt_textWidths.append(textAverageW)
}
glt_lineWidths.append(textAverageW)
continue
}
if text.count == 0 {
if !_isLayoutWidth {
glt_textWidths.append(60)
}
glt_lineWidths.append(60)
continue
}
let textW = text.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: 8), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font : layout.titleFont ?? UIFont.systemFont(ofSize: 16)], context: nil).size.width
if !_isLayoutWidth {
glt_textWidths.append(textW)
}
glt_lineWidths.append(textW)
}
if _isLayoutWidth {
glt_textWidths = layout.layoutItemWidths
}
// 按钮布局
var upX: CGFloat = layout.lrMargin
let subH = glt_height - 12
for index in 0..<titles.count {
let subW = glt_textWidths[index]
let buttonReact = CGRect(x: upX, y: 6, width: subW, height: subH)
let button = subButton(frame: buttonReact, flag: index, title: titles[index], parentView: sliderScrollView)
let color = (index == 0 ? layout.titleSelectColor : layout.titleColor)
button.setTitleColor(color, for: .normal)
upX = button.frame.origin.x + subW + layout.titleMargin
if titles.count == 3 {
button.center.x = self.center.x/2.0*CGFloat(index+1)
}
glt_buttons.append(button)
}
let firstButton = glt_buttons[0]
let firstLineWidth = glt_lineWidths[0]
let firstTextWidth = glt_textWidths[0]
if layout.isNeedScale {
firstButton.transform = CGAffineTransform(scaleX: layout.scale , y: layout.scale)
}
// lineView的宽度为第一个的宽度
if layout.sliderWidth == glt_sliderDefaultWidth {
if layout.isAverage {
sliderLineView.glt_width = firstLineWidth
sliderLineView.glt_left = (firstTextWidth - firstLineWidth) * 0.5 + layout.lrMargin
} else {
sliderLineView.glt_width = firstButton.glt_width
sliderLineView.glt_left = firstButton.glt_left
}
} else {
sliderLineView.glt_width = layout.sliderWidth
sliderLineView.glt_left = ((firstTextWidth + layout.lrMargin * 2) - layout.sliderWidth) * 0.5
}
if layout.bottomLineCornerRadius != 0.0 {
sliderLineView.layer.cornerRadius = layout.bottomLineCornerRadius
sliderLineView.layer.masksToBounds = true
sliderLineView.clipsToBounds = true
}
if layout.isAverage {
sliderScrollView.contentSize = CGSize(width: glt_width, height: 0)
return
}
// 计算sliderScrollView的contentSize
let sliderContenSizeW = upX - layout.titleMargin + layout.lrMargin
if sliderContenSizeW < glt_width {
sliderScrollView.glt_width = sliderContenSizeW
}
//最后多加了一个 layout.titleMargin, 这里要减去
sliderScrollView.contentSize = CGSize(width: sliderContenSizeW, height: 0)
}
@objc private func titleSelectIndex(_ btn: UIButton) {
setupTitleSelectIndex(btn.tag)
}
private func setupTitleSelectIndex(_ btnSelectIndex: Int) {
guard let scrollView = mainScrollView else { return }
if glt_currentIndex == btnSelectIndex || scrollView.isDragging || scrollView.isDecelerating {
return
}
let totalW = bounds.width
isClick = true
glt_isClickScrollAnimation = true
scrollView.setContentOffset(CGPoint(x: totalW * CGFloat(btnSelectIndex), y: 0), animated: isClickScrollAnimation)
if isClickScrollAnimation {
return
}
let nextButton = glt_buttons[btnSelectIndex]
if layout.sliderWidth == glt_sliderDefaultWidth {
if layout.isAverage {
let adjustX = (nextButton.frame.size.width - glt_lineWidths[btnSelectIndex]) * 0.5
sliderLineView.frame.origin.x = nextButton.frame.origin.x + adjustX + 10
sliderLineView.frame.size.width = glt_lineWidths[btnSelectIndex]
}else {
sliderLineView.frame.origin.x = nextButton.frame.origin.x + 10
sliderLineView.frame.size.width = nextButton.frame.width
}
}else {
setupSliderLineViewWidth(currentButton: nextButton)
}
glt_currentIndex = btnSelectIndex
}
}
//MARK: 处理刚进入滚动到第几个位置
extension LTPageTitleView {
private func glt_setupScrollToIndex(_ index:inout Int) {
if index >= titles.count {
print("超过最大数量限制, 请正确设置值, 默认这里取第一个")
index = 0
}
if isClickScrollAnimation {
let nextButton = glt_buttons[index]
if layout.sliderWidth == glt_sliderDefaultWidth {
if layout.isAverage {
let adjustX = (nextButton.frame.size.width - glt_lineWidths[index]) * 0.5
sliderLineView.frame.origin.x = nextButton.frame.origin.x + adjustX + 10
sliderLineView.frame.size.width = glt_lineWidths[index]
}else {
sliderLineView.frame.origin.x = nextButton.frame.origin.x + 10
sliderLineView.frame.size.width = nextButton.frame.width
}
}else {
if isFirstLoad {
setupSliderLineViewWidth(currentButton: glt_buttons[index])
isFirstLoad = false
}
}
}
setupTitleSelectIndex(index)
}
}
extension LTPageTitleView: LTPageViewDelegate {
private func scrollViewDidScrollOffsetX(_ offsetX: CGFloat) {
_ = setupLineViewX(offsetX: offsetX)
let index = currentIndex()
if glt_currentIndex != index {
//如果开启滚动动画
if isClickScrollAnimation {
//如果不是点击事件继续在这个地方设置偏移
if !glt_isClickScrollAnimation {
setupSlierScrollToCenter(offsetX: offsetX, index: index)
}
}else {
//设置滚动的位置
setupSlierScrollToCenter(offsetX: offsetX, index: index)
}
// 如果是点击的话
if isClick {
let upButton = glt_buttons[glt_currentIndex]
let currentButton = glt_buttons[index]
if layout.isNeedScale {
UIView.animate(withDuration: 0.2, animations: {
currentButton.transform = CGAffineTransform(scaleX: self.layout.scale , y: self.layout.scale)
upButton.transform = CGAffineTransform(scaleX: 1.0 , y: 1.0 )
})
}
setupButtonStatusAnimation(upButton: upButton, currentButton: currentButton)
}
if layout.isColorAnimation == false {
let upButton = glt_buttons[glt_currentIndex]
let currentButton = glt_buttons[index]
setupButtonStatusAnimation(upButton: upButton, currentButton: currentButton)
}
//如果开启滚动动画
if isClickScrollAnimation {
//如果不是点击事件继续在这个地方设置偏移
if !glt_isClickScrollAnimation {
glt_createViewControllerHandle?(index)
glt_didSelectTitleViewHandle?(index)
}
}else {
//默认的设置
glt_createViewControllerHandle?(index)
glt_didSelectTitleViewHandle?(index)
}
glt_currentIndex = index
}
isClick = false
}
//MARK: 让title的ScrollView滚动到中心点位置
private func setupSlierScrollToCenter(offsetX: CGFloat, index: Int) {
let currentButton = glt_buttons[index]
let btnCenterX = currentButton.center.x
var scrollX = btnCenterX - sliderScrollView.bounds.width * 0.5
if scrollX < 0 {
scrollX = 0
}
if scrollX > sliderScrollView.contentSize.width - sliderScrollView.bounds.width {
scrollX = sliderScrollView.contentSize.width - sliderScrollView.bounds.width
}
sliderScrollView.setContentOffset(CGPoint(x: scrollX, y: 0), animated: true)
}
private func setupButtonStatusAnimation(upButton: UIButton, currentButton: UIButton) {
upButton.setTitleColor(layout.titleColor, for: .normal)
currentButton.setTitleColor(layout.titleSelectColor, for: .normal)
}
private func currentIndex() -> Int {
return scrollIndexHandle?() ?? 0
}
private func setupIsClickScrollAnimation(index: Int) {
if !isClickScrollAnimation {
return
}
for button in glt_buttons {
if button.tag == index {
if layout.isNeedScale {
button.transform = CGAffineTransform(scaleX: layout.scale , y: layout.scale)
}
button.setTitleColor(self.layout.titleSelectColor, for: .normal)
}else {
if layout.isNeedScale {
button.transform = CGAffineTransform(scaleX: 1.0 , y: 1.0)
}
button.setTitleColor(self.layout.titleColor, for: .normal)
}
}
glt_isClickScrollAnimation = false
}
}
extension LTPageTitleView {
//MARK: 设置线的移动
private func setupLineViewX(offsetX: CGFloat) -> Bool {
if isClick {
return false
}
//目的是改变它的值,让制滑动第一个和最后一个的时候(-0.5),导致数组下标越界
var offsetX = offsetX
let scrollW = bounds.width
let scrollContenSizeW: CGFloat = bounds.width * CGFloat(titles.count)
// 目的是滑动到最后一个的时候 不让其再往后滑动
if offsetX + scrollW >= scrollContenSizeW {
if layout.sliderWidth == glt_sliderDefaultWidth {
let adjustX = (glt_textWidths.last! - glt_lineWidths.last!) * 0.5
sliderLineView.frame.origin.x = layout.lrMargin + adjustX + 10
}else {
setupSliderLineViewWidth(currentButton: glt_buttons.last!)
}
offsetX = scrollContenSizeW - scrollW - 0.5
}
// 目的是滑动到第一个的时候 不让其再往前滑动
if offsetX <= 0 {
if layout.sliderWidth == glt_sliderDefaultWidth {
let adjustX = (glt_textWidths[0] - glt_lineWidths[0]) * 0.5
sliderLineView.frame.origin.x = layout.lrMargin + adjustX + 10
}else {
sliderLineView.frame.origin.x = ((glt_textWidths[0] + layout.lrMargin * 2) - layout.sliderWidth) * 0.5 + 10
}
offsetX = 0.5
}
var nextIndex = Int(offsetX / scrollW)
var sourceIndex = Int(offsetX / scrollW)
//向下取整 目的是减去整数位,只保留小数部分
var progress = (offsetX / scrollW) - floor(offsetX / scrollW)
if offsetX > glt_startOffsetX { // 向左滑动
//向左滑动 下个位置比源位置下标 多1
nextIndex = nextIndex + 1
}else { // 向右滑动
//向右滑动 由于源向下取整的缘故 必须补1 nextIndex则恰巧是原始位置
sourceIndex = sourceIndex + 1
progress = 1 - progress
}
let nextButton = glt_buttons[nextIndex]
let currentButton = glt_buttons[sourceIndex]
if layout.isColorAnimation {
let colorDelta = (glt_selectTitleRGBlColor.0 - glt_titleRGBlColor.0, glt_selectTitleRGBlColor.1 - glt_titleRGBlColor.1, glt_selectTitleRGBlColor.2 - glt_titleRGBlColor.2)
let nextColor = UIColor(r: glt_titleRGBlColor.0 + colorDelta.0 * progress, g: glt_titleRGBlColor.1 + colorDelta.1 * progress, b: glt_titleRGBlColor.2 + colorDelta.2 * progress)
let currentColor = UIColor(r: glt_selectTitleRGBlColor.0 - colorDelta.0 * progress, g: glt_selectTitleRGBlColor.1 - colorDelta.1 * progress, b: glt_selectTitleRGBlColor.2 - colorDelta.2 * progress)
currentButton.setTitleColor(currentColor, for: .normal)
nextButton.setTitleColor(nextColor, for: .normal)
}
if layout.isNeedScale {
let scaleDelta = (layout.scale - 1.0) * progress
currentButton.transform = CGAffineTransform(scaleX: layout.scale - scaleDelta, y: layout.scale - scaleDelta)
nextButton.transform = CGAffineTransform(scaleX: 1.0 + scaleDelta, y: 1.0 + scaleDelta)
}
// 判断是否是自定义Slider的宽度(这里指没有自定义)
if layout.sliderWidth == glt_sliderDefaultWidth {
if layout.isAverage {
/*
* 原理:(按钮的宽度 - 线的宽度)/ 2 = 线的X便宜量
* 如果是不是平均分配 按钮的宽度 = 线的宽度
*/
// 计算宽度的该变量
let moveW = glt_lineWidths[nextIndex] - glt_lineWidths[sourceIndex]
// (按钮的宽度 - 线的宽度)/ 2
let nextButtonAdjustX = (nextButton.frame.size.width - glt_lineWidths[nextIndex]) * 0.5
// (按钮的宽度 - 线的宽度)/ 2
let currentButtonAdjustX = (currentButton.frame.size.width - glt_lineWidths[sourceIndex]) * 0.5
// x的该变量
let moveX = (nextButton.frame.origin.x + nextButtonAdjustX) - (currentButton.frame.origin.x + currentButtonAdjustX)
self.sliderLineView.frame.size.width = glt_lineWidths[sourceIndex] + moveW * progress
self.sliderLineView.frame.origin.x = currentButton.frame.origin.x + moveX * progress + currentButtonAdjustX + 10
}else {
// 计算宽度的该变量
let moveW = nextButton.frame.width - currentButton.frame.width
// 计算X的该变量
let moveX = nextButton.frame.origin.x - currentButton.frame.origin.x
self.sliderLineView.frame.size.width = currentButton.frame.width + moveW * progress + 10
self.sliderLineView.frame.origin.x = currentButton.frame.origin.x + moveX * progress - 0.25
}
}else {
/*
* 原理:按钮的最左边X(因为有lrMargin,这里必须减掉) 以及 按钮的相对右边X(注意不是最右边,因为每个按钮的X都有一个lrMargin, 所以相对右边则有两个才能保证按钮的位置,这个和titleMargin无关)
*/
let maxNextLeft = nextButton.frame.origin.x - layout.lrMargin
let maxNextRight = maxNextLeft + layout.lrMargin * 2.0 + nextButton.frame.size.width
let originNextX = (maxNextRight - maxNextLeft - layout.sliderWidth) * 0.5 + maxNextLeft
let maxLeft = currentButton.frame.origin.x - layout.lrMargin
let maxRight = maxLeft + layout.lrMargin * 2.0 + currentButton.frame.size.width
let originX = (maxRight - maxLeft - layout.sliderWidth) * 0.5 + maxLeft
let moveX = originNextX - originX
self.sliderLineView.frame.origin.x = originX + moveX * progress + 10
sliderLineView.frame.size.width = layout.sliderWidth
}
return false
}
// currentButton将要滚动到的按钮
private func setupSliderLineViewWidth(currentButton: UIButton) {
let maxLeft = currentButton.frame.origin.x - layout.lrMargin
let maxRight = maxLeft + layout.lrMargin * 2 + currentButton.frame.size.width
let originX = (maxRight - maxLeft - layout.sliderWidth) * 0.5 + maxLeft
sliderLineView.frame.origin.x = originX + 10
sliderLineView.frame.size.width = layout.sliderWidth
}
}
extension LTPageTitleView {
public func glt_scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollViewDidScrollOffsetX(scrollView.contentOffset.x)
}
public func glt_scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
delegate?.glt_scrollViewWillBeginDragging?(scrollView)
glt_startOffsetX = scrollView.contentOffset.x
}
public func glt_scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
delegate?.glt_scrollViewDidEndDragging?(scrollView, willDecelerate: decelerate)
}
public func glt_scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
if glt_isClickScrollAnimation {
let index = currentIndex()
glt_createViewControllerHandle?(index)
setupSlierScrollToCenter(offsetX: scrollView.contentOffset.x, index: index)
setupIsClickScrollAnimation(index: index)
glt_didSelectTitleViewHandle?(index)
}
}
}
extension LTPageTitleView {
private func getRGBWithColor(_ color : UIColor) -> (CGFloat, CGFloat, CGFloat) {
guard let components = color.cgColor.components else {
fatalError("请使用RGB方式给标题颜色赋值")
}
return (components[0] * 255, components[1] * 255, components[2] * 255)
}
}
extension LTPageTitleView {
@discardableResult
private func subButton(frame: CGRect, flag: Int, title: String?, parentView: UIView) -> LTPageTitleItemView {
let button = LTPageTitleItemView(type: .custom)
button.frame = frame
button.tag = flag
button.setTitle(title, for: .normal)
button.addTarget(self, action: #selector(titleSelectIndex(_:)), for: .touchUpInside)
button.titleLabel?.font = layout.titleFont
parentView.addSubview(button)
return button
}
}