forked from Zero-zhou/iOS-Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+Ex.swift
457 lines (357 loc) · 12.7 KB
/
UIView+Ex.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
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
//
// UIView+Ex.swift
//
// Created by Augus on 9/4/15.
// Copyright © 2015 iAugus. All rights reserved.
//
import UIKit
extension UIView {
var parentViewController: UIViewController? {
var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
if let viewController = parentResponder as? UIViewController {
return viewController
}
}
return nil
}
}
extension UIView {
var _maxSize: CGSize {
let maxL = max(frame.width, frame.height)
return CGSize(width: maxL, height: maxL)
}
var _minSize: CGSize {
let minL = min(frame.width, frame.height)
return CGSize(width: minL, height: minL)
}
var _maxBounds: CGRect {
return CGRect(origin: .zero, size: _maxSize)
}
var _minBounds: CGRect {
return CGRect(origin: .zero, size: _minSize)
}
}
extension UIView {
func setFrameSize(_ size: CGSize) {
var frame = self.frame
frame.size = size
self.frame = frame
}
func setFrameHeight(_ height: CGFloat) {
var frame = self.frame
frame.size.height = height
self.frame = frame
}
func setFrameWidth(_ width: CGFloat) {
var frame = self.frame
frame.size.width = width
self.frame = frame
}
func setFrameOriginX(_ originX: CGFloat) {
var frame = self.frame
frame.origin.x = originX
self.frame = frame
}
func setFrameOriginY(_ originY: CGFloat) {
var frame = self.frame
frame.origin.y = originY
self.frame = frame
}
func setFrameMaxX(_ maxX: CGFloat) {
setFrameOriginX(maxX - frame.width)
}
func setFrameMaxY(_ maxY: CGFloat) {
setFrameOriginY(maxY - frame.height)
}
/**
set current view's absolute center to other view's center
- parameter view: other view
*/
func centerTo(_ view: UIView) {
self.frame.origin.x = view.bounds.midX - self.frame.width / 2
self.frame.origin.y = view.bounds.midY - self.frame.height / 2
}
}
// MARK: -
extension UIView {
@discardableResult
func plusOriginX(_ delta: CGFloat) -> UIView {
var frame = self.frame
frame.origin.x += delta
self.frame = frame
return self
}
@discardableResult
func plusOriginY(_ delta: CGFloat) -> UIView {
var frame = self.frame
frame.origin.y += delta
self.frame = frame
return self
}
@discardableResult
func plusPoint(_ point: CGPoint) -> UIView {
frame.origin.x += point.x
frame.origin.y += point.y
return self
}
@discardableResult
func plusOriginXY(_ x: CGFloat, _ y: CGFloat) -> UIView {
let point = CGPoint(x: x, y: y)
plusPoint(point)
return self
}
@discardableResult
func plusWidth(_ width: CGFloat) -> UIView {
frame.size.width += width
return self
}
@discardableResult
func plusHeight(_ height: CGFloat) -> UIView {
frame.size.height += height
return self
}
}
extension UIView {
func simulateHighlight(withMinAlpha alpha: CGFloat = 0.5) {
UIView.animate(withDuration: 0.1, animations: { [weak self] in
self?.alpha = alpha
}, completion: { (_) -> Void in
UIView.animate(withDuration: 0.1, delay: 0.1, options: [], animations: { [weak self] in
self?.alpha = 1
}, completion: nil)
})
}
func morphingView(_ duration: TimeInterval = 0.3, toAlpha alpha: CGFloat) {
UIView.animate(withDuration: duration) {
self.alpha = alpha
}
}
}
extension UIView {
func point(inside point: CGPoint, from view: UIView, event: UIEvent?) -> Bool {
return self.point(inside: view.convert(point, to: self), with: event)
}
}
// MARK: -
extension UIView {
func isEqualToNameOfClass(_ name: String) -> Bool {
return String(describing: classForCoder) == name
}
/// loop subviews and subviews' subviews
///
/// - parameter closure: subview
func loopViews(_ closure: ((_ subView: UIView) -> ())) {
for v in subviews {
closure(v)
v.loopViews(closure)
}
}
/// loop subviews and subviews' subviews
///
/// - parameter nameOfView: name of subview
/// - parameter shouldReturn: should return or not when meeting the specific subview
/// - parameter execute: subview
func loopViews(_ nameOfView: String, shouldReturn: Bool = true, execute: ((_ subView: UIView) -> ())) {
for v in subviews {
if v.isEqualToNameOfClass(nameOfView) {
execute(v)
if shouldReturn {
return
}
}
v.loopViews(nameOfView, shouldReturn: shouldReturn, execute: execute)
}
}
}
// MARK: - rotation animation
extension UIView: CAAnimationDelegate {
/// Angle: 𝞹
///
/// - parameter duration: <#duration description#>
/// - parameter beginWithClockwise: <#beginWithClockwise description#>
/// - parameter clockwise: <#clockwise description#>
/// - parameter animated: <#animated description#>
func rotationAnimation(_ duration: CFTimeInterval? = 0.4, beginWithClockwise: Bool, clockwise: Bool, animated: Bool) {
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
let angle: Double = beginWithClockwise ? (clockwise ? M_PI : 0) : (clockwise ? 0 : -M_PI)
if beginWithClockwise {
if !clockwise { rotationAnimation.fromValue = M_PI }
} else {
if clockwise { rotationAnimation.fromValue = -M_PI }
}
rotationAnimation.toValue = angle
rotationAnimation.duration = animated ? duration! : 0
rotationAnimation.repeatCount = 0
rotationAnimation.delegate = self
rotationAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn)
rotationAnimation.fillMode = kCAFillModeForwards
rotationAnimation.isRemovedOnCompletion = false
layer.add(rotationAnimation, forKey: "rotationAnimation")
}
/// Angle: 𝞹/2
///
/// - parameter duration: <#duration description#>
/// - parameter clockwise: <#clockwise description#>
/// - parameter animated: <#animated description#>
func rotationAnimation(_ duration: TimeInterval, clockwise: Bool, animated: Bool) {
let angle = CGFloat(clockwise ? M_PI_2 : -M_PI_2)
if animated {
UIView.animate(withDuration: duration, delay: 0, options: .curveLinear, animations: { () -> Void in
self.transform = self.transform.rotated(by: angle)
}, completion: nil)
} else {
self.transform = self.transform.rotated(by: angle)
}
}
var transformAngle: Double {
return _angle
}
var transformAngleInDegrees: Double {
return _angle * (180 / M_PI)
}
private var _angle: Double {
return atan2(Double(transform.b), Double(transform.a))
}
}
// MARK: - Twinkle
extension UIView {
func twinkling(_ duration: TimeInterval, minAlpha: CGFloat = 0, maxAlpha: CGFloat = 1) {
UIView.animate(withDuration: duration, animations: {
self.alpha = minAlpha
}) { [weak self] (finished) in
if finished {
UIView.animate(withDuration: duration, animations: {
self?.alpha = maxAlpha
}, completion: { [weak self] (finished) in
if finished {
self?.twinkling(duration, minAlpha: minAlpha, maxAlpha: maxAlpha)
}
})
}
}
}
}
// MARK: -
extension UIView {
enum ShakeDirection {
case horizontal
case vertical
}
func startShaking(duration: TimeInterval = 0.2, offset: CGFloat = 1.5, direction: ShakeDirection = .horizontal, repeatCount: Float = FLT_MAX) {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = duration
animation.repeatCount = repeatCount
animation.autoreverses = true
switch direction {
case .horizontal:
animation.fromValue = NSValue(cgPoint: CGPoint(x: center.x - offset, y: center.y))
animation.toValue = NSValue(cgPoint:CGPoint(x: center.x + offset, y: center.y))
case .vertical:
animation.fromValue = NSValue(cgPoint: CGPoint(x: center.x, y: center.y - offset))
animation.toValue = NSValue(cgPoint:CGPoint(x: center.x, y: center.y + offset))
}
layer.add(animation, forKey: "position")
}
func stopShaking() {
layer.removeAnimation(forKey: "position")
}
}
// MARK: - Corner raidus
// REFERENCE: http://stackoverflow.com/a/35621736/4656574
extension UIView {
/**
Rounds the given set of corners to the specified radius
- parameter corners: Corners to round
- parameter radius: Radius to round to
*/
func round(_ corners: UIRectCorner, radius: CGFloat) {
_round(corners, radius: radius)
}
/**
Rounds the given set of corners to the specified radius with a border
- parameter corners: Corners to round
- parameter radius: Radius to round to
- parameter borderColor: The border color
- parameter borderWidth: The border width
*/
func round(_ corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
let mask = _round(corners, radius: radius)
addBorder(mask, borderColor: borderColor, borderWidth: borderWidth)
}
/**
Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border
- parameter diameter: The view's diameter
- parameter borderColor: The border color
- parameter borderWidth: The border width
*/
func fullyRound(_ diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
layer.masksToBounds = true
layer.cornerRadius = diameter / 2
layer.borderWidth = borderWidth
layer.borderColor = borderColor.cgColor
}
@discardableResult
fileprivate func _round(_ corners: UIRectCorner, radius: CGFloat) -> CAShapeLayer {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
return mask
}
fileprivate func addBorder(_ mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
let borderLayer = CAShapeLayer()
borderLayer.path = mask.path
borderLayer.fillColor = UIColor.clear.cgColor
borderLayer.strokeColor = borderColor.cgColor
borderLayer.lineWidth = borderWidth
borderLayer.frame = bounds
layer.addSublayer(borderLayer)
}
}
// MARK: -
extension UIView {
/// Change view's alpha animated
///
/// - parameter toAlpha: change to alpha, default is 0
/// - parameter duration: change duration, default is 0.3s
/// - parameter closure: closure, default is nil
func changeAlphaAnimated(_ toAlpha: CGFloat = 0, duration: TimeInterval = 0.3, closure: ((Bool) -> ())? = nil) {
UIView.animate(withDuration: duration, animations: { [weak self] in
self?.alpha = toAlpha
}, completion: closure)
}
}
// MARK: -
extension UIView {
func addGestureRecognizers(_ gestures: [UIGestureRecognizer]) {
gestures.forEach() {
addGestureRecognizer($0)
}
}
func addGestureRecognizers(_ gestures: UIGestureRecognizer...) {
addGestureRecognizers(gestures)
}
}
// MARK: -
extension UITableView {
var centerPoint: CGPoint {
let x = center.x + contentOffset.x
let y = center.y + contentOffset.y
return CGPoint(x: x, y: y)
}
var centerCellIndexPath: IndexPath? {
return indexPathForRow(at: centerPoint)
}
}
extension UICollectionView {
var centerPoint: CGPoint {
let x = center.x + contentOffset.x
let y = center.y + contentOffset.y
return CGPoint(x: x, y: y)
}
var centerItemIndexPath: IndexPath? {
return indexPathForItem(at: centerPoint)
}
}