Skip to content

Commit 4168165

Browse files
committed
Screen增加缩放模式处理, 优化缩放模式判断准确性, 设置条件增加缩放模式值.
1 parent 3780c0a commit 4168165

File tree

5 files changed

+63
-18
lines changed

5 files changed

+63
-18
lines changed

AutoInch.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AutoInch"
4-
s.version = "2.1.0"
4+
s.version = "2.2.0"
55
s.summary = "优雅的iPhone 等比例/精准 适配工具"
66

77
s.homepage = "https://github.com/lixiang1994/AutoInch"

AutoInch.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@
287287
"@executable_path/Frameworks",
288288
"@loader_path/Frameworks",
289289
);
290-
MARKETING_VERSION = 2.1.0;
290+
MARKETING_VERSION = 2.2.0;
291291
OTHER_CFLAGS = "-fembed-bitcode";
292292
PRODUCT_BUNDLE_IDENTIFIER = com.lee.autoinch;
293293
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";
@@ -319,7 +319,7 @@
319319
"@executable_path/Frameworks",
320320
"@loader_path/Frameworks",
321321
);
322-
MARKETING_VERSION = 2.1.0;
322+
MARKETING_VERSION = 2.2.0;
323323
OTHER_CFLAGS = "-fembed-bitcode";
324324
PRODUCT_BUNDLE_IDENTIFIER = com.lee.autoinch;
325325
PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)";

Demo/Demo/ViewController.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ class ViewController: UITableViewController {
3030
.level(.full, is: "屏幕级别 全面屏")
3131
.value
3232
)
33-
0.screen.inch(._3_5, is: 1).inch(._4_0, is: 2).value
33+
34+
35+
// 默认值 0 在3.5英寸的屏幕时返回1, 在4.0英寸的屏幕时返回2
36+
print(0.screen.inch(._3_5, is: 1).inch(._4_0, is: 2).value)
37+
// 默认值 0 在全面屏时返回1, 在6.1英寸的屏幕时返回2
3438
print(0.screen.level(.full, is: 1).inch(._6_1, is: 2).value)
39+
// 默认值 100 在宽度为375级别的屏幕时 正常返回120, 如果为缩放模式则返回110
40+
print(100.screen.width(._375, is: 120, zoomed: 110).value)
3541

3642
print("当前屏幕级别: \(Screen.Level.current)")
3743
print("是否为全面屏: \(Screen.isFull)")
44+
print("是否为缩放模式: \(Screen.isZoomedMode)")
3845
}
3946

