Created
September 20, 2025 07:53
-
-
Save ObuchiYuki/5bd6cceeb2a6da5298c18240b4fcb6f9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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