MNSwitchi

使用 Swift 5.2 编写的自定义动画分段控件

Twitter: @a_moha

要求

安装

Swift Package Manager

.package(
    url: "https://github.com/mednoor/MNSwitchi.git",
    from: "0.2.0"
)

用法

导入 MNSwitchi 模块

import MNSwitchi

初始化

你可以从代码中初始化一个 MNSwitchi 实例

let switchiView = MNSwitchiView(frame: CGRect(x: 0, y: 0, width: 240, height: 36))
view.addSubview(switchiView)

设置 MNSwitchi

switchiView.render(
	content: MNSwitchiContent,
	appearance: MNSwitchiAppearance,
	options: MNSwitchiOptions
)

配置内容

为了设置项目,你需要传递一个 MNSwitchiContent 类型的元组

let items: MNSwitchiContent = (first: "First", second: "Second")

外观

为了设置样式,你需要传递一个 MNSwitchiAppearance 类型的对象

let appearance = MNSwitchiAppearance(backgroundColor: UIColor,
                                     cornersRounded: Bool,
                                     selectedViewBackgroundColor: UIColor,
                                     states: MNSwitchiStates)

然后创建一个 MNSwitchiStates 的实例

let states = MNSwitchiStates(

default: MNSwitchiStates.MNSwitchiState(textColor: UIColor,
                                        titleFont: UIFont),

selected: MNSwitchiStates.MNSwitchiState(textColor: UIColor,
                                         titleFont: UIFont),
            
highlighted: MNSwitchiStates.MNSwitchiState(textColor: UIColor,
                                            titleFont: UIFont)
)

自定义

可以通过传递 MNSwitchiOptions 的实例来自定义 MNSwitchi

let options = MNSwitchiOptions(defaultSelectedItem: Int,
                               enableAnimation: Bool)

处理回调

switchiView.switchedTo = { index in
    print("Selected item at: \(index)")
}