I am using Shape animatableData to animate a random value for use, the value that get animated does not need a Shape or any other view modifier either or AnimatableModifier, I am using Shape just for accessing animatableData I could be get same result with AnimatableModifier as well but the idea is just accessing those sweet values, here my code:
struct ContentView: View {
@State private var value: CGFloat = .zero
var body: some View {
ZStack {
Button("update value") {
value += 10.0
}
BaseShape(value: value)
}
.frame(width: 300.0, height: 200.0)
.animation(Animation.easeInOut(duration: 5.0), value: value)
}
}
struct BaseShape: Shape {
var value: CGFloat
init(value: CGFloat) {
self.value = value
}
internal var animatableData: CGFloat {
get { return value }
set(newValue) { value = newValue }
}
func path(in rect: CGRect) -> Path {
print(value)
return Path()
}
}
With that said, I want build a function to put out the Shape that I do not need it, like this:
struct AnimatableType: Animatable {
var value: CGFloat
var animatableData: CGFloat {
get { return value }
set(newValue) {
value = newValue
}
}
}
func valueAnimator(value: CGFloat, animatedValue: @escaping (AnimatableType) -> Void) {
// Incoming value get processed ...
// Sending the animatedValue ...
animatedValue(AnimatableType(value: value))
}
And my use case goal is like this:
struct ContentView: View {
@State private var value: CGFloat = .zero
var body: some View {
ZStack {
Button("update value") {
value += 10.0
valueAnimator(value: value, animatedValue: { value in
print(value.animatableData)
})
}
}
.frame(width: 300.0, height: 200.0)
.animation(Animation.easeInOut(duration: 5.0), value: value)
}
}
Is it a way around to reach the goal, the use case goal with Swift? or SwiftUI?