Last active
January 16, 2026 16:41
-
-
Save ktiays/0e482fac677931e236fed3cdb3fb3382 to your computer and use it in GitHub Desktop.
An `AnimatableValues` implementation for iOS 17+
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
| // | |
| // Created by ktiays on 2026/1/16. | |
| // Copyright (c) 2026 ktiays. All rights reserved. | |
| // | |
| import SwiftUI | |
| @available(iOS 17.0, *) | |
| public struct AnimatableValues<each Value>: VectorArithmetic where repeat each Value : VectorArithmetic { | |
| /// The tuple of values. | |
| public var value: (repeat each Value) | |
| /// The zero value. | |
| public static var zero: Self { | |
| .init(repeat (each Value).zero) | |
| } | |
| /// The dot-product of the tuple of animatable values with itself. | |
| public var magnitudeSquared: Double { | |
| var result: Double = 0 | |
| for element in repeat each value { | |
| result += element.magnitudeSquared | |
| } | |
| return result | |
| } | |
| /// Creates a tuple of animatable values. | |
| public init(_ value: repeat each Value) { | |
| self.value = (repeat each value) | |
| } | |
| /// Multiplies each component of this value by the given value. | |
| public mutating func scale(by rhs: Double) { | |
| self.value = (repeat (each value).scaled(by: rhs)) | |
| } | |
| public static func + (lhs: Self, rhs: Self) -> Self { | |
| .init(repeat (each lhs.value) + (each rhs.value)) | |
| } | |
| public static func - (lhs: Self, rhs: Self) -> Self { | |
| .init(repeat (each lhs.value) - (each rhs.value)) | |
| } | |
| public static func == (lhs: Self, rhs: Self) -> Bool { | |
| let (lhs, rhs) = (lhs.value, rhs.value) | |
| for (left, right) in repeat (each lhs, each rhs) { | |
| if left != right { | |
| return false | |
| } | |
| } | |
| return true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment