-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIScaleView.swift
140 lines (112 loc) · 4.43 KB
/
UIScaleView.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
//
// UIScaleView.swift
// UIScaleView
//
// Created by Xeon on 21/11/2018.
// Copyright © 2018 Yu Tae Yeon. All rights reserved.
//
import UIKit
final class UIScaleView: UIView {
private var _minimumScaleFactor: CGFloat = CGFloat(0) {
didSet {
self.setNeedsLayout()
}
}
private var _maximumScaleFactor: CGFloat = CGFloat.greatestFiniteMagnitude {
didSet {
self.setNeedsLayout()
}
}
var minimumScaleFactor: CGFloat {
set {
guard newValue <= 1.0, newValue >= 0.0 else { return }
self._minimumScaleFactor = newValue
}
get {
return _minimumScaleFactor
}
}
var maximumScaleFactor: CGFloat {
set {
guard newValue >= 1.0 else { return }
self._maximumScaleFactor = newValue
}
get {
return _maximumScaleFactor
}
}
enum SizeFittingViewAxis {
case horizontal
case vertical
}
var axis: SizeFittingViewAxis = SizeFittingViewAxis.horizontal {
didSet {
self.setNeedsLayout()
}
}
weak var contentsView: UIView! {
didSet {
oldValue?.removeFromSuperview()
self.addSubview(contentsView)
self.setNeedsLayout()
}
}
func constraintView(targetSize: CGSize) {
let contentsSize: CGSize
switch self.axis {
case .horizontal:
contentsSize = contentsView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: CGFloat.greatestFiniteMagnitude) )
case .vertical:
contentsSize = contentsView.systemLayoutSizeFitting(CGSize(width: CGFloat.greatestFiniteMagnitude, height: targetSize.height) )
}
let targetRatio: CGFloat
switch self.axis {
case .horizontal:
targetRatio = targetSize.width/contentsSize.width
case .vertical:
targetRatio = targetSize.height/contentsSize.height
}
let constraintedRatio = min(max(self._minimumScaleFactor, targetRatio), self._maximumScaleFactor)
let constraintedSize: CGSize
switch self.axis {
case .horizontal:
constraintedSize = CGSize(width: targetSize.width / constraintedRatio, height: contentsSize.height)
case .vertical:
constraintedSize = CGSize(width: contentsSize.width, height: targetSize.height / constraintedRatio)
}
let v: CGFloat
switch self.axis {
case .horizontal:
v = targetSize.width / constraintedSize.width
case .vertical:
v = targetSize.height / constraintedSize.height
}
contentsView.bounds = CGRect(origin: CGPoint.zero, size: constraintedSize)
contentsView.center = CGPoint(x: (constraintedSize.width * v)/2, y: (constraintedSize.height * v)/2)
contentsView.transform = CGAffineTransform(scaleX: v, y: v)
}
override func layoutSubviews() {
super.layoutSubviews()
if let _ = contentsView {
self.constraintView(targetSize: self.bounds.size)
}
self.invalidateIntrinsicContentSize()
}
override func systemLayoutSizeFitting(_ targetSize: CGSize) -> CGSize {
switch self.axis {
case .horizontal:
let contentsSize = contentsView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: CGFloat.greatestFiniteMagnitude) )
let targetRatio = targetSize.width/contentsSize.width
let constraintedRatio = min(max(self._minimumScaleFactor, targetRatio), self._maximumScaleFactor)
return CGSize(width: targetSize.width, height: contentsSize.height * constraintedRatio)
case .vertical:
let contentsSize = contentsView.systemLayoutSizeFitting(CGSize(width: CGFloat.greatestFiniteMagnitude, height: targetSize.height) )
let targetRatio = targetSize.height/contentsSize.height
let constraintedRatio = min(max(self._minimumScaleFactor, targetRatio), self._maximumScaleFactor)
return CGSize(width: contentsSize.width * constraintedRatio, height: targetSize.height)
}
}
override var intrinsicContentSize: CGSize {
return self.systemLayoutSizeFitting(bounds.size)
}
}