SSTSwitch
示例
要运行示例项目,请克隆仓库,并首先从 Example 目录中运行 pod install
。
要求
- iOS 10.0+
- Swift 4.2+
安装
SSTSwitch 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile:
pod 'SSTSwitch'
用法
此 pod 中的开关有两种使用方法。
- 通过 Interface Builder 和 IBOutlet 初始化
- 通过代码方式初始化按钮
- 将
SSTSwitch
导入您想使用的类
import SSTSwitch
通过界面构建器和 IBOutlet
- 从对象库中拖拽一个
UIView
元素,并设置其宽、高、XY 约束。 - 使用类型为
SSTSwitch
的连接到其文件所属者。
- 您 必须 将其
delegate
设置为将监听此开关状态变化的类文件。您还可以指定其switchType
和其他属性。
@IBOutlet weak var switchOnStoryboard: SSTSwitch!
// You can set it up in viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
// Clearing the backgroundColor we set in the InterfaceBuilder for visibility during that time.
switchOnStoryboard.backgroundColor = .clear
switchOnStoryboard.switchType = .ios
switchOnStoryboard.delegate = self
}
- 设置代理将要求类文件符合 SSTSwitchDelegate 协议。在本例中,ViewController 类将符合此协议。
extension ViewController: SSTSwitchDelegate {
func didToggleSwitch(currentState: SSTSwitchState) {
if currentState == .on {
// Handle On Actions here
print("Switch is On")
} else {
// Handle Off Actions here
print("Switch is Off")
}
}
}
- 它将显示如下
用代码初始化
SSTSwitch 有一个初始化器,它接受一个必需的 type
参数和许多其他可选参数。然而,这些可选参数可以通过运行时明确地通过属性设置。
- 初始化 SSTSwitch。在以下代码之后,将初始化一个处于 开启 状态的 SSTSwitch。
let codeSwitch = SSTSwitch(type: .material, state: .on)
- 其代理 必须 设置为一个类,并且其 XYZ 位置约束 必须 也添加以正确显示。
codeSwitch.delegate = self
self.view.addSubview(codeSwitch)
codeSwitch.translatesAutoresizingMaskIntoConstraints = false
codeSwitch.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: 0).isActive = true
codeSwitch.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive = true
- 现在该类需要符合
SSTSwitchDelegate
。
extension ViewController: SSTSwitchDelegate {
func didToggleSwitch(currentState: SSTSwitchState) {
if currentState == .on {
// Handle On Actions here
print("Switch is On")
} else {
// Handle Off Actions here
print("Switch is Off")
}
}
}
- 完成!开关将显示如下
可自定义属性
开关类型
目前实现了四种开关类型。
- iOS 开关
- 带图片的 iOS 开关
- 材料开关
- 圆形开关
您可以通过以下方式更改开关的类型
let codeSwitch = SSTSwitch(type: .ios)
// iOS Switch
codeSwitch.switchType = .ios
// iOS Switch with Image, you have to specify its image to have it appear in the knob
codeSwitch.switchType = .iosImg
codeSwitch.image = UIImage(named: "grape")
// Material Switch
codeSwitch.switchType = .material
// Rounded Switch, you can specify custom corner radius for this type
codeSwitch.switchType = .rounded
codeSwitch.switchCornerRadius = 20
状态
您可以通过为所有类型的开关指定state
属性来设置开关的状态。
let codeSwitch = SSTSwitch(type: .ios)
// Have it on
codeSwitch.state = .on
// or Have it off
codeSwitch.state = .off
// Note that this changes state instantly, therefore this property should only be used for the starting state.
// You will see additional states (onSuspendState and offSuspendState) through the auto-completion. They should be ignored.
开关开启和关闭状态的颜色(适用于旋钮和凹痕)
这些颜色默认值与您的switchType
相关。但是,您可以指定旋钮和凹痕每个状态的自定义颜色。旋钮:那个抓取的东西,凹痕:那个滑块东西
let codeSwitch = SSTSwitch(type: .ios)
// On-Color for the Knob
codeSwitch.activeColorKnob = .red
// Off-Color for the Knob
codeSwitch.idleColorKnob = .blue
// On-Color for the Crease
codeSwitch.activeColorCrease = .gray
// Off-Color for the Crease
codeSwitch.idleColorCrease = .lightGray
属性基本上是自我解释的。
IOS图片类型的图片补丁
您可以指定图片在旋钮内的填充。默认值是零。
let codeSwitch = SSTSwitch(type: .iosImg)
codeSwitch.image = UIImage(named: "grape")
// 5 means that the image will be padded 5 points all around
codeSwitch.imgPadding = 5
作者
bupstan, [email protected]
许可证
SSTSwitch采用MIT许可证。更多信息请查看LICENSE文件。