美观、简洁、可定制且易于使用的 iOS 进度 HUD,使用 Swift 编写。
Swift Package Manager 是一个用于自动化 Swift 代码分发的工具,并已集成到 swift
编译器中。要将包依赖项添加到您的 Xcode 项目,请选择 File > Swift Packages > Add Package Dependency 并输入 https://github.com/treatwell/TWHud.git
。
将以下条目添加到您的 Cartfile
github "treatwell/TWHud"
首先,导入 TWHud
import TWHud
然后,在您的 AppDelegate
中,您必须配置 TWHud
。必需的配置参数是 maskImage
- 用作 HUD 遮罩的图像,以及 colours
- 用于动画的颜色数组。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
TWHud.configure(with:
TWHud.Configuration(
maskImage: UIImage(named: "LoaderLogoMask")!,
colours: [.red, .green, .blue]
)
)
}
您可以配置更多内容,例如:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
TWHud.configure(with:
TWHud.Configuration(
maskImage: UIImage(named: "LoaderLogoMask")!,
cornerRadius: 5.0,
size: CGSize(width: 140, height: 140),
fillUpRect: CGRect(x: 14, y: 29, width: 72, height: 42),
fillUpTime: 0.6,
waitTime: 0.2,
hudBackgroundColour: .white,
containerBackgroundColour: UIColor.black.withAlphaComponent(0.5),
colours: [.red, .green, .blue]
)
)
}
在 AppDelegate
中配置 TWHud
后,您可以在 iOS 应用程序代码中的任何位置使用它
TWHud.show()
这将在您的所有 UI 上创建 HUD
使用以下方法关闭它
TWHud.dismiss()
如果您不需要在所有 UI 元素上都显示 HUD,您可以将其添加到您的自定义视图中
let hud = TWHud.showIn(
view: container,
configuration: TWHud.Configuration(
maskImage: UIImage(named: "LoaderLogoMask64")!,
hudBackgroundColour: UIColor.lightGray,
containerBackgroundColour: UIColor.lightGray,
colours: [.red, .green, .blue, .yellow]
)
)
使用以下方法关闭它
hud.dismiss()
如果您需要指定在当前颜色之后应使用哪种颜色,您可以在 AppDelegate
中的 TWHud
配置之后添加您的验证器。
TWHud.shared?.nextFillColourIndexIsValid = { next, previous in
// Next colour is different than current
var valid: Bool = next != previous
if valid {
if next == 0 {
valid = next != 6
} else if next == 6 {
valid = next != 0
} else if next == 3 {
valid = next != 5
} else if next == 5 {
valid = next != 3
}
}
return valid
}
Marius Kažemėkaitis - marius@treatwell.com
本仓库的内容根据 Apache License, version 2.0 获得许可。