HActionButton 2.1.0

HActionButton 2.1.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016年10月
SwiftSwift 版本3.0
SPM支持 SPM

Hao 维护。



  • 作者
  • Chang, Hao

HActionButton

HActionButton 是一个可自定义的动作按钮,它继承自 UIView,其中主按钮、项目按钮和动画都可以自定义。

除了定制灵活性外,默认设置和内置功能 also 提供了很好的 pod 可用性。

特性

  • [x] 定制化配置
    • 主按钮
    • 项目按钮
    • 背景视图
    • 项目位置
    • 动画
      • 持续时间
      • 活动/非活动状态配置

  • [x] 数据源协议
    • 设置项目按钮数量
    • 可选 项目按钮配置
    • 可选 每个项目按钮的中心位置

  • [x] 代理协议
    • 项目按钮动作
    • 可选 在动作按钮激活/非激活后调用的函数

  • [x] 动画代理协议
    • 可选 动画时间
    • 可选 活动和非活动状态时主按钮、项目按钮和背景视图的配置

  • [x] 文档

示例

示例

要运行示例项目,请克隆仓库,然后首先从 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!

协议

HActionButtonDataSource

实现自定义每个项目按钮。

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)
   }
}

HActionButtonDelegate

实现按钮动作的分派。

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) {

   }
}

HActionButtonAnimationDelegate

实现自定义动画

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 文件。