4047
override var preferredStatusBarStyle: UIStatusBarStyle {

Demo/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- AutoInch (1.3.2)
2+
- AutoInch (2.2.0)
33

44
DEPENDENCIES:
55
- AutoInch (from `../`)
@@ -9,8 +9,8 @@ EXTERNAL SOURCES:
99
:path: "../"
1010

1111
SPEC CHECKSUMS:
12-
AutoInch: beaafbcef5f075d34b57bddd6b1893cb19d0f0de
12+
AutoInch: 1239050818579ea0cb009cfb73c153f83e83f3ce
1313

1414
PODFILE CHECKSUM: ecf7d3b2dbf7db87c4f919501ebdb320458e7aef
1515

16-
COCOAPODS: 1.9.3
16+
COCOAPODS: 1.10.1

Sources/Screen.swift

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,77 @@ extension ScreenCompatible {
4242

4343
extension ScreenWrapper {
4444

45-
public func width(_ types: Screen.Width... , is value: Base) -> Self {
45+
public func width(_ types: Screen.Width..., is value: Base) -> Self {
46+
return width(types, is: value, zoomed: value)
47+
}
48+
public func width(_ types: Screen.Width..., is value: Base, zoomed: Base) -> Self {
49+
return width(types, is: value, zoomed: zoomed)
50+
}
51+
private func width(_ types: [Screen.Width], is value: Base, zoomed: Base) -> Self {
4652
for type in types where Screen.Width.current == type {
47-
self.value = value
53+
self.value = Screen.isZoomedMode ? zoomed : value
4854
}
4955
return self
5056
}
5157

5258
public func height(_ types: Screen.Height..., is value: Base) -> Self {
59+
return height(types, is: value, zoomed: value)
60+
}
61+
public func height(_ types: Screen.Height..., is value: Base, zoomed: Base) -> Self {
62+
return height(types, is: value, zoomed: zoomed)
63+
}
64+
private func height(_ types: [Screen.Height], is value: Base, zoomed: Base) -> Self {
5365
for type in types where Screen.Height.current == type {
54-
self.value = value
66+
self.value = Screen.isZoomedMode ? zoomed : value
5567
}
5668
return self
5769
}
5870

5971
public func inch(_ types: Screen.Inch..., is value: Base) -> Self {
72+
return inch(types, is: value, zoomed: value)
73+
}
74+
public func inch(_ types: Screen.Inch..., is value: Base, zoomed: Base) -> Self {
75+
return inch(types, is: value, zoomed: zoomed)
76+
}
77+
private func inch(_ types: [Screen.Inch], is value: Base, zoomed: Base) -> Self {
6078
for type in types where Screen.Inch.current == type {
61-
self.value = value
79+
self.value = Screen.isZoomedMode ? zoomed : value
6280
}
6381
return self
6482
}
6583

6684
public func level(_ types: Screen.Level..., is value: Base) -> Self {
85+
return level(types, is: value, zoomed: value)
86+
}
87+
public func level(_ types: Screen.Level..., is value: Base, zoomed: Base) -> Self {
88+
return level(types, is: value, zoomed: zoomed)
89+
}
90+
private func level(_ types: [Screen.Level], is value: Base, zoomed: Base) -> Self {
6791
for type in types where Screen.Level.current == type {
68-
self.value = value
92+
self.value = Screen.isZoomedMode ? zoomed : value
6993
}
7094
return self
7195
}
7296
}
7397

7498
public enum Screen {
7599

76-
public static var size: CGSize = UIScreen.main.bounds.size
77-
public static var scale: CGFloat = UIScreen.main.scale
100+
public static var isZoomedMode: Bool {
101+
UIScreen.main.scale != UIScreen.main.nativeScale
102+
}
103+
104+
static var size: CGSize {
105+
UIScreen.main.bounds.size
106+
}
107+
static var nativeSize: CGSize {
108+
UIScreen.main.nativeBounds.size
109+
}
110+
static var scale: CGFloat {
111+
UIScreen.main.scale
112+
}
113+
static var nativeScale: CGFloat {
114+
UIScreen.main.nativeScale
115+
}
78116

79117
public enum Width: CGFloat {
80118
case unknown = -1
@@ -85,7 +123,7 @@ public enum Screen {
85123
case _428 = 428
86124

87125
public static var current: Width {
88-
return Width(rawValue: size.width) ?? .unknown
126+
return Width(rawValue: nativeSize.width / scale) ?? .unknown
89127
}
90128
}
91129

@@ -101,7 +139,7 @@ public enum Screen {
101139
case _926 = 926
102140

103141
public static var current: Height {
104-
return Height(rawValue: size.height) ?? .unknown
142+
return Height(rawValue: nativeSize.height / scale) ?? .unknown
105143
}
106144
}
107145

@@ -118,7 +156,7 @@ public enum Screen {
118156
case _6_7 = 6.7
119157

120158
public static var current: Inch {
121-
switch (size.width, size.height, scale) {
159+
switch (nativeSize.width / scale, nativeSize.height / scale, scale) {
122160
case (320, 480, 2):
123161
return ._3_5
124162

@@ -162,7 +200,7 @@ public enum Screen {
162200
case full
163201

164202
public static var current: Level {
165-
switch (size.width, size.height) {
203+
switch (nativeSize.width / scale, nativeSize.height / scale) {
166204
case (320, 480):
167205
return .compact
168206

0 commit comments

Comments
 (0)