-
Notifications
You must be signed in to change notification settings - Fork 0
/
KRoundLayout.swift
256 lines (192 loc) · 8.33 KB
/
KRoundLayout.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
//
//
// Copyright © 2017 Kenan Atmaca. All rights reserved.
// kenanatmaca.com
//
//
import UIKit
protocol KRoundLayoutDelegate: class {
func getItem(item:(UIImage,Int))
}
fileprivate protocol RoundCollectionDelegate: class {
func didSelect(index:Int)
}
class KRoundLayout: UIButton, RoundCollectionDelegate {
fileprivate lazy var collection:RoundCollection = RoundCollection()
private var rightConstraints = NSLayoutConstraint()
var images:[UIImage] = []
var titles:[String] = []
var delegate:KRoundLayoutDelegate?
var selectColor:UIColor? {
didSet {
collection.selectColor = selectColor
}
}
var borderColors:UIColor? {
didSet {
collection.borderColor = borderColors
}
}
var action:(()->Void)?
override init(frame: CGRect) {
super.init(frame: frame)
collection.delegate = self
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMoveToSuperview() {
collection.images = images
collection.titles = titles
setupButton()
self.superview?.addSubview(collection)
self.superview?.bringSubview(toFront: collection)
collection.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collection.bottomAnchor.constraint(equalTo: self.topAnchor, constant: -10),
collection.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 40),
collection.heightAnchor.constraint(equalToConstant: 60),
collection.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0)
])
}
private func setupButton() {
self.setTitle("", for: .normal)
self.layer.borderWidth = 5
self.layer.borderColor = UIColor.white.withAlphaComponent(0.8).cgColor
self.clipsToBounds = true
self.layer.cornerRadius = frame.height / 2
self.backgroundColor = UIColor.clear
}
func didSelect(index:Int) {
delegate?.getItem(item: (images[index],index))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if action != nil {
self.action!()
}
}
func hide() {
collection.collectionView.isHidden = true
}
func show() {
collection.collectionView.isHidden = false
}
}//
fileprivate class RoundCollection: UIView, UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
fileprivate var delegate:RoundCollectionDelegate?
private var selectCellRow:IndexPath!
var selectFlag:Bool = false
var images:[UIImage] = []
var titles:[String] = []
var selectColor:UIColor?
var borderColor:UIColor?
var collectionView:UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
collection.backgroundColor = UIColor.clear
return collection
}()
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.register(RoundCollectionCell.self, forCellWithReuseIdentifier: "cCell")
collectionView.delegate = self
collectionView.dataSource = self
collectionView.isScrollEnabled = true
collectionView.showsHorizontalScrollIndicator = false
addSubview(collectionView)
}
override func didMoveToSuperview() {
self.frame = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.width - 40, height: 60))
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.frame.size = CGSize(width: self.frame.width / 2, height: 60)
collectionView.isScrollEnabled = true
NSLayoutConstraint.activate([
collectionView.widthAnchor.constraint(equalToConstant: self.frame.width),
collectionView.heightAnchor.constraint(equalToConstant: self.frame.height),
collectionView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0),
collectionView.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cCell", for: indexPath) as! RoundCollectionCell
if images.count == titles.count {
if !cell.isSelected {
cell.imgView.alpha = 1
cell.imgView.layer.borderWidth = 0
cell.textLabel.font = UIFont(name: "Avenir", size: 13)
}
cell.imgView.image = images[indexPath.row]
cell.textLabel.text = titles[indexPath.row]
} else {
fatalError("@ Arrays should be equel items count.")
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 70, height: 60)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
delegate?.didSelect(index: indexPath.row)
let cell = collectionView.cellForItem(at: indexPath) as! RoundCollectionCell
cell.imgView.alpha = 0.7
cell.imgView.layer.borderWidth = 2
cell.imgView.layer.borderColor = selectColor?.cgColor ?? UIColor.black.cgColor
cell.textLabel.font = UIFont.boldSystemFont(ofSize: 14)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
collectionView.reloadItems(at: [indexPath])
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}//
fileprivate class RoundCollectionCell: UICollectionViewCell {
var imgView:UIImageView = {
let view = UIImageView()
view.frame = CGRect(origin: .zero, size: CGSize(width: 42, height: 42))
view.layer.cornerRadius = view.frame.height / 2
view.clipsToBounds = true
view.contentMode = .scaleAspectFill
return view
}()
var textLabel:UILabel = {
let label = UILabel()
label.textAlignment = .center
label.textColor = UIColor.white
label.backgroundColor = UIColor.clear
label.font = UIFont(name: "Avenir", size: 13)
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
contentView.addSubview(imgView)
contentView.addSubview(textLabel)
imgView.translatesAutoresizingMaskIntoConstraints = false
textLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imgView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0),
imgView.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0),
imgView.widthAnchor.constraint(equalToConstant: 42),
imgView.heightAnchor.constraint(equalToConstant: 42)
])
NSLayoutConstraint.activate([
textLabel.topAnchor.constraint(equalTo: imgView.bottomAnchor, constant: 2),
textLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0),
textLabel.widthAnchor.constraint(equalToConstant: 70),
textLabel.heightAnchor.constraint(equalToConstant: 20)
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}//