-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRollerView.swift
105 lines (85 loc) · 3.28 KB
/
RollerView.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
//
// RollerView.swift
// test2
//
// Created by 铭 on 2018/11/1.
// Copyright © 2018 铭 clmd. All rights reserved.
// 卷帘效果
import UIKit
class RollerView: UIView {
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var contentHeight: NSLayoutConstraint!
private var subViewHeight: CGFloat = 0.0 // 子视图的高度
var isShow = false // 是否显示状态
class func sharedInstance(y: CGFloat = 0) -> RollerView {
let nibView = Bundle.main.loadNibNamed("RollerView", owner: self, options: nil)!.first as! RollerView
nibView.backgroundColor = UIColor.black.withAlphaComponent(0)
nibView.contentHeight.constant = 0
nibView.frame = CGRect(x: 0, y: y, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - y)
nibView.layoutIfNeeded()
UIApplication.shared.keyWindow?.addSubview(nibView)
return nibView
}
// subView:需要显示的view
func show(subView: UIView) {
isShow = true
subViewHeight = subView.bounds.height
contentHeight.constant = subViewHeight // contentHeight.constant = 0 时添加会出现UI位置异常
self.layoutIfNeeded() // 视图预载
contentView.addSubview(subView)
contentHeight.constant = 0
self.layoutIfNeeded()
UIView.animate(withDuration: 0.3) {
self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
self.contentHeight.constant = self.subViewHeight
self.layoutIfNeeded()
}
}
// 同一时间只能调用一次 调了多次容易出现未知的UI行为
func hidden() {
UIView.animate(withDuration: 0.3, animations: {
self.backgroundColor = UIColor.black.withAlphaComponent(0)
self.contentHeight.constant = 0
self.layoutIfNeeded()
}) { (finish) in
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
self.removeFromSuperview()
}
}
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let view = super.hitTest(point, with: event)
if view == self || !getSubViews(view: self).contains(view ?? UIView()) {
if isShow {
hidden()
isShow = !isShow
}
}
return view
}
func getSubViews(view: UIView) -> [UIView] {
if view.subviews.count == 0 {
return [view]
} else {
var subView = [view]
for item in view.subviews {
subView.append(contentsOf: getSubViews(view: item))
}
return subView
}
}
class func hiddenRoller(from view: UIView) {
if let roller = RollerView.getSuperRollerView(view: view) as? RollerView {
roller.hidden()
}
}
class func getSuperRollerView(view: UIView) -> UIView? {
if view.isKind(of: RollerView.self) {
return view
}else if view.superview == nil {
return nil
} else {
return getSuperRollerView(view: (view.superview ?? UIView()))
}
}
}