Hi!,
I have this ViewModifier in order to group 4 modifiers in just one in order to use with more than one View:
struct FadeInWithOffsetAnimation: ViewModifier {
let opacityAmount: Double
let opacityDelay: Double
let offsetAmount: Double
let offsetDelay: Double
func body(content: Content) -> some View {
content
.offset(y: offsetAmount)
.animation(.spring(response: 0.8, dampingFraction: 0.7, blendDuration: 0).delay(offsetDelay), value: offsetAmount)
.opacity(opacityAmount)
.animation(.default.delay(opacityDelay), value: opacityAmount)
}
}
extension View {
func fadeInWithOffsetAnimation(opacityAmount: Double, opacityDelay: Double, offsetAmount: Double, offsetDelay: Double) -> some View {
modifier(FadeInWithOffsetAnimation(opacityAmount: opacityAmount, opacityDelay: opacityDelay, offsetAmount: offsetAmount, offsetDelay: offsetDelay))
}
}
I use it by this way:
@State private var opacityAnimationAmount = 0.0
@State private var offsetAnimationAmount = 20.0
VStack {
....
VStack {
....
}
.fadeInWithOffsetAnimation(opacityAmount: opacityAnimationAmount, opacityDelay: 0.2, offsetAmount: offsetAnimationAmount, offsetDelay: 0.3)
....
}
.onAppear {
opacityAnimationAmount = 1.0
offsetAnimationAmount = 0.0
}
How should the struct properties be declared? I have seen code both ways. I know that whenever possible we should use let over var but I don't know if in this scenario it would be correct. Either way the code works as expected.
Thanks!