Created
July 6, 2022 20:00
-
-
Save insidegui/091bfc42f59f35148c158e7e7e664a86 to your computer and use it in GitHub Desktop.
Handy debugging extension on SwiftUI's Animation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
/// On macOS, modifying an Animation with .currentSpeed(), or using the .current static property | |
/// allows for easy animation debugging by holding down the Shift key when triggering the animation. | |
/// When the animation is triggered while the Shift key is pressed, it will be played in slow motion. | |
/// Using this extension has no effect when targeting other OSes or when building for release. | |
extension Animation { | |
static var currentSpeed: Double { | |
#if DEBUG | |
#if os(macOS) | |
if NSEvent.modifierFlags.contains(.shift) { | |
return 0.1 | |
} else { | |
return 1 | |
} | |
#else | |
return 1 | |
#endif | |
#else | |
return 1 | |
#endif | |
} | |
static var current: Animation { Animation.default.currentSpeed() } | |
func currentSpeed() -> Animation { | |
speed(Self.currentSpeed) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment