测试已测试 | ✓ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016年10月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Hao 维护。
HActionButton 是一个可自定义的动作按钮,它继承自 UIView,其中主按钮、项目按钮和动画都可以自定义。
除了定制灵活性外,默认设置和内置功能 also 提供了很好的 pod 可用性。
可选
项目按钮配置可选
每个项目按钮的中心位置可选
在动作按钮激活/非激活后调用的函数可选
动画时间可选
活动和非活动状态时主按钮、项目按钮和背景视图的配置要运行示例项目,请克隆仓库,然后首先从 Example 目录运行 pod install
HActionButton 通过 CocoaPods 提供。要安装它,只需将其以下行添加到您的 Podfile 中
pod "HActionButton"
Swift 版本 | 版本 |
---|---|
2.3 | 2.0.x |
3.0 | 2.1.0 或更高版本 |
您可以使用代码以编程方式添加 HActionButton
let actionButton = HActionButton()
view.addSubview(actionButton)
// Customized configuration
// actionButton.mainButton.backgroundColor = UIColor.blueColor()
// actionButton.backgroundView.backgroundColor = UIColor.darkGrayColor()
// .
// .
或者只需将 UIView 拖放到 storyboard(Interface Builder)中,并将类别设置为 HActionButton
。
class ViewController: UIViewController, HActionButtonDataSource, HActionButtonDelegate{
@IBOutlet weak var actionButton: HActionButton!
// .
// .
// .
override func viewDidLoad(){
super.viewDidLoad()
actionButton.dataSource = self
actionButton.delegate = self
//.
//.
//.
}
// .
// .
// .
// MARK: - HActionButtonDataSource
func numberOfItemButtons(actionButton: HActionButton) -> Int {
return 20
}
func actionButton(actionButton: HActionButton, itemButtonAtIndex index: Int) -> UIButton {
// return circle button with random color by default
return HActionButton.CircleItemButton(actionButton)
}
func actionButton(actionButton: HActionButton, relativeCenterPositionOfItemAtIndex index: Int) -> CGPoint{
// return circle button with random color by default
return HActionButton.EquallySpacedArcPosition(actionButton, atIndex: index, from: 0, to: 2 * M_PI)
}
// MARK: - HActionButtonDelegate
func actionButton(actionButton: HActionButton, didClickItemButtonAtIndex index: Int) {
actionButton.toggle()
print("button \(index) clicked")
}
}
现在,您的动作按钮应该开始工作了。查看以下部分以定制动作按钮。
主要操作按钮可以通过配置 mainButton
进行自定义,该配置定义在 HActionButton
类中。
// swift 2.3
public var mainButton: UIButton!
// swift 3.0
open var mainButton: UIButton!
实现自定义每个项目按钮。
class ViewController: UIViewController, HActionButtonDataSource{
// .
// .
// .
// MARK: - HActionButtonDataSource
func numberOfItemButtons(actionButton: HActionButton) -> Int {
return 20
}
// Optional
func actionButton(actionButton: HActionButton, itemButtonAtIndex index: Int) -> UIButton {
// return circle button with random color by default
return HActionButton.CircleItemButton(actionButton)
}
// Optional
func actionButton(actionButton: HActionButton, relativeCenterPositionOfItemAtIndex index: Int) -> CGPoint{
// return circle button with random color by default
return HActionButton.EquallySpacedArcPosition(actionButton, atIndex: index, from: 0, to: 2 * M_PI)
}
}
实现按钮动作的分派。
class ViewController: UIViewController, HActionButtonDataSource, HActionButtonDelegate{
// .
// .
// .
// MARK: - HActionButtonDelegate
func actionButton(actionButton: HActionButton, didClickItemButtonAtIndex index: Int) {
actionButton.toggle()
print("button \(index) clicked")
}
// Optional
func actionButton(actionButton: HActionButton, didBecome active: Bool) {
}
}
实现自定义动画
class ViewController: UIViewController, HActionButtonDataSource, HActionButtonDelegate, HActionButtonAnimationDelegate{
// .
// .
// .
// MARK: - HActionButtonAnimationDelegate
// Optional
func actionButton(actionButton: HActionButton, animationTimeForStatus active: Bool) -> NSTimeInterval {
// return 0.3 by default
return active ? 2 : 0.3
}
// Optional
func actionButton(actionButton: HActionButton, confugureMainButton mainButton: UIButton, forStatus active: Bool) {
// rotate button with PI/2 back and forth for status by default
HActionButton.RotateButton(mainButton, byAngle: M_PI/4, forStatus: active)
}
// Optional
func actionButton(actionButton: HActionButton, confugureItemButton itemButton: UIButton, atIndex index: Int, forStatus active: Bool) {
// set alpha of itemButton by default
itemButton.alpha = CGFloat(active)
}
// Optional
func actionButton(actionButton: HActionButton, confugureBackgroundView backgroundView: UIView, forStatus active: Bool) {
// set alpha of backgroundView by default
backgroundView.alpha = CGFloat(active)
backgroundView.backgroundColor = UIColor(red: CGFloat(drand48()), green: CGFloat(drand48()), blue: CGFloat(drand48()), alpha: 0.4)
}
}
郝
HActionButton 在 MIT 许可下可用。更多信息请参阅 LICENSE 文件。