-
Notifications
You must be signed in to change notification settings - Fork 313
/
UIView.swift
250 lines (207 loc) · 8.42 KB
/
UIView.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
import UIKit
extension UIView {
class func defaultAnimation(_ duration: TimeInterval, delay: TimeInterval = 0, animations: @escaping () -> Void, completion: ((_ completed: Bool) -> Void)?) {
UIView.animate(withDuration: duration, delay: delay, options: .curveEaseInOut, animations: animations, completion: completion)
}
class func defaultSpringAnimation(_ duration: TimeInterval, delay: TimeInterval = 0, animations: @escaping () -> Void, completion: ((_ completed: Bool) -> Void)?) {
UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.6, options: [.beginFromCurrentState], animations: animations, completion: completion)
}
func roundCorners(_ corners: UIRectCorner = [.allCorners], radius: CGFloat = 5.0) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.path = path.cgPath
layer.mask = maskLayer
}
func applyDefaultCornerRadiusShadow(cornerRadius: CGFloat? = 4, shadowOpacity: CGFloat? = 0.1) {
layer.cornerRadius = cornerRadius!
layer.shadowOffset = CGSize(width: 0, height: 0)
layer.shadowRadius = 4
layer.shadowOpacity = Float(shadowOpacity!)
}
func applyDefaultShadow(shadowOpacity: CGFloat? = 0.1) {
layer.shadowOffset = CGSize(width: 0, height: 0)
layer.shadowRadius = 4
layer.shadowOpacity = Float(shadowOpacity!)
}
func applyGradient(colors: [UIColor], locations: [NSNumber]? = nil) {
let gradient: CAGradientLayer = CAGradientLayer()
gradient.frame = self.bounds
gradient.colors = colors.map { $0.cgColor }
gradient.locations = locations
if let sublayers = layer.sublayers, !sublayers.isEmpty, let sublayer = sublayers.first {
layer.replaceSublayer(sublayer, with: gradient)
} else {
layer.addSublayer(gradient)
}
}
func startRippleAnimation() {
layer.masksToBounds = true
let rippleLayer = RippleLayer()
rippleLayer.rippleRadius = bounds.midX
rippleLayer.position = CGPoint(x: bounds.midX, y: bounds.midY)
layer.addSublayer(rippleLayer)
rippleLayer.startAnimation()
}
class func fromNib<ViewType : UIView>() -> ViewType? {
let nibName = String(describing: ViewType.self)
return Bundle.main.loadNibNamed(nibName, owner: nil, options: nil)?[0] as? ViewType
}
func constraints(affecting view: UIView?) -> [NSLayoutConstraint]? {
guard let view = view else { return nil }
return constraints.filter { constraint in
if let first = constraint.firstItem as? UIView, first == view {
return true
}
if let second = constraint.secondItem as? UIView, second == view {
return true
}
return false
}
}
func pinInSuperview(respectingMargins margins: Bool = false) {
guard let superview = superview else { return }
let guide: Anchorable = (margins) ? superview.layoutMarginsGuide : superview
let constraints = [
topAnchor.constraint(equalTo: guide.topAnchor),
leftAnchor.constraint(equalTo: guide.leftAnchor),
bottomAnchor.constraint(equalTo: guide.bottomAnchor),
rightAnchor.constraint(equalTo: guide.rightAnchor)
]
NSLayoutConstraint.activate(constraints)
}
class func forAutoLayout<ViewType: UIView>(frame: CGRect = .zero, hidden: Bool = false) -> ViewType {
let view = ViewType.init(frame: frame)
view.isHidden = hidden
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
var safeArea: UIEdgeInsets {
guard #available(iOS 11.0, *) else { return .zero }
return safeAreaInsets
}
var safeTopAnchor: NSLayoutYAxisAnchor {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide.topAnchor
}
return topAnchor
}
var safeLeftAnchor: NSLayoutXAxisAnchor {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide.leftAnchor
}
return leftAnchor
}
var safeLeadingAnchor: NSLayoutXAxisAnchor {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide.leadingAnchor
}
return leadingAnchor
}
var safeBottomAnchor: NSLayoutYAxisAnchor {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide.bottomAnchor
}
return bottomAnchor
}
var safeRightAnchor: NSLayoutXAxisAnchor {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide.rightAnchor
}
return rightAnchor
}
var safeTrailingAnchor: NSLayoutXAxisAnchor {
if #available(iOS 11.0, *) {
return safeAreaLayoutGuide.trailingAnchor
}
return trailingAnchor
}
var imageRepresentation: UIImage? {
let size = CGSize(width: frame.size.width, height: frame.size.height)
UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
guard let currentContext = UIGraphicsGetCurrentContext() else { return nil }
layer.render(in:currentContext)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
class RippleLayer: CAReplicatorLayer {
var animationGroup: CAAnimationGroup? {
didSet {
animationGroup?.delegate = self
}
}
var rippleRadius: CGFloat = 100
var rippleColor: UIColor = .red
var rippleRepeatCount: Float = .greatestFiniteMagnitude
var rippleWidth: CGFloat = 10
fileprivate var rippleEffect: CALayer?
override init() {
super.init()
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
func commonInit() {
setupRippleEffect()
repeatCount = Float(rippleRepeatCount)
}
override func layoutSublayers() {
super.layoutSublayers()
rippleEffect?.bounds = CGRect(x: 0, y: 0, width: rippleRadius*2, height: rippleRadius*2)
rippleEffect?.cornerRadius = rippleRadius
instanceCount = 3
instanceDelay = 0.4
}
func setupRippleEffect() {
rippleEffect = CALayer()
rippleEffect?.borderWidth = CGFloat(rippleWidth)
rippleEffect?.borderColor = rippleColor.cgColor
rippleEffect?.opacity = 0
addSublayer(rippleEffect!)
}
func startAnimation() {
animationGroup = rippleAnimationGroup()
rippleEffect?.add(animationGroup!, forKey: "ripple")
}
func stopAnimation() {
rippleEffect?.removeAnimation(forKey: "ripple")
}
func rippleAnimationGroup() -> CAAnimationGroup {
let duration: CFTimeInterval = 3
let group = CAAnimationGroup()
group.duration = duration
group.repeatCount = self.repeatCount
group.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionDefault)
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
scaleAnimation.fromValue = 0.0
scaleAnimation.toValue = 1.0
scaleAnimation.duration = duration
let opacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
opacityAnimation.duration = duration
let fromAlpha = 1.0
opacityAnimation.values = [fromAlpha, (fromAlpha * 0.5), 0]
opacityAnimation.keyTimes = [0, 0.2, 1]
group.animations = [scaleAnimation, opacityAnimation]
return group
}
}
extension RippleLayer: CAAnimationDelegate {
func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
if let count = rippleEffect?.animationKeys()?.count, count > 0 {
rippleEffect?.removeAllAnimations()
}
}
}
protocol Anchorable {
var topAnchor: NSLayoutYAxisAnchor { get }
var bottomAnchor: NSLayoutYAxisAnchor { get }
var leftAnchor: NSLayoutXAxisAnchor { get }
var rightAnchor: NSLayoutXAxisAnchor { get }
var leadingAnchor: NSLayoutXAxisAnchor { get }
var trailingAnchor: NSLayoutXAxisAnchor { get }
}
extension UIView: Anchorable {}
extension UILayoutGuide: Anchorable {}