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
).
By default, the class uses the SFSymbols checkbox symbols, but these are sometimes considered a bit awkward for UIKit (which is why UIKit doesn’t use them).
You can also supply three of your own images to use for the control (or only two, if you are sticking with the “two-state” version).
Images that specify .alwaysTemplate
are drawn 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 public, read-only accessor for the next state.
Declaration
Swift
@MainActor open var nextState: States { get }
-
This holds the control’s current state (before any changes).
Default is clear.
Declaration
Swift
@MainActor open var checkboxState: States { 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 is assumed false, but it can be forced true, programmatically.
Declaration
Swift
@MainActor open var isUsingSFSymbols: Bool { get set }
-
This is the image to be displayed in an “ON” state.
Note
If either
offImage
oronImage
is nil, then the checkbox will use SFSymbols. It must have BOTH to allow custom images.Declaration
Swift
@IBInspectable @MainActor open override var onImage: UIImage? { get set }
-
This is the image to be displayed in an “OFF” state.
Note
If either
offImage
oronImage
is nil, then the checkbox will use SFSymbols. It must have BOTH to allow custom images.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.
Note
If a clear image is not avaialable, then
useOffImageForClear
is forced on.isUsingSFSymbols
mode (default) will always have the image avaialable.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).
Note
If a clear image is not avaialable, then
useOffImageForClear
is forced on.isUsingSFSymbols
mode (default) will always have the image avaialable.Declaration
Swift
@IBInspectable @MainActor open var isThreeState: Bool { get set }
-
If this is true (default is false), then CLEAR will use the image supplied for the three-state OFF.
Note
If a clear image is not avaialable, then
useOffImageForClear
is forced on.isUsingSFSymbols
mode (default) will always have the image avaialable.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 (if two-state, this is not returned -OFF is returned, instead)
- 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)
inIsAnimated
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
inIsAnimated
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.
inIsAnimated
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.
inIsAnimated
If true, the change is animated.
-
The property is overridden, so we will trigger a redraw, if it changes.
Declaration
Swift
@MainActor override open var tintColor: UIColor! { get set }
-
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).
inEvent
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).
inEvent
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).
inEvent
The event (passed to superclass).
-
Called when a touch is canceled.
Declaration
Swift
@MainActor override open func cancelTracking(with inEvent: UIEvent?)
Parameters
inEvent
The event (passed to superclass).