-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMIndexBar.swift
179 lines (150 loc) · 5.57 KB
/
CMIndexBar.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
// Converted to Swift 4 by Swiftify v4.2.36673 - https://objectivec2swift.com/
//
// indexBar.h
//
// Created by Craig Merchant on 07/04/2011.
// Copyright 2011 RaptorApps. All rights reserved.
//
import Foundation
import QuartzCore
protocol CMIndexBarDelegate: NSObjectProtocol {
func indexSelectionDidChange(_ indexBar: CMIndexBar, index: Int, title: String)
}
class CMIndexBar: UIView {
weak var delegate: (NSObject & CMIndexBarDelegate)?
var highlightedBackgroundColor: UIColor?
var textColor: UIColor?
var font: UIFont?
override init(frame: CGRect) {
super.init(frame: frame)
// Defaults
backgroundColor = UIColor.clear
textColor = UIColor.darkGray
highlightedBackgroundColor = UIColor.lightGray
font = UIFont(name: "TimesNewRomanPS-BoldMT", size: 12.0)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func setIndexes(_ indexes: [String]) {
clearIndex()
let count = indexes.count
for i in 0..<count {
var ypos: Float
if i == 0 {
ypos = 0
} else if i == count - 1 {
ypos = Float(frame.size.height - 24.0)
} else {
var sectionheight = Float(((frame.size.height - 24.0) / CGFloat(count)))
sectionheight = sectionheight + (sectionheight / Float(count))
ypos = sectionheight * Float(i)
}
let label = UILabel(frame: CGRect(x: 0, y: CGFloat(ypos), width: frame.size.width, height: 24.0))
label.textAlignment = .center
label.text = indexes[i]
label.font = font ?? label.font
label.backgroundColor = UIColor.clear
label.textColor = textColor ?? label.textColor
addSubview(label)
}
}
func clearIndex() {
for subview: UIView in subviews {
subview.removeFromSuperview()
}
}
override func layoutSubviews() {
super.layoutSubviews()
var i: Int = 0
var subcount: Int = 0
for subview: UIView in subviews {
if subview is UILabel {
subcount += 1
}
}
for subview: UIView in subviews {
if subview is UILabel {
var ypos: Float
if i == 0 {
ypos = 0
} else if i == subcount - 1 {
ypos = Float(frame.size.height - 24.0)
} else {
var sectionheight = Float(((frame.size.height - 24.0) / CGFloat(subcount)))
sectionheight = sectionheight + (sectionheight / Float(subcount))
ypos = sectionheight * Float(i)
}
subview.frame = CGRect(x: 0, y: CGFloat(ypos), width: frame.size.width, height: 24.0)
i += 1
}
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
let backgroundview = UIView(frame: CGRect(x: 0, y: 0, width: bounds.size.width, height: bounds.size.height))
backgroundview.backgroundColor = highlightedBackgroundColor
backgroundview.layer.cornerRadius = bounds.size.width / 2
backgroundview.layer.masksToBounds = true
backgroundview.tag = 767
addSubview(backgroundview)
sendSubviewToBack(backgroundview)
if delegate == nil {
return
}
let touchPoint: CGPoint? = (event?.touches(for: self)?.first)?.location(in: self)
if (touchPoint?.x ?? 0.0) < 0 {
return
}
var title = ""
var count: Int = 0
for subview in subviews {
if let subview = subview as? UILabel {
if (touchPoint?.y ?? 0.0) < subview.frame.origin.y + subview.frame.size.height {
count += 1
title = subview.text ?? ""
break
}
count += 1
title = subview.text ?? ""
}
}
delegate?.indexSelectionDidChange(self, index: count, title: title)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
if delegate == nil {
return
}
let touchPoint: CGPoint? = (event?.touches(for: self)?.first)?.location(in: self)
if (touchPoint?.x ?? 0.0) < 0 {
return
}
var title = ""
var count = 0
for subview in subviews {
if let subview = subview as? UILabel {
if (touchPoint?.y ?? 0.0) < subview.frame.origin.y + subview.frame.size.height {
count += 1
title = subview.text ?? ""
break
}
count += 1
title = subview.text ?? ""
}
}
delegate?.indexSelectionDidChange(self, index: count, title: title)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
touchesEndedOrCancelled(touches, with: event)
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
touchesEndedOrCancelled(touches, with: event)
}
func touchesEndedOrCancelled(_ touches: Set<AnyHashable>?, with event: UIEvent?) {
let backgroundView = viewWithTag(767)
backgroundView?.removeFromSuperview()
}
}