forked from dipkasyap/LBZSpinner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBZSpinner.swift
368 lines (290 loc) · 12.6 KB
/
LBZSpinner.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
//
// LBZSpinner.swift
// LBZSpinner
//
// Created by LeBzul on 18/02/2016.
// Copyright © 2016 LeBzul. All rights reserved.
//
import Foundation
import UIKit
@IBDesignable class LBZSpinner : UIView, UITableViewDelegate, UITableViewDataSource {
private var firstDraw:Bool = true
let heightTableviewCell:CGFloat = 45
var heightTableview:CGFloat = 200
//public variable
static var INDEX_NOTHING = 0
//spinner
@IBInspectable var textColor: UIColor = UIColor.grayColor() { didSet{ updateUI() } }
@IBInspectable var lineColor: UIColor = UIColor.grayColor() { didSet{ updateUI() } }
@IBInspectable var list:[String] = [String]() { didSet{ updateUI() } }
@IBInspectable var text: String = "" { didSet{ updateUI() } }
//Drop down list
@IBInspectable var dDLMaxSize: CGFloat = 200
@IBInspectable var dDLColor: UIColor = UIColor.whiteColor()
@IBInspectable var dDLTextColor: UIColor = UIColor.grayColor()
@IBInspectable var dDLStroke: Bool = true
@IBInspectable var dDLStrokeColor: UIColor = UIColor.grayColor()
@IBInspectable var dDLStrokeSize: CGFloat = 1
//Drop down list view back
@IBInspectable var dDLblurEnable: Bool = true
var delegate:LBZSpinnerDelegate!
//actual seleted index
private(set) internal var selectedIndex = INDEX_NOTHING
private var labelValue: UILabel!
private var blurEffectView:UIVisualEffectView!
private var viewChooseDisable: UIView!
private var tableviewChoose: UITableView!
private var tableviewChooseShadow: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
self.initCustomView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.initCustomView()
}
override func prepareForInterfaceBuilder() {
backgroundColor = UIColor.clearColor() // clear black background IB
}
private func initCustomView() {
backgroundColor = UIColor.clearColor() // clear black background
NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged", name: UIDeviceOrientationDidChangeNotification, object: nil)
//Open spinner click
let gesture = UITapGestureRecognizer(target: self, action: "openSpinner:")
addGestureRecognizer(gesture)
heightTableview = heightTableviewCell*CGFloat(list.count)
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
if firstDraw {
//create spinner label
labelValue = UILabel(frame: bounds)
addSubview(labelValue)
updateUI()
firstDraw = false
}
drawCanvas(frame: rect)
}
func changeSelectedIndex(index:Int) {
if list.count > index {
selectedIndex = index
text = list[selectedIndex]
updateUI()
if (delegate != nil) {
delegate.spinnerChoose(self,index:selectedIndex, value: list[selectedIndex])
}
}
}
private func updateUI() {
if (labelValue != nil) {
labelValue.text = text
labelValue.textColor = textColor
}
setNeedsDisplay()
}
//Config spinner style
func decoratedSpinner(textColor:UIColor!,lineColor:UIColor!,text:String!) {
if(textColor != nil) { self.textColor=textColor }
if(lineColor != nil) { self.lineColor=lineColor }
if(text != nil) { self.text=text }
}
//Config drop down list style
func decoratedDropDownList(backgroundColor:UIColor!,textColor:UIColor!,withStroke:Bool!,strokeSize:CGFloat!,strokeColor:UIColor!) {
if(backgroundColor != nil) { dDLColor=backgroundColor }
if(textColor != nil) { dDLTextColor=textColor }
if(withStroke != nil) { dDLStroke=withStroke }
if(strokeSize != nil) { dDLStrokeSize=strokeSize }
if(strokeColor != nil) { dDLStrokeColor=strokeColor }
}
//Update drop down list
func updateList(list:[String]) {
self.list = list;
heightTableview = heightTableviewCell*CGFloat(list.count)
if(tableviewChoose != nil) {
tableviewChoose.reloadData()
}
}
//Open spinner animation
func openSpinner(sender:UITapGestureRecognizer){
heightTableview = heightTableviewCell*CGFloat(list.count)
let parentView = findLastUsableSuperview()
let globalPoint = convertPoint(bounds.origin, toView:parentView) // position spinner in superview
viewChooseDisable = UIView(frame: parentView.frame) // view back click
if(dDLblurEnable) { // with blur effect
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.alpha = 0 // blur effect alpha
blurEffectView.frame = viewChooseDisable.bounds
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
viewChooseDisable.addSubview(blurEffectView)
}
var expandBottomDirection = true
if((globalPoint.y+heightTableview) < parentView.frame.height) {
expandBottomDirection = true
} else if((globalPoint.y-heightTableview) > 0) {
expandBottomDirection = false
} else {
//find best direction
let margeBot = parentView.frame.height - globalPoint.y
let margeTop = parentView.frame.height - (parentView.frame.height - globalPoint.y)
if( margeBot > margeTop ) {
expandBottomDirection = true
heightTableview = margeBot - 5
} else {
expandBottomDirection = false
heightTableview = margeTop - 5
}
}
if(heightTableview > dDLMaxSize) {
heightTableview = dDLMaxSize
}
// expand bottom animation
if (expandBottomDirection) {
tableviewChoose = UITableView(frame: CGRectMake(globalPoint.x , globalPoint.y, frame.size.width, 0))
tableviewChooseShadow = UIView(frame: tableviewChoose.frame)
UIView.animateWithDuration(0.3,
delay: 0.0,
options: UIViewAnimationOptions.TransitionFlipFromBottom,
animations: {
self.tableviewChoose.frame.size.height = self.heightTableview
self.tableviewChooseShadow.frame.size.height = self.heightTableview
if self.blurEffectView != nil {
self.blurEffectView.alpha = 0.5
}
},
completion: { finished in
})
}
// expand top animation
else {
tableviewChoose = UITableView(frame: CGRectMake(globalPoint.x , globalPoint.y, frame.size.width, self.frame.height))
tableviewChooseShadow = UIView(frame: tableviewChoose.frame)
UIView.animateWithDuration(0.3,
delay: 0.0,
options: UIViewAnimationOptions.TransitionFlipFromBottom,
animations: {
self.tableviewChoose.frame.origin.y = globalPoint.y-self.heightTableview+self.frame.height
self.tableviewChoose.frame.size.height = self.heightTableview
self.tableviewChooseShadow.frame.origin.y = globalPoint.y-self.heightTableview+self.frame.height
self.tableviewChooseShadow.frame.size.height = self.heightTableview
if self.blurEffectView != nil {
self.blurEffectView.alpha = 0.5
}
},
completion: { finished in
})
}
// config tableview drop down list
tableviewChoose.backgroundColor = dDLColor
tableviewChoose.tableFooterView = UIView() //Eliminate Extra separators below UITableView
tableviewChoose.delegate = self
tableviewChoose.dataSource = self
tableviewChoose.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
tableviewChoose.userInteractionEnabled = true
tableviewChoose.showsHorizontalScrollIndicator = false
tableviewChoose.showsVerticalScrollIndicator = false
tableviewChoose.separatorStyle = UITableViewCellSeparatorStyle.None
tableviewChoose.layer.cornerRadius = 5
//Show stroke
if(dDLStroke) {
tableviewChoose.layer.borderColor = dDLStrokeColor.CGColor
tableviewChoose.layer.borderWidth = dDLStrokeSize
}
// config shadow drop down list
tableviewChooseShadow.backgroundColor = dDLColor
tableviewChooseShadow.layer.shadowOpacity = 0.5;
tableviewChooseShadow.layer.shadowOffset = CGSizeMake(3, 3);
tableviewChooseShadow.layer.shadowRadius = 5;
tableviewChooseShadow.layer.cornerRadius = 5
tableviewChooseShadow.layer.masksToBounds = false
tableviewChooseShadow.clipsToBounds = false
// add to superview
parentView.addSubview(viewChooseDisable)
parentView.addSubview(tableviewChooseShadow)
parentView.addSubview(tableviewChoose)
// close spinner click back
let gesture = UITapGestureRecognizer(target: self, action: "closeSpinner")
viewChooseDisable.addGestureRecognizer(gesture)
}
// close spinner animation
func closeSpinner() {
if(tableviewChoose != nil) {
UIView.animateWithDuration(0.3,
delay: 0.0,
options: UIViewAnimationOptions.TransitionFlipFromBottom,
animations: {
self.tableviewChoose.alpha = 0.0
self.tableviewChooseShadow.alpha = 0.0
self.viewChooseDisable.alpha = 0.0
},
completion: { finished in
// delete dropdown list
self.tableviewChoose.removeFromSuperview()
self.viewChooseDisable.removeFromSuperview()
self.tableviewChooseShadow.removeFromSuperview()
self.tableviewChoose = nil
self.tableviewChooseShadow = nil
self.viewChooseDisable = nil
})
}
}
// find usable superview
private func findLastUsableSuperview() -> UIView {
return (window?.subviews[0])!
}
//draw background spinner
private func drawCanvas(frame frame: CGRect = CGRectMake(0, 0, 86, 11)) {
let bezierPath = UIBezierPath()
bezierPath.moveToPoint(CGPointMake(frame.maxX - 11, frame.maxY))
bezierPath.addLineToPoint(CGPointMake(frame.maxX, frame.maxY))
bezierPath.addLineToPoint(CGPointMake(frame.maxX, frame.maxY - 11))
bezierPath.addLineToPoint(CGPointMake(frame.maxX - 11, frame.maxY))
bezierPath.closePath()
bezierPath.lineCapStyle = .Square;
bezierPath.lineJoinStyle = .Bevel;
lineColor.setFill()
bezierPath.fill()
let rectanglePath = UIBezierPath(rect: CGRectMake(frame.minX, frame.minY + frame.height - 1, frame.width, 1))
lineColor.setFill()
rectanglePath.fill()
}
func orientationChanged()
{
/*
if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
{}
if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
{}
*/
closeSpinner()
}
/**
* TableView Delegate method
**/
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
labelValue.text = list[indexPath.row]
if (delegate != nil) {
delegate.spinnerChoose(self,index: indexPath.row, value: list[indexPath.row])
}
selectedIndex = indexPath.row
closeSpinner()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as UITableViewCell
cell.contentView.backgroundColor = dDLColor
cell.textLabel?.backgroundColor = UIColor.clearColor()
cell.detailTextLabel?.backgroundColor = UIColor.clearColor()
cell.textLabel?.text = list[indexPath.row]
cell.textLabel?.textColor = dDLTextColor
return cell
}
}
protocol LBZSpinnerDelegate{
func spinnerChoose(spinner:LBZSpinner, index:Int,value:String)
}