JLActivityIndicator
用于替换 iOS 内置的 UIActivityIndicator
。为您的旋转器提供自定义图片,或提供任何 UIBezierPath
!使用 Swift 和 Core Animation 框架编写。
安装
Cocoapods
Cocoapods 是 Cocoa 项目的依赖管理器。请在执行以下内容之前确保安装它
要安装 JLActivityIndicator
,将以下内容添加到您的 Podfile
pod 'JLActivityIndicator'
然后在终端中运行此命令
pod install
Carthage
Carthage 是一个去中心化的依赖管理器,用于构建您的依赖并为您提供二进制框架。要使用 Carthage 将 JLActivityIndicator
集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它
github "ljw980105/JLActivityIndicator"
运行 carthage bootstrap
以在您仓库的 Carthage 目录中构建框架。
使用方法
首先在文件顶部添加 import JLActivityIndicator
。
可用模式
JLActivityIndicator
有两种动画模式,通过 JLAnimationMode
枚举表示。
case path //supply custom UIBezierPath objects for the activity indicator
case image // supply a custom image
自定义图片
提供自定义的 UIImage
对象。当调用 start()
方法时开始旋转,调用 end()
方法时停止旋转。
let spinner = JLActivityIndicator(on: view, mode: .image)
spinner.image = UIImage(named: "myImage")
spinner.start()
DispatchQueue.global(qos: .userInitiated).async {
// time consuming task
DispatchQueue.main.async {
spinner.stop()
}
}
自定义路径
提供自定义的 UIBezierPath
对象,代码将绘制一个或多个自定义路径!可以通过填充 paths
数组来添加更多路径。
// The code below produces the circular animation with the red color, as shown in the beginning.
let spinner = JLActivityIndicator(on: view, mode: .path)
spinner.paths = [JLBezierPath(strokeColor: UIColor.red, strokePath: UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 60, height: 60)))],
spinner.start()
DispatchQueue.global(qos: .userInitiated).async {
// time consuming task
DispatchQueue.main.async {
spinner.stop()
}
}
示例心跳路径
let path = UIBezierPath() // contained in a 100 x 100 frame
path.move(to: CGPoint(x: 0, y: 60))
path.addLine(to: CGPoint(x: 20, y: 60))
path.addLine(to: CGPoint(x: 40, y: 20))
path.addLine(to: CGPoint(x: 60, y: 100))
path.addLine(to: CGPoint(x: 80, y: 60))
path.addLine(to: CGPoint(x: 100, y: 60))
let spinner = JLActivityIndicator(on: view, mode: .path)
spinner.paths = [JLBezierPath(strokeColor: UIColor.red, strokePath: path)]
spinner.start()
可定制属性
对于 .image
模式,您可以指定图像、动画时长、是否添加灰色背景以及旋转的方向。其他所有定制将被忽略。
spinner.image = UIImage(named: "myImage")
spinner.duration = 1.0 // defaults to 1 second
spinner.reverseDirection = false // defaults to false
spinner.enableBackdrop = false // defaults to false
对于 .path
模式,有更多可定制选项。将更多项目添加到 paths
数组将导致数组中包含的所有路径同时动画化。
此外,JLBezierPath
结构是封装了 UIBezierPath
的包装器,这是库所要求的。您可以自定义 JLBezierPath
的颜色、宽度和实际贝塞尔路径。
// All properties of JLBezierPath have default values.
// strokeWidth defaults to 3.0, strokeColor defaults to lightGray, and the default path is a 60x60 circle.
// To get started by using default values, you can simply call 'let bezierPath = JLBezierPath()'.
let bezierPath = JLBezierPath(strokeWidth: 5.0,
strokeColor: UIColor.blue,
strokePath: UIBezierPath(ovalIn: CGRect(x: 0, y: 0, width: 40, height: 40)))
spinner.duration = 1.0 // defaults to 1 second
spinner.reverseDirection = false // defaults to false
spinner.enableBackdrop = false // defaults to false
spinner.paths = [bezierPath] // if you don't assign the paths, the activity indicator's path will default to a 60 x 60 gray circle w/ a stroke width of 3.0
许可
此仓库遵循MIT许可协议。