提示框

提示框

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

效果

有一下6种样式


image

image

image

image

image

image

使用

LYProgressHUD.showLoading()
LYProgressHUD.showInfo("")
LYProgressHUD.showError("")
LYProgressHUD.showSuccess("")
LYProgressHUD.showOnlyText("")
LYProgressHUD.showStatusBarWithText(text: "")
LYProgressHUD.showWaitingWithImages(images: Array<UIImage>())

源码

//
//  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!
}
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容