Skip to content

Instantly share code, notes, and snippets.

@ObuchiYuki
Created September 20, 2025 07:53
Show Gist options
  • Select an option

  • Save ObuchiYuki/5bd6cceeb2a6da5298c18240b4fcb6f9 to your computer and use it in GitHub Desktop.

Select an option

Save ObuchiYuki/5bd6cceeb2a6da5298c18240b4fcb6f9 to your computer and use it in GitHub Desktop.
private struct EquatableArray: Equatable {
private var values = [Any]()
private var compares = [(Any) -> Bool]()
mutating func append<T: Equatable>(_ value: T) {
values.append(value)
compares.append { ($0 as? T) == value }
}
static func == (lhs: EquatableArray, rhs: EquatableArray) -> Bool {
lhs.values.count == rhs.values.count && zip(lhs.compares, rhs.values).allSatisfy { $0($1) }
}
}
extension View {
/// A version of `.animation(_:value:)` that accepts multiple equatable values.
///
/// Usage:
///
/// .animation(.spring(), values: boolValue, intValue, enumValue, stringValue, ...)
public func animation<each T: Equatable>(_ animation: Animation?, values: repeat each T) -> some View {
var equatableArray = EquatableArray()
for value in repeat each values {
equatableArray.append(value)
}
return self.animation(animation, value: equatableArray)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment