TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Aug 2017 |
SwiftSwift Version | 3.0.2 |
SPMSupports SPM | ✓ |
Maintained by Dimitris C..
A simple and custom UISwitch made out of images.
To use ToggleSwitch as a Swift Package Manager package just add the following in your Package.swift file.
import PackageDescription
let package = Package(
name: "HelloToggleSwitch",
dependencies: [
.Package(url: "https://github.com/dimitris-c/ToggleSwitch.git", "1.0")
]
)
If you prefer not to use either of the aforementioned dependency managers, you can integrate ToggleSwitch into your project manually.
+
button under the "Embedded Binaries" section.ToggleSwitch.framework
.Pretty standard integration as it's like adding a UISwitch.
let images = ToggleSwitchImages(baseOnImage: UIImage(named: "base_on"),
baseOffImage: UIImage(named: "base_off"),
thumbOnImage: UIImage(named: "thumb_on"),
thumbOffImage: UIImage(named: "thumb_off"))
// ToggleSwitch will use the baseOnImage to construct the size of the control
let onOffSwitch = ToggleSwitch(with: images)
onOffSwitch.frame.origin = CGPoint(x: 100, y: 100)
self.addSubview(onOffSwitch)
The control exposes two ways of retrieving when the value/state has changed.
onOffSwitch.stateChange = { isOn in
if isOn {
// do something
}
}
onOffSwitch.addTarget(self, action: #selector(toggleValueChanged), for: .valueChanged)
@objc func toggleValueChanged(control: ToggleSwitch) {
if onOffSwitch.isOn {
// do something
}
}
Similar to UISwitch, ToggleSwitch exposes isOn
and setOn(on:animated:)
method
onOffSwitch.isOn = true
onOffSwitch.setOn(on: false, animated: true)
ToggleSwitch is released under the MIT license. See LICENSE for details.