安装
LabelSwitch可通过CocoaPods获取。要安装它,只需将以下行添加到Podfile中:
pod 'CPLabelSwitch'
用法
您可以通过代码或InterfaceBuilder创建视图
let ls = LabelSwitchConfig(text: "Text1",
textColor: .white,
font: UIFont.boldSystemFont(ofSize: 15),
backgroundColor: .red)
let rs = LabelSwitchConfig(text: "Text2",
textColor: .white,
font: UIFont.boldSystemFont(ofSize: 20),
backgroundColor: .green)
// Set the default state of the switch,
let labelSwitch = LabelSwitch(center: .zero, leftConfig: ls, rightConfig: rs)
// Set the appearance of the circle button
labelSwitch.circleShadow = false
labelSwitch.circleColor = .red
// Make switch be triggered by tapping on any position in the switch
labelSwitch.fullSizeTapEnabled = true
// Set the delegate to inform when the switch was triggered
labelSwitch.delegate = self
extension ViewController: LabelSwitchDelegate {
func switchChangToState(_ state: LabelSwitchState) {
switch state {
case .L: print("circle on left")
case .R: print("circle on right")
}
}
}
您还可以设置切换背景为图片或渐变色
// gradient color
init(text: String, textColor: UIColor, font: UIFont, gradientColors: [CGColor], startPoint: CGPoint, endPoint: CGPoint)
// image
init(text: String, textColor: UIColor, font: UIFont, image: UIImage?)
作者
artep, [email protected]
许可证
CPLabelSwitch在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。