JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
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
//
//  Copyright © 2016 dahua. All rights reserved.
//
//  常量和全局函数
 
import UIKit
import AFNetworking
 
public typealias dh_closure = () -> ()
public typealias dh_boolClosure = (Bool) -> ()
public typealias dh_stringClosure = (String) -> ()
public typealias dh_intClosure = (Int) -> ()
 
public let dh_animDuratuion: TimeInterval = 0.4
 
public let dh_legalPassword = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#$%()*+,-./<=>?@[\\]^_`{|}~"
 
/// 除【 ’】【‘’】【;】【:】【&】【空格】 "[^'\\\"\\;\\:\\& ]"
public let dh_legalPasswordRegEx = "^[A-Za-z0-9!#%,<=>@_~`\\-\\.\\/\\(\\)\\*\\+\\?\\$\\[\\]\\\\\\^\\{\\}\\|]$"
//对应的plist文件中为:^[A-Za-z0-9!#%,<=>@_~`\-\.\/\(\)\*\+\?\$\[\]\\^\{\}\|]$
 
public let dh_legalValidCode = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
 
/// 主窗口
public var dh_keyWindow: UIWindow? {
    return UIApplication.shared.keyWindow
}
 
 
 
/// 5s及以下机型判断
public var dh_isPhone5s: Bool {
    return dh_screenWidth <= dh_screenWidth_5s
}
 
/// 5s的屏幕宽度
public var dh_screenWidth_5s: CGFloat {
    return 320
}
 
/// 屏幕宽度
public var dh_screenWidth: CGFloat {
    return min(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
}
 
/// 屏幕高度
public var dh_screenHeight: CGFloat {
    return max(UIScreen.main.bounds.size.width, UIScreen.main.bounds.size.height)
}
 
// 状态栏高度
public var dh_statusBarHeight: CGFloat {
    return dh_isiPhoneX ? 44 : 20
}
 
// 导航栏高度
public let dh_navBarHeight: CGFloat = 44
 
// 导航栏加状态栏高度
public var dh_navViewHeight: CGFloat {
    return UIApplication.shared.statusBarFrame.height + dh_navBarHeight
}
 
// Tabbar高度
public var dh_tabBarHeight: CGFloat {
    return dh_isiPhoneX ? (49 + 34) : 49
}
 
// 底部安全间隔
public var dh_bottomSafeMargin: CGFloat {
    return dh_isiPhoneX ? 34 : 0
}
 
// 左右安全间隔
public var dh_landscapeSafeMargin: CGFloat {
    return dh_isiPhoneX ? 44 : 0
}
 
// 视频左右安全间隔
public var dh_landscapeVideoSafeMargin: CGFloat {
    return dh_isiPhoneX ? 64 : 0
}
 
// 底部安全间隔
public var dh_landscapeBottomSafeMargin: CGFloat {
    return dh_isiPhoneX ? 21 : 0
}
 
// 顶底部安全间隔
public var dh_topSafeMargin: CGFloat {
    return dh_isiPhoneX ? 24 : 0
}
 
/// 屏幕方向
public var dh_screenOrientation: UIInterfaceOrientation {
    return UIApplication.shared.statusBarOrientation
}
 
/// 是否竖屏
public var dh_isAppProtrait: Bool {
    let orientation = UIApplication.shared.statusBarOrientation
    return UIInterfaceOrientationIsPortrait(orientation)
}
 
/// 是否横屏
public var dh_isAppLandscape: Bool {
    let orientation = UIApplication.shared.statusBarOrientation
    return UIInterfaceOrientationIsLandscape(orientation)
}
 
/// 命名空间
public var dh_nameSpace: String? {
    return Bundle.main.infoDictionary!["CFBundleExecutable"] as? String
}
 
/// 命名空间
public var dh_appName: String? {
     return Bundle.main.infoDictionary!["CFBundleDisplayName"] as? String
}
 
// 网络状态
public var dh_networkStatus: AFNetworkReachabilityStatus {
    return DHNetWorkHelper.sharedInstance().emNetworkStatus
}
 
// iPhone5适配比例
public var heightScare: CGFloat {
    return dh_isPhone5s ? 0.75 : 1
}
 
/// 自定义带索引的for_in,⚠️中途无法通过return退出
public func  dh_for<T>(_ items: [T]?, closure: (T, Int) -> Void) {
    
    if items == nil {
        return
    }
    
    var index = 0
    
    for item in items! {
        closure(item, index)
        index += 1
    }  
}
 
/// 延迟函数
public func  dh_delay(_ seconds: Double, closure: @escaping dh_closure) {
    
    DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: closure)
}
 
/// 同步锁🔐
public func  dh_synchronizd(lock: AnyObject, closure:() -> ()) {
    objc_sync_enter(lock)
    closure()
    objc_sync_exit(lock)
}
 
/// 根据iPhone6缩放尺寸
public func  dh_scaleSize(_ size: CGFloat) -> CGFloat {
    return dh_screenHeight / 667 * size
}
 
/// 根据iPhone6缩放尺寸,取最小值
public func  dh_scaleMinSize(_ size: CGFloat) -> CGFloat {
     return min(size, dh_scaleSize(size))
}
 
/// 根据iPhone6缩放尺寸,取最大值
public func  dh_scaleMaxSize(_ size: CGFloat) -> CGFloat {
    return max(size, dh_scaleSize(size))
}
 
/// 设置状态栏背景颜色
public func  dh_setStatusBarBackgroundColor(_ color: UIColor) {
    
    if let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIView {
        // 获取statusBarWindow
        if let statusBar = statusBarWindow.value(forKey: "statusBar") as? UIView {
            // 获取statusBar
            statusBar.backgroundColor = color
        }
    }
}
 
 
/// Inline functions
public func  dh_printDeinit(_ className: Any) {
    debugPrint("🍻🍻🍻", "Deinit Success:", className)
}