An implementation of a momentary switch: one that has an on/off state, can be turned on repeatedly, turns off after a delay. If triggered multiple times before the off delay is elapses, it re-schedules the reaction (relative to the last time it was triggered). Ideally used for triggering animations on user interaction, and auto-reverting once they’ve stopped interacting for enough time.
NOTE: The switch itself will operate on the background thread, but the on/off callbacks are executed on the @MainActor.
SwiftUIExtensions Library
An open source library with utilities and extensions for SwiftUI.
Developed as re-usable components for various projects at XII’s iOS, macOS, and watchOS applications.
Installation
Swift Package Manager
SwiftUIExtensions
library to add to your projectDependencies
License
See the LICENSE file.
Text case shorthands (Source)
Shorthand for applying the
textCase
ViewModifier
.Shorthand for stretching
View
s (Source)Sets the view’s frame to stretch to fill its parent by setting
maxWidth
(and/or)maxHeight
based on the requestedAxis
..Horizontal
as it’s the more common scenario.center
used as the defaultScenePhase
change shorthand (Source)Calls an
action
when theScenePhase
has changed. Avoid performing long-running tasks inaction
since it is called from the main thread.URL
open helpersCross platform (iOS + macOS)
iOS only
iOS keyboard helpers (Source)
HiddenView
shorthand (Source)A specialty view that has no visible representation (clear 0x0 pixel view) but can still have functionality attached (e.g.
onChange(of:)
).NOTE: If used in a container view that has spacing (e.g.
VStack
) this view will still be counted as one and will add unintended spacing.Avoid animations (Source)
Performs the provided block in a transaction with animations disabled.
Useful for cancelling some animations that are hard to control (e.g. navigation view transitions).
NOTE: This is a bit finicky, use at your own risk.
MomentarySwitch
Utility (Source)An implementation of a momentary switch: one that has an on/off state, can be turned on repeatedly, turns off after a delay. If triggered multiple times before the off delay is elapses, it re-schedules the reaction (relative to the last time it was triggered). Ideally used for triggering animations on user interaction, and auto-reverting once they’ve stopped interacting for enough time.
NOTE: The switch itself will operate on the background thread, but the on/off callbacks are executed on the
@MainActor
.