SimpleGradientButton
SimpleGradientButton 是一个以 Swift 编写的适用于 iOS 的简单的渐变按钮。您可以在不接触 Core Animation 的情况下指定垂直或水平渐变颜色。此外,它还支持圆角,以方便您使用。
要求
- iOS 8.0+
- Xcode 10.0+
- Swift 4.2
安装
CocoaPods
SimpleGradientButton 通过 CocoaPods 提供使用。要安装它,您只需将以下行添加到您的 Podfile 中
pod 'SimpleGradientButton'
然后,运行以下命令
pod install
Carthage
Carthage 也受支持。请将以下行添加到您的 Cartfile 中
github "ildar-gilfanov/SimpleGradientButton" ~> 1.0
然后,运行以下命令
carthage update
示例
不要忘记导入模块
import SimpleGradientButton
最简单的示例
您可以像创建普通按钮一样创建按钮,并传递包含渐变颜色的数组来指定颜色。构造函数的第二个参数是渐变点的相对位置。
let button = GradientButton()
button.setTitle("Press me!", for: .normal)
if let color1 = GradientColor(color: .yellow, position: 0),
let color2 = GradientColor(color: .green, position: 1.0) {
button.setGradientColors([color1, color2], for: .normal)
}
自定义
您可以指定渐变方向、圆角半径并启用颜色变化的动画。
let button = GradientButton()
button.setTitle("Press me!", for: .normal)
button.gradientDirection = .horizontal // horizontal or vertical
button.cornerRadius = .specific(4.0) // specific or automatic (height / 2)
button.disableGradientChangeAnimation = false // enable implicit CoreAnimation's animation
// You don't have to sort colors by position
if let color1 = GradientColor(color: .red, position: 0),
let color2 = GradientColor(color: .yellow, position: 1.0),
let color3 = GradientColor(color: .orange, position: 0.5) {
button.setGradientColors([color1, color2, color3], for: .normal)
}
渐变颜色是状态特定的
您可以为按钮的每个状态选择不同的渐变。
let button = GradientButton()
button.setTitle("Press me!", for: .normal)
// Gradient from one color is also posible. Position in this case is ignored.
if let color = GradientColor(color: .cyan, position: 0) {
button.setGradientColors([color], for: .normal)
}
if let color1 = GradientColor(color: .green, position: 0),
let color2 = GradientColor(color: .blue, position: 1.0) {
button.setGradientColors([color1, color2], for: .highlighted)
}
许可证
SimpleGradientButton 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。