提示框

"迈入Swift的学习中"

Posted by 李勇 on 2017-07-23

自己写了一个hud,里面图片是通过代码画出来的,不需要其他资源文件

效果

有一下6种样式
image
image
image
image
image
image

使用

1
2
3
4
5
6
7
LYProgressHUD.showLoading()
LYProgressHUD.showInfo("")
LYProgressHUD.showError("")
LYProgressHUD.showSuccess("")
LYProgressHUD.showOnlyText("")
LYProgressHUD.showStatusBarWithText(text: "")
LYProgressHUD.showWaitingWithImages(images: Array<UIImage>())

源码

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
//
// LYProgressHUD.swift
// LYProgressHUD
// _
// |.| /\ /\
// |.| \ \_/ /
// |.| \_~_/
// |.| /.\
// |.|__/\ [.]
// |_|__,/ \_/
//
// Created by ly on 16/12/16.
// Copyright © 2016年 aoke. All rights reserved.
//
import UIKit
public enum ShowType {
case success
case error
case info
}
public extension LYProgressHUD {
/**
加载动画图片
- parameter images: 图片数组
- parameter timeInterval: 动画每次执行时间
*/
public static func showWaitingWithImages(images : Array<UIImage>, timeInterval : TimeInterval = 0, autoRemove: Bool = true) {
LYProgressHUD.showWaitWithImages(images: images, timeInterval: timeInterval, autoRemove: autoRemove)
}
/**
显示菊花
- parameter text: 需要显示的文字,如果不设置文字,则只显示菊花
- parameter autoRemove: 是否自动移除,默认3秒后自动移除
*/
public static func showLoading(_ text: String = "", autoRemove: Bool = true) {
LYProgressHUD.showWaitingWithText(text: text, autoRemove: autoRemove)
}
/**
状态栏显示
- parameter text: 需要显示的文字
- parameter color: 背景颜色
- parameter autoRemove: 是否自动移除,默认3秒后自动移除
*/
public static func showStatusBarWithText(text: String = "OK", color: UIColor = UIColor(red: 131 / 255.0, green: 178 / 255.0, blue: 158 / 255.0, alpha: 1), autoRemove: Bool = true) {
LYProgressHUD.showStatusBar(text: text, color: color, autoRemove: autoRemove)
}
/**
只显示文字
- parameter text: 需要显示的文字
- parameter autoRemove: 是否自动移除,默认3秒后自动移除
*/
public static func showOnlyText(_ text: String = "", autoRemove: Bool = true) {
LYProgressHUD.onlyText(text: text, autoRemove: autoRemove)
}
/**
Success样式
- parameter successText: 需要显示的文字,默认为 Success!
- parameter autoRemove: 是否自动移除,默认3秒后自动移除
*/
public static func showSuccess(_ success: String = "Success!", autoRemove: Bool = true) {
LYProgressHUD.showText(type: .success, text: success, autoRemove: autoRemove)
}
/**
Error样式
- parameter error: 需要显示的文字,默认为 Error!
- parameter autoRemove: 是否自动移除,默认3秒后自动移除
*/
public static func showError(_ error: String = "Error!", autoRemove: Bool = true) {
LYProgressHUD.showText(type: .error, text: error, autoRemove: autoRemove)
}
/**
Info样式
- parameter infoText: 需要显示的文字,默认为 Info!
- parameter autoRemove: 是否自动移除,默认3秒后自动移除
*/
public static func showInfo(_ infoText: String = "info!", autoRemove: Bool = true) {
LYProgressHUD.showText(type: .info, text: infoText, autoRemove: autoRemove)
}
/**
移除HUD,会移除所有
*/
public static func dismiss() {
LYProgressHUD.clear()
}
}
//提示框大小
private let hudW : CGFloat = 150
private let hudH : CGFloat = 150
//提示图片大小
private let imgW : CGFloat = 30
private let imgH : CGFloat = 30
//提示图标大小
private let circleSize = CGSize(width: imgW, height: imgH)
private let windowBgColor = UIColor.black.withAlphaComponent(0.5)
private let bgColor : UIColor = UIColor(red: 250 / 255.0, green: 250 / 255.0, blue: 250 / 255.0, alpha:1)
private let textColor = UIColor(red: 33 / 255.0, green: 33 / 255.0, blue: 33 / 255.0, alpha: 1)
public class LYProgressHUD : NSObject {
static var windows = Array<UIWindow!>()
static var angle: Double {
return [0, 0, 180, 270, 90][UIApplication.shared.statusBarOrientation.hashValue] as Double
}
static public func showWaitWithImages(images : Array<UIImage>, timeInterval : TimeInterval, autoRemove: Bool = true) {
self.dismiss()
let frame = CGRect(x: 0, y: 0, width: hudW, height: hudH)
let imageView = UIImageView()
imageView.frame = frame
imageView.contentMode = .scaleAspectFit
imageView.backgroundColor = bgColor
imageView.layer.cornerRadius = 10
imageView.animationImages = images
imageView.animationDuration = timeInterval == 0 ? TimeInterval(images.count) * 0.07 : timeInterval
imageView.animationRepeatCount = 0
imageView.startAnimating()
let window = LYProgressHUD.createWindow(frame: frame, view: imageView)
if autoRemove {
perform(#selector(LYProgressHUD.removeHUD(object:)), with: window, afterDelay: 2)
}
}
static public func showWaitingWithText(text: String, autoRemove: Bool) {
self.dismiss()
let frame = CGRect(x: 0, y: 0, width: hudW, height: hudH)
let view = UIView(frame: frame)
view.backgroundColor = bgColor
view.layer.cornerRadius = 10
let activity = UIActivityIndicatorView(activityIndicatorStyle: .gray)
if text.isEmpty {
activity.frame = CGRect(x: (hudW-60)/2.0, y: (hudH-60)/2.0, width: 60, height: 60)
activity.transform = CGAffineTransform(scaleX: 2.5, y: 2.5)
}else{
activity.frame = CGRect(x: (hudW-40)/2.0, y: 20, width: 40, height: 40)
activity.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
let lable = LYProgressHUD.createLable()
lable.frame = CGRect(x: 5, y: 20+40+10, width: hudW-10, height: hudH-20-40-20)
lable.text = text
view.addSubview(lable)
}
activity.color = textColor
activity.startAnimating()
view.addSubview(activity)
let window = LYProgressHUD.createWindow(frame: frame, view: view)
if autoRemove {
perform(#selector(LYProgressHUD.removeHUD(object:)), with: window, afterDelay: 10)
}
}
static public func showStatusBar(text: String, color: UIColor, autoRemove: Bool) {
self.dismiss()
var frame = UIApplication.shared.statusBarFrame
frame.size.height = 64
let lable = LYProgressHUD.createLable()
lable.text = text
let window = UIWindow(frame:frame)
window.transform = CGAffineTransform(rotationAngle: CGFloat(angle * Double.pi / 180))
window.isHidden = false
window.addSubview(lable)
windows.append(window)
window.backgroundColor = color
window.windowLevel = UIWindowLevelStatusBar
lable.frame = frame
window.center = CGPoint(x: frame.width * 0.5, y: frame.height * 0.5)
lable.center = window.center
if autoRemove {
perform(#selector(LYProgressHUD.removeHUD(object:)), with: window, afterDelay: 2.0)
}
}
static public func onlyText(text: String, autoRemove: Bool) {
self.dismiss()
let view = UIView()
view.backgroundColor = bgColor
view.layer.cornerRadius = 10
let label = LYProgressHUD.createLable()
label.text = text
label.font = UIFont.systemFont(ofSize: 13)
label.frame = CGRect(x: 5, y: 5, width: 200, height: 100)
view.addSubview(label)
let frame = CGRect(x: 0, y: 0, width: 210, height: 110)
view.frame = frame
let window = LYProgressHUD.createWindow(frame: frame, view: view)
if autoRemove {
perform(#selector(LYProgressHUD.removeHUD(object:)), with: window, afterDelay: 3)
}
}
static public func showText(type: ShowType, text: String, autoRemove: Bool) {
self.dismiss()
let frame = CGRect(x: 0, y: 0, width: hudW, height: hudH)
let view = UIView(frame: frame)
view.layer.cornerRadius = 10
view.backgroundColor = bgColor
var image = UIImage()
switch type {
case .success:
image = drawImage.imageOfSuccess
case .error:
image = drawImage.imageOfError
case .info:
image = drawImage.imageOfInfo
}
let imageView = UIImageView(image: image)
imageView.frame = CGRect(origin: CGPoint(x: (hudW-imgW)/2.0, y: 20), size: circleSize)
imageView.contentMode = .center
view.addSubview(imageView)
let lable = LYProgressHUD.createLable()
lable.frame = CGRect(x: 5, y: 20+imgH+10, width: hudW-10, height: hudH-20-imgH-20)
lable.text = text
view.addSubview(lable)
let window = LYProgressHUD.createWindow(frame: frame, view: view)
if autoRemove {
perform(#selector(LYProgressHUD.removeHUD(object:)), with: window, afterDelay: 1.0)
}
}
static private func createLable() -> UILabel {
let lable = UILabel()
lable.font = UIFont.systemFont(ofSize: 13)
lable.backgroundColor = UIColor.clear
lable.textColor = textColor
lable.numberOfLines = 0
lable.textAlignment = .center
return lable
}
static private func createWindow(frame: CGRect, view: UIView) -> UIWindow {
let window = UIWindow(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
window.backgroundColor = windowBgColor
window.windowLevel = UIWindowLevelAlert
window.transform = CGAffineTransform(rotationAngle: CGFloat(angle * Double.pi / 180))
window.isHidden = false
window.center = getCenter()
view.center = window.center
window.addSubview(view)
windows.append(window)
return window
}
@objc static public func removeHUD(object: AnyObject) {
if let window = object as? UIWindow {
if let index = windows.index(where: { (item) -> Bool in
return item == window
}) {
windows.remove(at: index)
}
}
}
static public func clear() {
if windows.isEmpty { return }
self.cancelPreviousPerformRequests(withTarget: self)
windows.removeAll(keepingCapacity: false)
}
static func getCenter() -> CGPoint {
let view = UIApplication.shared.keyWindow?.subviews.first as UIView!
if UIApplication.shared.statusBarOrientation.hashValue >= 3 {
return CGPoint(x: view!.center.y, y: view!.center.x)
} else {
return view!.center
}
}
}
class drawImage {
struct imageCache {
static var imageOfSuccess: UIImage?
static var imageOfError: UIImage?
static var imageOfInfo: UIImage?
}
class func draw(type : ShowType) {
let path = UIBezierPath()
path.move(to: CGPoint(x: imgW, y: imgH/2.0))
path.addArc(withCenter: CGPoint(x: imgW/2.0, y: imgH/2.0), radius: imgW/2.0-1, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: true)
// path.move(to: CGPoint(x: imgW/2.0, y: imgH/2.0))
path.close()
switch type {
case .success:
path.move(to: CGPoint(x: imgW/2.0-5, y: imgH/2.0))
path.addLine(to: CGPoint(x: imgW/2.0, y: imgH/2.0+5))
path.addLine(to: CGPoint(x: imgW/2.0+7, y: imgH/2.0-7))
path.move(to: CGPoint(x: imgW/2.0-5, y: imgH/2.0))
path.close()
case .error:
path.move(to: CGPoint(x: imgW/2.0-7, y: imgH/2.0-7))
path.addLine(to: CGPoint(x: imgW/2.0+7, y: imgH/2.0+7))
path.move(to: CGPoint(x: imgW/2.0+7, y: imgH/2.0-7))
path.addLine(to: CGPoint(x: imgW/2.0-7, y: imgH/2.0+7))
path.move(to: CGPoint(x: imgW/2.0-7, y: imgH/2.0-7))
path.close()
case .info:
path.move(to: CGPoint(x: imgW/2.0, y: imgH/2.0-7))
path.addLine(to: CGPoint(x: imgW/2.0, y: imgH/2.0+4))
path.move(to: CGPoint(x: imgW/2.0, y: imgH/2.0+6))
path.addLine(to: CGPoint(x: imgW/2.0, y: imgH/2.0+7))
path.move(to: CGPoint(x: imgW/2.0, y: imgH/2.0-7))
path.close()
let tmpPath = UIBezierPath()
tmpPath.move(to: CGPoint(x: 20, y: 30))
tmpPath.addArc(withCenter: CGPoint(x: 20, y: 30), radius: 1, startAngle: 0, endAngle: CGFloat(Double.pi*2), clockwise: true)
tmpPath.close()
UIColor.white.setFill()
tmpPath.fill()
}
UIColor.black.setStroke()
path.stroke()
}
class var imageOfSuccess: UIImage {
if imageCache.imageOfSuccess != nil {
return imageCache.imageOfSuccess!
}
UIGraphicsBeginImageContextWithOptions(circleSize, false, 0)
drawImage.draw(type: .success)
imageCache.imageOfSuccess = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageCache.imageOfSuccess!
}
class var imageOfError: UIImage {
if imageCache.imageOfError != nil {
return imageCache.imageOfError!
}
UIGraphicsBeginImageContextWithOptions(circleSize, false, 0)
drawImage.draw(type: .error)
imageCache.imageOfError = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageCache.imageOfError!
}
class var imageOfInfo: UIImage {
if imageCache.imageOfInfo != nil {
return imageCache.imageOfInfo!
}
UIGraphicsBeginImageContextWithOptions(circleSize, false, 0)
drawImage.draw(type: .info)
imageCache.imageOfInfo = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return imageCache.imageOfInfo!
}
}