-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWDDropDownMenu.swift
More file actions
497 lines (344 loc) · 17.4 KB
/
WDDropDownMenu.swift
File metadata and controls
497 lines (344 loc) · 17.4 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
//
// WDDropDownMenu.swift
// WhoDo-Swift
//
// Created by mac on 2017/4/25.
// Copyright © 2017年 lanxiao. All rights reserved.
//
import UIKit
import SnapKit
// cell被点击后的结果回调函数
public typealias CellClickedBlock = (_ row:NSInteger) -> Void
final class WDDropDownMenu: UIView {
var tableview :UITableView!
var view :UIView!
var shapHeight : CGFloat = 10
var rect1 :CGRect!
var DataArray = [String]()
let shapeLayer = CAShapeLayer()
var cellClickedBlock: CellClickedBlock!
static var single = WDDropDownMenu.init(frame: CGRect(x:0,y:0,width:UIScreen.main.bounds.size.width,height:UIScreen.main.bounds.size.height))
private override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clear
self.addView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func addView(){
view = UIView.init(frame: self.frame)
view.backgroundColor = UIColor.clear
self.addSubview(view)
let singleRecognizer:UITapGestureRecognizer!
singleRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(singleClick))
singleRecognizer.numberOfTapsRequired = 1;
//给self.view添加一个手势监测;
view.addGestureRecognizer(singleRecognizer)
tableview = UITableView.init()
tableview.layer.masksToBounds = true;
tableview.separatorStyle = .none;
tableview.layer.cornerRadius = 6.0;
tableview.layer.borderWidth = 1;
tableview.layer.borderColor = UIColor.orange.cgColor
tableview.backgroundColor = .white
tableview.delegate = self
tableview.dataSource = self
self.addSubview(tableview)
}
func singleClick(){
self.dismiss()
}
}
extension WDDropDownMenu : UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.DataArray.count;
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell:DropDownMenuTableViewCell = DropDownMenuTableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier:"cell")
cell.backgroundColor = .white
cell.label?.text = self.DataArray[indexPath.row]
if indexPath.row == (self.DataArray.count-1){
cell.lineView.isHidden = true
}else{
cell.lineView.isHidden = false
}
return cell
}
}
extension WDDropDownMenu : UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return 40;
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
tableView.deselectRow(at: indexPath, animated: true)
cellClickedBlock!(indexPath.row)
self.dismiss()
}
}
extension WDDropDownMenu{
public func SetDataArrayandPoint(array:[String],rect:CGRect){
self.DataArray = array;
let ScreenWidth = UIScreen.main.bounds.size.width
let ScreenHeight = UIScreen.main.bounds.size.height
let tableviewWidth : CGFloat
tableviewWidth = 60
var tableviewHeight : CGFloat
tableviewHeight = CGFloat(array.count * 40)
if tableviewHeight > 4 * 40{
tableviewHeight = 160
}
let CenterPoint = CGPoint(x:rect.origin.x + rect.size.width/2,y:rect.origin.y + rect.size.height/2)
var direction : NSInteger
// 0 底部 1左边 2上边 3右边
direction = 0
if ((rect.size.height + rect.origin.y + tableviewHeight) <= ScreenHeight && (ScreenWidth - CenterPoint.x > tableviewWidth/2 + shapHeight) && (CenterPoint.x > tableviewWidth/2 + shapHeight)){ // 下合适
direction = 0
print("下边")
}else if(rect.origin.y) >= tableviewHeight && (ScreenWidth - CenterPoint.x > tableviewWidth/2 + shapHeight) && (CenterPoint.x > tableviewWidth/2 + shapHeight){//上合适
direction = 2
print("上边")
}else if(CenterPoint.x >= tableviewWidth + shapHeight){//左合适
direction = 1
print("左边")
}else{//右合适
direction = 3
print("右边")
}
self.jisuandian(rect: rect, direction: direction)
self.tableview.contentSize = CGSize(width:60, height:array.count * 40)
}
func jisuandian(rect:CGRect, direction:NSInteger) {
self.rect1 = rect
let ScreenWidth = UIScreen.main.bounds.size.width
let ScreenHeight = UIScreen.main.bounds.size.height
let tableviewWidth : CGFloat
tableviewWidth = 60
var tableviewHeight : CGFloat
tableviewHeight = CGFloat(self.DataArray.count * 40)
if tableviewHeight > 4 * 40{
tableviewHeight = 160
}
let CenterPoint = CGPoint(x:rect.origin.x + rect.size.width/2,y:rect.origin.y + rect.size.height/2)
switch direction {
case 0://下
if (ScreenWidth - CenterPoint.x > tableviewWidth/2 + shapHeight) && (CenterPoint.x > tableviewWidth/2 + shapHeight){
self.tableview.snp.remakeConstraints { (make) in
make.top.equalTo(rect.origin.y + rect.size.height + shapHeight)
make.left.equalTo(CenterPoint.x - 30)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else if((ScreenWidth - CenterPoint.x < tableviewWidth/2 + 3)){ //右边小于所需的距离
self.tableview.snp.remakeConstraints { (make) in
make.top.equalTo(rect.origin.y + rect.size.height + shapHeight)
make.right.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else{ //左边小于所需的距离
self.tableview.snp.remakeConstraints { (make) in
make.top.equalTo(rect.origin.y + rect.size.height + shapHeight)
make.left.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}
break
case 1: //左
if (ScreenHeight - CenterPoint.y > tableviewHeight/2 + shapHeight) && (CenterPoint.y > tableviewHeight/2 + shapHeight){//上下都合适
self.tableview.snp.remakeConstraints { (make) in
make.right.equalTo( rect.origin.x - shapHeight )
make.top.equalTo(rect.origin.y - tableviewHeight/2)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else if (ScreenHeight - CenterPoint.y < tableviewHeight/2 + shapHeight){//下边小于所需距离
self.tableview.snp.remakeConstraints { (make) in
make.right.equalTo( rect.origin.x - shapHeight)
make.bottom.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else{
self.tableview.snp.remakeConstraints { (make) in
make.right.equalTo( rect.origin.x - shapHeight)
make.top.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}
break
case 2: //上
if (ScreenWidth - CenterPoint.x > tableviewWidth/2 + shapHeight) && (CenterPoint.x > tableviewWidth/2 + shapHeight){
self.tableview.snp.remakeConstraints { (make) in
make.top.equalTo( rect.origin.y - shapHeight - tableviewHeight)
make.left.equalTo(CenterPoint.x - 30)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else if((ScreenWidth - CenterPoint.x < tableviewWidth/2 + shapHeight)){ //右边小于所需的距离
self.tableview.snp.remakeConstraints { (make) in
make.top.equalTo( rect.origin.y - shapHeight - tableviewHeight)
make.right.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else{ //左边小于所需的距离
self.tableview.snp.remakeConstraints { (make) in
make.top.equalTo( rect.origin.y - shapHeight - tableviewHeight)
make.left.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}
break
case 3: //右
if (ScreenHeight - CenterPoint.y > tableviewHeight/2 + shapHeight) && (CenterPoint.y > tableviewHeight/2 + shapHeight){//上下都合适
self.tableview.snp.remakeConstraints { (make) in
make.left.equalTo( rect.origin.x + rect.size.width + shapHeight)
make.top.equalTo(rect.origin.y + rect.size.height/2 - tableviewHeight/2)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else if (ScreenHeight - CenterPoint.y < tableviewHeight/2 + shapHeight){//下边小于所需距离
self.tableview.snp.remakeConstraints { (make) in
make.left.equalTo( rect.origin.x + rect.size.width + shapHeight)
make.bottom.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}else{
self.tableview.snp.remakeConstraints { (make) in
make.left.equalTo( rect.origin.x + rect.size.width + shapHeight)
make.top.equalTo(3)
make.width.equalTo(60)
make.height.equalTo(tableviewHeight)
}
}
break
default: break
}
self.addbezierPath(rect: rect, direction: direction, rect1: self.tableview.frame)
}
func addbezierPath(rect:CGRect, direction:NSInteger ,rect1:CGRect){
shapeLayer.removeAllAnimations()
let aPath = UIBezierPath()
aPath.lineWidth = 1.0 // 线条宽度
aPath.lineCapStyle = CGLineCap.round // 线条拐角
aPath.lineJoinStyle = CGLineJoin.round // 终点处理
if direction == 0{//下
// Set the starting point of the shape.
aPath.move(to: CGPoint(x:rect.origin.x + rect.size.width/2,y:rect.origin.y + rect.size.height))
// Draw the lines
aPath.addLine(to: CGPoint(x:rect.origin.x + rect.size.width/2 - 10,y:rect.origin.y + rect.size.height + 10))
aPath.addLine(to: CGPoint(x:rect.origin.x + rect.size.width/2 + 10,y:rect.origin.y + rect.size.height + 10))
aPath.close() // 最后一条线通过调用closePath方法得到
}
if direction == 2{//上
aPath.move(to: CGPoint(x:rect.origin.x + rect.size.width/2, y:rect.origin.y))
aPath.addLine(to: CGPoint(x:rect.origin.x + rect.size.width/2 - 10, y:rect.origin.y - 10))
aPath.addLine(to: CGPoint(x:rect.origin.x + rect.size.width/2 + 10, y:rect.origin.y - 10))
aPath.close()
}
if direction == 1{//左
aPath.move(to: CGPoint(x:rect.origin.x ,y:rect.origin.y + rect.size.height/2))
aPath.addLine(to: CGPoint(x:rect.origin.x - 10,y:rect.origin.y + rect.size.height/2 - 10))
aPath.addLine(to: CGPoint(x:rect.origin.x - 10,y:rect.origin.y + rect.size.height/2 + 10))
aPath.close()
}
if direction == 3{//右
aPath.move(to: CGPoint(x:rect.origin.x + rect.size.width ,y:rect.origin.y + rect.size.height/2))
aPath.addLine(to: CGPoint(x:rect.origin.x + rect.size.width + 10,y:rect.origin.y + rect.size.height/2 - 10))
aPath.addLine(to: CGPoint(x:rect.origin.x + rect.size.width + 10,y:rect.origin.y + rect.size.height/2 + 10))
aPath.close()
}
//创建一个Layer用于显示
//let shapeLayer = CAShapeLayer()
//设置区域
shapeLayer.frame = self.bounds
//边框颜色
shapeLayer.strokeColor = UIColor.orange.cgColor
shapeLayer.fillColor = nil
//边框宽度
shapeLayer.lineWidth = 1
shapeLayer.path = aPath.cgPath
self.layer.addSublayer(shapeLayer)
//创建动画
let drawLineAni = self.baseAnimationWithKeyPath("strokeEnd", fromValue: 0, toValue: 1, duration: 0.3, repeatCount: 0, timingFunction: nil)
//翻转
drawLineAni.autoreverses = false
shapeLayer.add(drawLineAni, forKey: "drawLineAnimation")
self.show()
}
func baseAnimationWithKeyPath(_ path : String , fromValue : Any? , toValue : Any?, duration : CFTimeInterval, repeatCount : Float? , timingFunction : String?) -> CABasicAnimation{
let animate = CABasicAnimation(keyPath: path)
//起始值
animate.fromValue = fromValue;
//变成什么,或者说到哪个值
animate.toValue = toValue
//所改变属性的起始改变量 比如旋转360°,如果该值设置成为0.5 那么动画就从180°开始
// animate.byValue =
//动画结束是否停留在动画结束的位置
// animate.isRemovedOnCompletion = false
//动画时长
animate.duration = duration
//重复次数 Float.infinity 一直重复 OC:HUGE_VALF
animate.repeatCount = repeatCount ?? 0
//设置动画在该时间内重复
// animate.repeatDuration = 5
//延时动画开始时间,使用CACurrentMediaTime() + 秒(s)
// animate.beginTime = CACurrentMediaTime() + 2;
//设置动画的速度变化
/*
kCAMediaTimingFunctionLinear: String 匀速
kCAMediaTimingFunctionEaseIn: String 先慢后快
kCAMediaTimingFunctionEaseOut: String 先快后慢
kCAMediaTimingFunctionEaseInEaseOut: String 两头慢,中间快
kCAMediaTimingFunctionDefault: String 默认效果和上面一个效果极为类似,不易区分
*/
animate.timingFunction = CAMediaTimingFunction(name: timingFunction ?? kCAMediaTimingFunctionEaseInEaseOut)
//动画在开始和结束的时候的动作
/*
kCAFillModeForwards 保持在最后一帧,如果想保持在最后一帧,那么isRemovedOnCompletion应该设置为false
kCAFillModeBackwards 将会立即执行第一帧,无论是否设置了beginTime属性
kCAFillModeBoth 该值是上面两者的组合状态
kCAFillModeRemoved 默认状态,会恢复原状
*/
animate.fillMode = kCAFillModeBoth
//动画结束时,是否执行逆向动画
// animate.autoreverses = true
return animate
}
public func show(){
//let appDelegate = (UIApplication.shared.delegate) as! AppDelegate
//appDelegate.rootViewController1.view.addSubview(WDDropDownMenu.single);
self.getCurrentVC().view.addSubview(WDDropDownMenu.single)
}
public func dismiss(){
WDDropDownMenu.single.removeFromSuperview()
}
//MARK: ---取得当前显示的视图控制器---
func getCurrentVC() -> UIViewController {
var result:UIViewController?
var window = UIApplication.shared.keyWindow
if window?.windowLevel != UIWindowLevelNormal{
let windows = UIApplication.shared.windows
for tmpWin in windows{
if tmpWin.windowLevel == UIWindowLevelNormal{
window = tmpWin
break
}
}
}
let fromView = window?.subviews[0]
if let nextRespnder = fromView?.next{
if nextRespnder.isKind(of: UIViewController.self){
result = nextRespnder as? UIViewController
}else{
result = window?.rootViewController
}
}
return result!
}
}