/
TagViewController.swift
143 lines (122 loc) · 6.3 KB
/
TagViewController.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
//
// TagViewController.swift
// iOS Sample
//
// Copyright (c) 2017 Kazuhiro Hayashi
//
// 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
import PagingKit
class TagViewController: UIViewController {
var contentViewController: PagingContentViewController!
var menuViewController: PagingMenuViewController!
@IBOutlet weak var plusButton: UIButton!
let dataSource: [(menu: (title: String, color: UIColor), content: UIViewController)] = ["Martinez", "Alfred", "Louis", "Justin", "Tim", "Deborah", "Michael", "Choi", "Hamilton", "Decker", "Johnson", "George"].map {
let title = $0
let vc = UIStoryboard(name: "ContentTableViewController", bundle: nil).instantiateInitialViewController() as! ContentTableViewController
let color = UIColor(
red: (CGFloat(arc4random_uniform(255)) + 1) / 255,
green: (CGFloat(arc4random_uniform(255)) + 1) / 255,
blue: (CGFloat(arc4random_uniform(255)) + 1) / 255,
alpha: 1
)
return (menu: (title: title, color: color), content: vc)
}
override func viewDidLoad() {
super.viewDidLoad()
// add shadow to button
do {
plusButton.layer.shadowColor = UIColor.gray.cgColor
plusButton.layer.shadowOpacity = 0.5
plusButton.layer.shadowOffset = CGSize(width: 0.5, height: 0)
plusButton.layer.shouldRasterize = true
plusButton.layer.rasterizationScale = UIScreen.main.scale
plusButton.layer.shadowPath = UIBezierPath(rect: plusButton.bounds).cgPath
}
contentViewController?.scrollView.bounces = true
menuViewController?.register(nib: UINib(nibName: "TagMenuCell", bundle: nil), forCellWithReuseIdentifier: "identifier")
menuViewController?.reloadData(with: 4)
contentViewController?.reloadData(with: 4)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
menuViewController?.reloadData()
contentViewController?.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? PagingContentViewController {
contentViewController = vc
contentViewController?.delegate = self
contentViewController?.dataSource = self
} else if let vc = segue.destination as? PagingMenuViewController {
menuViewController = vc
menuViewController?.dataSource = self
menuViewController?.delegate = self
}
}
}
extension TagViewController: PagingContentViewControllerDataSource {
func numberOfItemsForContentViewController(viewController: PagingContentViewController) -> Int {
return dataSource.count
}
func contentViewController(viewController: PagingContentViewController, viewControllerAt index: Int) -> UIViewController {
return dataSource[index].content
}
}
extension TagViewController: PagingMenuViewControllerDataSource {
func numberOfItemsForMenuViewController(viewController: PagingMenuViewController) -> Int {
return dataSource.count
}
func menuViewController(viewController: PagingMenuViewController, widthForItemAt index: Int) -> CGFloat {
TagMenuCell.sizingCell.titieLabel.text = dataSource[index].menu.title
var referenceSize = UIView.layoutFittingCompressedSize
referenceSize.height = viewController.view.bounds.height
let size = TagMenuCell.sizingCell.systemLayoutSizeFitting(referenceSize)
return size.width
}
func menuViewController(viewController: PagingMenuViewController, cellForItemAt index: Int) -> PagingMenuViewCell {
let cell = viewController.dequeueReusableCell(withReuseIdentifier: "identifier", for: index) as! TagMenuCell
cell.titieLabel.text = dataSource[index].menu.title
cell.contentView.backgroundColor = dataSource[index].menu.color
let percent: CGFloat = viewController.currentFocusedIndex == index ? 1 : 0
cell.focus(percent: percent)
return cell
}
}
extension TagViewController: PagingMenuViewControllerDelegate {
func menuViewController(viewController: PagingMenuViewController, didSelect page: Int, previousPage: Int) {
viewController.visibleCells.compactMap { $0 as? TagMenuCell }.forEach { $0.focus(percent: 0, animated: true) }
let nextCell = viewController.cellForItem(at: page) as? TagMenuCell
nextCell?.focus(percent: 1, animated: true)
contentViewController?.scroll(to: page, animated: true)
}
}
extension TagViewController: PagingContentViewControllerDelegate {
func contentViewController(viewController: PagingContentViewController, didManualScrollOn index: Int, percent: CGFloat) {
let adjucentCell = menuViewController.cellForItem(at: viewController.adjucentPageIndex) as? TagMenuCell
let currentCell = menuViewController.cellForItem(at: index) as? TagMenuCell
currentCell?.focus(percent: 1 - abs(percent))
adjucentCell?.focus(percent: abs(percent))
menuViewController?.scroll(index: index, percent: percent, animated: false)
}
}