MKAToggleButton 1.3.1

MKAToggleButton 1.3.1

Hituzi Ando 维护。



  • Hituzi Ando

MKAToggleButton

MKAToggleButton 是具有多种状态的按钮,适用于 iOS。请参见以下示例。

入门

将框架安装到您的 iOS 应用

有几种方法可以安装此框架。

1. CocoaPods

MKAToggleButton 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行

pod "MKAToggleButton"

2. 手动安装

  1. 下载最新版本的 MKAToggleButton
  2. 将 MKAToggleButton.framework 拖放到您的 Xcode 项目中

快速使用

  1. 导入框架

    import MKAToggleButton
  2. 创建实例

    private lazy var toggleButton: MKAIconToggleButton = {
        // Creates an instance with options.
        let button = MKAIconToggleButton(items: [MKAToggleItem(image: UIImage(named: "circle"), title: "Circle"),
                                                 MKAToggleItem(image: UIImage(named: "square"), title: "Square"),
                                                 MKAToggleItem(image: UIImage(named: "triangle"), title: "Triangle"),
                                                 MKAToggleItem(image: UIImage(named: "star"), title: "Start")],
                                         font: UIFont.systemFont(ofSize: 40.0, weight: .bold),
                                         color: .gray)
    
        // Should use the click handler for user interaction.
        button.onClicked = { button in
            // `currentStateIndex` property returns the current state.
            // The toggle button automatically increments the state each time it is clicked.
            // When the current state is last, the next state is rewinded to the first.
            print("index=\(button.currentStateIndex)")
        }
    
        // Sets the initial state. By default, the initial state index is zero.
        button.currentStateIndex = 1
    
        return button
    }()
  3. 将切换按钮添加到父视图中

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.view.addSubview(self.toggleButton)
    }

在故事板中使用切换按钮

您可以在故事板中使用MKAToggleButton。使用方法如下。

  1. 在故事板中,将MKAIconToggleButton类设置到自定义类字段

  2. 将多个图像文件名,用逗号分隔,设置到图像名称字段

选项

模板渲染模式

当模板模式启用时,切换按钮将其tintColor应用到图标图像。

// Use the template rendering mode and set a color to `tintColor`.
toggleButton.isImageTemplate = true
toggleButton.tintColor = UIColor(red: 241.0 / 255.0, 
                                 green: 196.0 / 255.0, 
                                 blue: 15.0 / 255.0, 
                                 alpha: 1.0)

扩展触摸区域

您可以扩展按钮的触摸区域。设置touchableExtensionXxxx属性。

button.touchableExtensionTop = 16.0
button.touchableExtensionLeft = 40.0
button.touchableExtensionBottom = 16.0
button.touchableExtensionRight = 40.0

长按事件处理器

您可以设置处理长按事件的事件处理器。

button.longPressGesture.minimumPressDuration = 1.0
button.onLongPressBegan = { print("Began index=\($0.currentStateIndex)") }
button.onLongPressChanged = { print("Changed index=\($0.currentStateIndex)") }
button.onLongPressEnded = { print("Ended index=\($0.currentStateIndex)") }
button.onLongPressCancelled = { print("Cancelled index=\($0.currentStateIndex)") }

更多信息,请参阅我的示例代码