/
TabView.swift
408 lines (337 loc) · 15.5 KB
/
TabView.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
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
//
// TabView.swift
// TabPageViewController
//
// Created by EndouMari on 2016/02/24.
// Copyright © 2016年 EndouMari. All rights reserved.
//
import UIKit
internal class TabView: UIView {
var pageItemPressedBlock: ((_ index: Int, _ direction: UIPageViewController.NavigationDirection) -> Void)?
var pageTabItems: [String] = [] {
didSet {
pageTabItemsCount = pageTabItems.count
beforeIndex = pageTabItems.count
}
}
var layouted: Bool = false
fileprivate var isInfinity: Bool = false
fileprivate var option: TabPageOption = TabPageOption()
fileprivate var beforeIndex: Int = 0
fileprivate var currentIndex: Int = 0
fileprivate var pageTabItemsCount: Int = 0
fileprivate var shouldScrollToItem: Bool = false
fileprivate var pageTabItemsWidth: CGFloat = 0.0
fileprivate var collectionViewContentOffsetX: CGFloat = 0.0
fileprivate var currentBarViewWidth: CGFloat = 0.0
fileprivate var cellForSize: TabCollectionCell!
fileprivate var cachedCellSizes: [IndexPath: CGSize] = [:]
fileprivate var currentBarViewLeftConstraint: NSLayoutConstraint?
@IBOutlet var contentView: UIView!
@IBOutlet fileprivate weak var collectionView: UICollectionView!
@IBOutlet fileprivate weak var currentBarView: UIView!
@IBOutlet fileprivate weak var currentBarViewWidthConstraint: NSLayoutConstraint!
@IBOutlet fileprivate weak var currentBarViewHeightConstraint: NSLayoutConstraint!
@IBOutlet fileprivate weak var bottomBarViewHeightConstraint: NSLayoutConstraint!
init(isInfinity: Bool, option: TabPageOption) {
super.init(frame: CGRect.zero)
self.option = option
self.isInfinity = isInfinity
Bundle(for: TabView.self).loadNibNamed("TabView", owner: self, options: nil)
addSubview(contentView)
contentView.backgroundColor = option.tabBackgroundColor.withAlphaComponent(option.tabBarAlpha)
let top = NSLayoutConstraint(item: contentView,
attribute: .top,
relatedBy: .equal,
toItem: self,
attribute: .top,
multiplier: 1.0,
constant: 0.0)
let left = NSLayoutConstraint(item: contentView,
attribute: .leading,
relatedBy: .equal,
toItem: self,
attribute: .leading,
multiplier: 1.0,
constant: 0.0)
let bottom = NSLayoutConstraint (item: self,
attribute: .bottom,
relatedBy: .equal,
toItem: contentView,
attribute: .bottom,
multiplier: 1.0,
constant: 0.0)
let right = NSLayoutConstraint(item: self,
attribute: .trailing,
relatedBy: .equal,
toItem: contentView,
attribute: .trailing,
multiplier: 1.0,
constant: 0.0)
contentView.translatesAutoresizingMaskIntoConstraints = false
self.addConstraints([top, left, bottom, right])
let bundle = Bundle(for: TabView.self)
let nib = UINib(nibName: TabCollectionCell.cellIdentifier(), bundle: bundle)
collectionView.register(nib, forCellWithReuseIdentifier: TabCollectionCell.cellIdentifier())
cellForSize = nib.instantiate(withOwner: nil, options: nil).first as? TabCollectionCell
collectionView.scrollsToTop = false
currentBarView.backgroundColor = option.currentColor
currentBarViewHeightConstraint.constant = option.currentBarHeight
if !isInfinity {
currentBarView.removeFromSuperview()
collectionView.addSubview(currentBarView)
currentBarView.translatesAutoresizingMaskIntoConstraints = false
let top = NSLayoutConstraint(item: currentBarView,
attribute: .top,
relatedBy: .equal,
toItem: collectionView,
attribute: .top,
multiplier: 1.0,
constant: option.tabHeight - currentBarViewHeightConstraint.constant)
let left = NSLayoutConstraint(item: currentBarView,
attribute: .leading,
relatedBy: .equal,
toItem: collectionView,
attribute: .leading,
multiplier: 1.0,
constant: 0.0)
currentBarViewLeftConstraint = left
collectionView.addConstraints([top, left])
}
bottomBarViewHeightConstraint.constant = 1.0 / UIScreen.main.scale
}
required internal init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
// MARK: - View
extension TabView {
/**
Called when you swipe in isInfinityTabPageViewController, moves the contentOffset of collectionView
- parameter index: Next Index
- parameter contentOffsetX: contentOffset.x of scrollView of isInfinityTabPageViewController
*/
func scrollCurrentBarView(_ index: Int, contentOffsetX: CGFloat) {
var nextIndex = isInfinity ? index + pageTabItemsCount : index
if isInfinity && index == 0 && (beforeIndex - pageTabItemsCount) == pageTabItemsCount - 1 {
// Calculate the index at the time of transition to the first item from the last item of pageTabItems
nextIndex = pageTabItemsCount * 2
} else if isInfinity && (index == pageTabItemsCount - 1) && (beforeIndex - pageTabItemsCount) == 0 {
// Calculate the index at the time of transition from the first item of pageTabItems to the last item
nextIndex = pageTabItemsCount - 1
}
if collectionViewContentOffsetX == 0.0 {
collectionViewContentOffsetX = collectionView.contentOffset.x
}
let currentIndexPath = IndexPath(item: currentIndex, section: 0)
let nextIndexPath = IndexPath(item: nextIndex, section: 0)
if let currentCell = collectionView.cellForItem(at: currentIndexPath) as? TabCollectionCell, let nextCell = collectionView.cellForItem(at: nextIndexPath) as? TabCollectionCell {
nextCell.hideCurrentBarView()
currentCell.hideCurrentBarView()
currentBarView.isHidden = false
if currentBarViewWidth == 0.0 {
currentBarViewWidth = currentCell.frame.width
}
let distance = (currentCell.frame.width / 2.0) + (nextCell.frame.width / 2.0)
let scrollRate = contentOffsetX / frame.width
if abs(scrollRate) > 0.6 {
nextCell.highlightTitle()
currentCell.unHighlightTitle()
} else {
nextCell.unHighlightTitle()
currentCell.highlightTitle()
}
let width = abs(scrollRate) * (nextCell.frame.width - currentCell.frame.width)
if isInfinity {
let scroll = scrollRate * distance
collectionView.contentOffset.x = collectionViewContentOffsetX + scroll
} else {
if scrollRate > 0 {
currentBarViewLeftConstraint?.constant = currentCell.frame.minX + scrollRate * currentCell.frame.width
} else {
currentBarViewLeftConstraint?.constant = currentCell.frame.minX + nextCell.frame.width * scrollRate
}
}
currentBarViewWidthConstraint.constant = currentBarViewWidth + width
}
}
/**
Center the current cell after page swipe
*/
func scrollToHorizontalCenter() {
let indexPath = IndexPath(item: currentIndex, section: 0)
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
collectionViewContentOffsetX = collectionView.contentOffset.x
}
/**
Called in after the transition is complete pages in isInfinityTabPageViewController in the process of updating the current
- parameter index: Next Index
*/
func updateCurrentIndex(_ index: Int, shouldScroll: Bool) {
deselectVisibleCells()
currentIndex = isInfinity ? index + pageTabItemsCount : index
let indexPath = IndexPath(item: currentIndex, section: 0)
moveCurrentBarView(indexPath, animated: !isInfinity, shouldScroll: shouldScroll)
}
/**
Make the tapped cell the current if isInfinity is true
- parameter index: Next IndexPath√
*/
fileprivate func updateCurrentIndexForTap(_ index: Int) {
deselectVisibleCells()
if isInfinity && (index < pageTabItemsCount) || (index >= pageTabItemsCount * 2) {
currentIndex = (index < pageTabItemsCount) ? index + pageTabItemsCount : index - pageTabItemsCount
shouldScrollToItem = true
} else {
currentIndex = index
}
let indexPath = IndexPath(item: index, section: 0)
moveCurrentBarView(indexPath, animated: true, shouldScroll: true)
}
/**
Move the collectionView to IndexPath of Current
- parameter indexPath: Next IndexPath
- parameter animated: true when you tap to move the isInfinityTabCollectionCell
- parameter shouldScroll:
*/
fileprivate func moveCurrentBarView(_ indexPath: IndexPath, animated: Bool, shouldScroll: Bool) {
if shouldScroll {
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: animated)
layoutIfNeeded()
collectionViewContentOffsetX = 0.0
currentBarViewWidth = 0.0
}
if let cell = collectionView.cellForItem(at: indexPath) as? TabCollectionCell {
currentBarView.isHidden = false
if animated && shouldScroll {
cell.isCurrent = true
}
cell.hideCurrentBarView()
currentBarViewWidthConstraint.constant = cell.frame.width
if !isInfinity {
currentBarViewLeftConstraint?.constant = cell.frame.origin.x
}
UIView.animate(withDuration: 0.2, animations: {
self.layoutIfNeeded()
}, completion: { _ in
if !animated && shouldScroll {
cell.isCurrent = true
}
self.updateCollectionViewUserInteractionEnabled(true)
})
}
beforeIndex = currentIndex
}
/**
Touch event control of collectionView
- parameter userInteractionEnabled: collectionViewに渡すuserInteractionEnabled
*/
func updateCollectionViewUserInteractionEnabled(_ userInteractionEnabled: Bool) {
collectionView.isUserInteractionEnabled = userInteractionEnabled
}
/**
Update all of the cells in the display to the unselected state
*/
fileprivate func deselectVisibleCells() {
collectionView
.visibleCells
.compactMap { $0 as? TabCollectionCell }
.forEach { $0.isCurrent = false }
}
}
// MARK: - UICollectionViewDataSource
extension TabView: UICollectionViewDataSource {
internal func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return isInfinity ? pageTabItemsCount * 3 : pageTabItemsCount
}
internal func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TabCollectionCell.cellIdentifier(), for: indexPath) as! TabCollectionCell
configureCell(cell, indexPath: indexPath)
return cell
}
fileprivate func configureCell(_ cell: TabCollectionCell, indexPath: IndexPath) {
let fixedIndex = isInfinity ? indexPath.item % pageTabItemsCount : indexPath.item
cell.item = pageTabItems[fixedIndex]
cell.option = option
cell.isCurrent = fixedIndex == (currentIndex % pageTabItemsCount)
cell.tabItemButtonPressedBlock = { [weak self, weak cell] in
var direction: UIPageViewController.NavigationDirection = .forward
if let pageTabItemsCount = self?.pageTabItemsCount, let currentIndex = self?.currentIndex {
if self?.isInfinity == true {
if (indexPath.item < pageTabItemsCount) || (indexPath.item < currentIndex) {
direction = .reverse
}
} else {
if indexPath.item < currentIndex {
direction = .reverse
}
}
}
self?.pageItemPressedBlock?(fixedIndex, direction)
if cell?.isCurrent == false {
// Not accept touch events to scroll the animation is finished
self?.updateCollectionViewUserInteractionEnabled(false)
}
self?.updateCurrentIndexForTap(indexPath.item)
}
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
// FIXME: Tabs are not displayed when processing is performed during introduction display
if let cell = cell as? TabCollectionCell, layouted {
let fixedIndex = isInfinity ? indexPath.item % pageTabItemsCount : indexPath.item
cell.isCurrent = fixedIndex == (currentIndex % pageTabItemsCount)
}
}
}
// MARK: - UIScrollViewDelegate
extension TabView: UICollectionViewDelegate {
internal func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isDragging {
currentBarView.isHidden = true
let indexPath = IndexPath(item: currentIndex, section: 0)
if let cell = collectionView.cellForItem(at: indexPath) as? TabCollectionCell {
cell.showCurrentBarView()
}
}
guard isInfinity else {
return
}
if pageTabItemsWidth == 0.0 {
pageTabItemsWidth = floor(scrollView.contentSize.width / 3.0)
}
if (scrollView.contentOffset.x <= 0.0) || (scrollView.contentOffset.x > pageTabItemsWidth * 2.0) {
scrollView.contentOffset.x = pageTabItemsWidth
}
}
internal func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
// Accept the touch event because animation is complete
updateCollectionViewUserInteractionEnabled(true)
guard isInfinity else {
return
}
let indexPath = IndexPath(item: currentIndex, section: 0)
if shouldScrollToItem {
// After the moved so as not to sense of incongruity, to adjust the contentOffset at the currentIndex
collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
shouldScrollToItem = false
}
}
}
// MARK: - UICollectionViewDelegateFlowLayout
extension TabView: UICollectionViewDelegateFlowLayout {
internal func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if let size = cachedCellSizes[indexPath] {
return size
}
configureCell(cellForSize, indexPath: indexPath)
let size = cellForSize.sizeThatFits(CGSize(width: collectionView.bounds.width, height: option.tabHeight))
cachedCellSizes[indexPath] = size
return size
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0.0
}
}