-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathAnimatableImageView.swift
More file actions
79 lines (71 loc) · 3.16 KB
/
AnimatableImageView.swift
File metadata and controls
79 lines (71 loc) · 3.16 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
import UIKit
final class AnimatableImageView: UIView {
private let imageView = UIImageView()
override var contentMode: UIViewContentMode {
didSet { update() }
}
override var frame: CGRect {
didSet { update() }
}
var image: UIImage? {
didSet {
imageView.image = image
update()
}
}
init() {
super.init(frame: .zero)
clipsToBounds = true
addSubview(imageView)
imageView.contentMode = .scaleToFill
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
private extension AnimatableImageView {
func update() {
guard let image = image else { return }
switch contentMode {
case .scaleToFill:
imageView.bounds = Utilities.rect(forSize: bounds.size)
imageView.center = Utilities.center(forSize: bounds.size)
case .scaleAspectFit:
imageView.bounds = Utilities.aspectFitRect(forSize: image.size, insideRect: bounds)
imageView.center = Utilities.center(forSize: bounds.size)
case .scaleAspectFill:
imageView.bounds = Utilities.aspectFillRect(forSize: image.size, insideRect: bounds)
imageView.center = Utilities.center(forSize: bounds.size)
case .redraw:
imageView.bounds = Utilities.aspectFillRect(forSize: image.size, insideRect: bounds)
imageView.center = Utilities.center(forSize: bounds.size)
case .center:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.center(forSize: bounds.size)
case .top:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.centerTop(forSize: image.size, insideSize: bounds.size)
case .bottom:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.centerBottom(forSize: image.size, insideSize: bounds.size)
case .left:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.centerLeft(forSize: image.size, insideSize: bounds.size)
case .right:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.centerRight(forSize: image.size, insideSize: bounds.size)
case .topLeft:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.topLeft(forSize: image.size, insideSize: bounds.size)
case .topRight:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.topRight(forSize: image.size, insideSize: bounds.size)
case .bottomLeft:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.bottomLeft(forSize: image.size, insideSize: bounds.size)
case .bottomRight:
imageView.bounds = Utilities.rect(forSize: image.size)
imageView.center = Utilities.bottomRight(forSize: image.size, insideSize: bounds.size)
}
}
}