本文有大量截图,建议感兴趣的朋友下载demo。git
ProHUD = Toast(通知横幅) + Alert(ProgressHUD、弹窗) + ActionSheet(操做表)github
在文档页面能够查看大图
发一个弹窗:swift
Alert.push(scene: .loading, title: "正在加载", message: "请稍等片刻")
发一个通知横幅:网络
Toast.push(scene: .warning, title: "设备电量太低", message: "请及时对设备进行充电,以避免影响使用。")
例如发一个弹窗:ide
Alert.push() { (alert) in alert.identifier = "error" alert.update { (vm) in vm.scene = .error vm.title = "同步失败" vm.message = "请检查网络是否链接" vm.add(action: .default, title: "重试") { // do something } vm.add(action: .cancel, title: "取消", handler: nil) } }
避免重复发送同一个实例:布局
Toast.find("aaa", last: { (t) in t.update() { (vm) in vm.title = "已经存在了" } }) { Toast.push(title: "这是一条id为aaa的横幅", message: "避免重复发布同一条信息") { (t) in t.identifier = "aaa" t.update { (vm) in vm.scene = .warning vm.duration = 0 } } }
更新loading的结果:优化
Alert.find("loading", last: { (a) in a.update { (vm) in vm.scene = .success vm.title = "同步成功" vm.message = nil } })
更新为加载失败,并增长重试按钮:spa
Alert.find("loading", last: { (a) in a.update { (vm) in vm.scene = .error vm.title = "同步失败" vm.message = "请检查网络是否链接" vm.add(action: .default, title: "重试") { // do something } vm.add(action: .cancel, title: "取消", handler: nil) } })
这个库采用配置UI和调用接口分离的设计,这种思路借鉴了和而泰公共库,我认为这是一种调用比传统UI库方便的同时可定制化能力也比传统UI库强大的设计思路。设计
简单来讲,就是你在AppDelegate中告诉ProHUD,你要的横幅、弹窗、操做表分别是什么样的,若是参数是什么什么,就怎么展现UI。
而后调用的地方就不须要设置UI了,只须要专一于数据,如:3d
Alert.push(scene: .loading, title: "正在加载", message: "请稍等片刻")
这样就发出了一个弹窗,而弹窗的样式,则在AppDelegate中以及预先配置好了。我使用了scene这个灵活的参数,你能够本身扩展场景,例如:
extension ProHUD.Scene { static var confirm: ProHUD.Scene { var scene = ProHUD.Scene(identifier: "confirm") scene.image = UIImage(named: "ProHUDMessage") return scene } static var delete: ProHUD.Scene { var scene = ProHUD.Scene(identifier: "delete") scene.image = UIImage(named: "ProHUDTrash") scene.title = "确认删除" scene.message = "此操做不可撤销" return scene } static var buy: ProHUD.Scene { var scene = ProHUD.Scene(identifier: "buy") scene.image = UIImage(named: "ProHUDBuy") scene.title = "确认付款" scene.message = "一旦购买拒不退款" return scene } }
一个scene就能够理解成一套模板。
不少库没有多实例管理,很容易出现简单粗暴的视图重叠现象,ProHUD针对不一样场景作了不一样的优化,对于横幅来讲,能够平铺显示,像系统的通知中心同样,你能够拖拽向上移除。对于弹窗来讲,我给底层的弹窗作了景深效果处理,使得看起来不像是BUG。