RVS_Checkbox
@IBDesignable
@MainActor
open class RVS_Checkbox : UISwitch
This class provides a simple Swift-only module that implements a “checkbox” to replace the standard UISwitch.
The switch can behave exactly like a standard UISwitch, but also has a “three-state” option, where it has definitely off, definitely on, and clear, which is a sort of “undefined” state. Default is two-state (like UISwitch).
The class can use the SFSymbols checkbox symbols, but these are a bit awkward for UIKit (which is why UIKit doesn’t use them), so we have three dynamically-generated “artisanal” images that can be used to
implement a round checkbox, which is friendlier for fat fingers.
You can also supply three images to use for the control (or only two, if you are sticking with the “two-state” version).
All images are drawn template mode, using the control tintColor
.
Like the UISwitch class, you can specifically call setOn(_:animated:)
, and it will animate the transition. You can also call setClear(animated:)
, if in three-state mode.
This uses haptics, in the same manner as UISwitch, except that you can turn them off, by setting useHaptics
to false.
-
These Int-based enum values define the possible switch states.
See moreDeclaration
Swift
public enum States : Int
-
This is the image to be displayed in an “ON” state.
Declaration
Swift
@IBInspectable @MainActor open override var onImage: UIImage? { get set }
-
This is the image to be displayed in an “OFF” state.
Declaration
Swift
@IBInspectable @MainActor open override var offImage: UIImage? { get set }
-
This is the image to be displayed in a “CLEAR” (“OFF,” if two-state) state.
Declaration
Swift
@IBInspectable @MainActor open var clearImage: UIImage? { get set }
-
If this is true, then control is three state (OFF-CLEAR-ON). Otherwise (default), it is two state (OFF-ON).
Declaration
Swift
@IBInspectable @MainActor open var isThreeState: Bool { get set }
-
If true, then the control will not use the dynamically-generated images, and will, instead, use standard SFSymbols square images. If the user supplies images, then this will be ignored.
Declaration
Swift
@IBInspectable @MainActor open var isUsingSFSymbols: Bool { get set }
-
If this is true (default is false), then CLEAR will use the image supplied for the three-state OFF. This is ignored in three-state mode, or SFSymbols mode (which already does that).
Declaration
Swift
@IBInspectable @MainActor open var useOffImageForClear: Bool { get set }
-
If this is true (default), then the control will use subtle haptics. These will not happen for programmatic set; only for direct UI interaction.
Declaration
Swift
@IBInspectable @MainActor open var useHaptics: Bool { get set }
-
This is the control value, as an Int:
- -1: OFF
- 0: CLEAR (Or OFF, in two-state)
- 1: ON
Declaration
Swift
@MainActor public var value: Int { get set }
-
This returns true, if the control is currently in “CLEAR” state (which is also off, for two-state). READ-ONLY
Declaration
Swift
@MainActor public var isClear: Bool { get }
-
This returns true, if the control is in “OFF” state (three state), or either “off” or “clear” (two-state). READ-ONLY
Declaration
Swift
@MainActor public var isOff: Bool { get }
-
This returns true, if the control is in ON state. If explicitly set to false, the checkbox is set to OFF (or CLEAR for two state).
Declaration
Swift
@MainActor open override var isOn: Bool { get set }
-
This sets the control to either ON or OFF. It can be animated.
Declaration
Swift
@MainActor public override func setOn(_ inIsOn: Bool, animated inIsAnimated: Bool = false)
Parameters
inIsOn
If true, the control is set to ON. If false, the control is set to OFF (or CLEAR, in two-state)
animated
If true, the change is animated.
-
This sets the control to CLEAR (or OFF, in two-state). It can be animated.
Declaration
Swift
@MainActor public func setClear(animated inIsAnimated: Bool = false)
Parameters
animated
If true, the change is animated.
-
This explicitly sets the control state. It can be animated.
Declaration
Swift
@MainActor public func setState(_ inState: States, animated inIsAnimated: Bool = false)
Parameters
inState
The control state.
animated
If true, the change is animated.
-
This explicitly sets the control state. It can be animated.
Declaration
Swift
@MainActor public func setValue(_ inValue: Int, animated inIsAnimated: Bool = false)
Parameters
inValue
This is the desired control value, as an Int. If the number is any negative number, it is considered OFF. If it is any positive number, it is considered ON. 0 is CLEAR. If in two-state, negative numbers are the same as 0.
animated
If true, the change is animated.
-
It’s important for the background to be clear.
Declaration
Swift
@MainActor override open func layoutSubviews()
-
This draws the control, dimming, if tracking, and over the control.
Declaration
Swift
@MainActor override open func draw(_ inRect: CGRect)
Parameters
inRect
The rect to draw.
-
Called when the user starts a press.
Declaration
Swift
@MainActor override open func beginTracking(_ inTouch: UITouch, with inEvent: UIEvent?) -> Bool
Parameters
inTouch
The touch instance (passed to superclass).
with
The event (passed to superclass).
Return Value
True, if the touch is approved.
-
Called when the user starts a press.
Declaration
Swift
@MainActor override open func continueTracking(_ inTouch: UITouch, with inEvent: UIEvent?) -> Bool
Parameters
inTouch
The touch instance (passed to superclass).
with
The event (passed to superclass).
Return Value
True, if the drag/touch should continue.
-
Called when the user ends a press. If the touch ends inside the control, the value is changed, and messages are sent.
Declaration
Swift
@MainActor override open func endTracking(_ inTouch: UITouch?, with inEvent: UIEvent?)
Parameters
inTouch
The touch instance (passed to superclass).
with
The event (passed to superclass).
-
Called when a touch is canceled.
Declaration
Swift
@MainActor override open func cancelTracking(with inEvent: UIEvent?)
Parameters
with
The event (passed to superclass).