目的:用少許的代碼,簡單的接口實現一個簡單卻可統籌全局的HUD信息彈窗,包含更易於擴展和自定義的樣式。git
public extension CD_HUD {
public enum Style {
case text
case loading(_ l:Loading?)
case info
case succeed
case warning
case error
case progress(_ l:Progress)
case custom(_ view:UIView)
public enum Loading {
/// 系統菊花
case activity
/// 環
case ring
/// 鑽戒
case diamond
/// 毛筆墨
case brush
/// 寫輪眼
case roundEyes
/// refresh 箭頭
case arrow
/// 自定義 Gif 動畫組
case images(_ images:[UIImage], _ duration:TimeInterval, _ repeatCount:Int)
/// 自定義 View
case view(_ view:UIView)
}
public enum Progress {
/// 默認
case `default`(model:CD_HUDProgressView.Model, handler:((CD_HUDProgressView?)->Void)?)
/// 自定義 View
case view(_ view:UIView)
}
}
}
複製代碼
public extension CD_HUD {
public enum Axis {
case vertical
case horizontal
}
public enum Position {
case top
case center
case bottom
/// offsetY > 0 以top爲參照 < 0 以bottom爲參照
case custom(_ offsetY:CGFloat)
}
public enum Animation {
case fade
case slide
case zoom
/// 彈簧動畫 只適用 HUD顯示 & Position .top .bottom .custom(_ point:CGPoint)
case spring
/// 自定義動畫
case custom(_ animat:((_ hud:CD_HUD?, _ contentView:UIView?)->Void)?)
case none
}
}
複製代碼
public extension UIView {
func hud_loading() {
self.cd.hud_remove()
self.cd.hud(.loading(.activity))
}
func hud_hidden() {
self.cd.hud_remove()
}
func hud_msg(_ title:String) {
self.cd.hud_remove()
var model = CD_HUD.modelDefault
model._position = .bottom
model._showAnimation = .slide
self.cd.hud(.text, title: title, model:model)
}
func hud_succeed(_ title:String) {
self.cd.hud_remove()
var model = CD_HUD.modelDefault
model._axis = .horizontal
model._isSquare = false
self.cd.hud(.succeed, title: title, model:model)
}
func hud_error(_ title:String) {
self.cd.hud_remove()
var model = CD_HUD.modelDefault
model._axis = .horizontal
model._isSquare = false
self.cd.hud(.error, title: title, model:model)
}
func hud_info(_ title:String) {
self.cd.hud_remove()
var model = CD_HUD.modelDefault
model._axis = .horizontal
model._isSquare = false
self.cd.hud(.info, title: title, model:model)
}
func hud_warning(_ title:String) {
self.cd.hud_remove()
var model = CD_HUD.modelDefault
model._axis = .horizontal
model._isSquare = false
self.cd.hud(.warning, title: title, model:model)
}
}
複製代碼