Skip to content

Instantly share code, notes, and snippets.

View timothycosta's full-sized avatar

Timothy Costa timothycosta

View GitHub Profile
@timothycosta
timothycosta / ViewPlayground.swift
Created August 9, 2019 08:33
Resetting @State when other state changes
struct StateUpdatedPreferenceKey: PreferenceKey {
static var defaultValue: Int = Int.min
static func reduce(value: inout Int, nextValue: () -> Int) {
value = nextValue()
}
}
struct StateUpdated: View {
@timothycosta
timothycosta / TrackedValue.swift
Created April 10, 2020 04:43
Replacement for didSet when using SwiftUI
/// TrackedValue is a property wrapper that allows you to work around the
/// lack of didSet when working with SwiftUI.
/// Explanation and example usage at: https://stackoverflow.com/a/60145397/467209
@propertyWrapper
struct TrackedValue<Tracked, Value>: DynamicProperty {
var trackedHolder: State<ValueHolder<Tracked>>
var valueHolder: State<ValueHolder<Value>>
init(wrappedValue value: Value, tracked: Tracked) {
self.trackedHolder = State(initialValue: ValueHolder(tracked))
@timothycosta
timothycosta / SwiftUIKeyboardAnimation.swift
Last active April 9, 2025 14:42
Create a SwiftUI Animation with the correct curve and duration from UIKit keyboard notifications
func animation(from notification: Notification) -> Animation? {
guard
let info = notification.userInfo,
let duration = info[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
let curveValue = info[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int,
let uiKitCurve = UIView.AnimationCurve(rawValue: curveValue)
else {
return nil
}