使用 Swift 5.2 编写的自定义动画分段控件
.package(
url: "https://github.com/mednoor/MNSwitchi.git",
from: "0.2.0"
)
import MNSwitchi
你可以从代码中初始化一个 MNSwitchi
实例
let switchiView = MNSwitchiView(frame: CGRect(x: 0, y: 0, width: 240, height: 36))
view.addSubview(switchiView)
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)")
}