SimpleButton
具有动画、状态感知属性的 UIButton 子类。易于子类化和配置!完整 API 文档
使用方法
只需创建自己的 SimpleButton
子类,并通过重写 configureButtonStyles
来配置按钮属性。
class PrimaryButton: SimpleButton {
override func configureButtonStyles() {
super.configureButtonStyles()
setBorderWidth(4.0, for: .normal)
setBackgroundColor(UIColor(red: 52/255, green: 73/255, blue: 94/255, alpha: 1.0), for: .normal)
setBackgroundColor(UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0), for: .highlighted)
setBorderColor(UIColor(red: 44/255, green: 62/255, blue: 80/255, alpha: 1.0), for: .normal)
setScale(0.98, for: .highlighted)
setTitleColor(UIColor.whiteColor(), for: .normal)
}
}
在 Interfacebuilder 中使用,只需将您的 SimpleButton
子类作为任何 UIButton
元素的自定义类即可。所有定义的样式将自动应用。
您还可以直接内联配置按钮而不使用子类。
let awesomeButton = SimpleButton(type: .custom)
awesomeButton.setBorderWidth(2.0, for: .normal)
awesomeButton.setBorderColor(UIColor.redColor(), for: .highlighted)
view.addSubview(awesomeButton)
请注意,您应该使用 UIButtonType.custom
以避免不期望的效果。
请查阅示例项目以获取详细的使用演示。
@IBDesignable
请查看示例项目中的 DesignableButton 子类,以了解 @IBDesignable
的使用。
动画
SimpleButton
的每种状态变化默认都会进行动画。有时您可能需要指定哪些状态转换应该进行动画,哪些应该立即发生。因此,您可以使用 animated
和 animationDuration
参数来控制这种行为。
setBorderWidth(4.0, for: .normal, animated: true, animationDuration: 0.2)
setBorderWidth(8.0, for: .highlighted, animated: false)
这意味着,每次状态变化到 .normal
都会动画 borderWidth
到 4.0
。每次状态变化到 .highlighted
都会即时将 borderWidth
改为 8.0
,不进行动画。
加载状态
SimpleButton
具有一个自定义的 loading
状态。您可以通过设置 simpleButton.isLoading
来切换此状态。当添加 loading
状态时,按钮将显示 UIActivityIndicator
,而不是标题。
simpleButton.setCornerRadius(20, for: SimpleButtonControlState.loading)
simpleButton.isLoading = true
如果您不喜欢默认的加载指示器,您可以通过以下方式设置自己的 UIView
:
simpleButton.loadingView = CustomAwesomeLoadingView()
请注意,在使用自定义加载视图时,不要忘记自己处理位置和初始添加到子视图,以满足您的需求。
可配置属性
请参阅 API 文档 中的 状态属性设置器
部分。
安装
请注意,SimpleButton 是用 swift 4
编写的,可能与之前的 Swift 版本不兼容。
Carthage
将以下行添加到您的 Cartfile 中。
github "aloco/SimpleButton" ~> 4.0
然后运行 carthage update
。
手动
只需将 SimpleButton.swift
文件拖放到您的项目中。
贡献
- 创造一些了不起的东西,改进代码,添加一些功能,无论什么(这是最困难的部分)。
- 分支
- 创建新的分支以进行更改
- 将您的所有更改提交到您的分支
- 提交一个pull request