I am struggling to create an animation of a changing date value. A simplified example is below. It is intended to update the Text view with changing values as the date is updated from its original value to its new value. Help would be most appreciated!
struct DateAnimationView: View {
@State var date = Date()
typealias AnimatableData = Date
var animatableData: Date{
get{date}
set{date = newValue}
}
var body: some View {
VStack{
Text(date, style: .time)
.padding()
Button(action:{
withAnimation(.linear(duration: 3.0)){
date.addTimeInterval(60 * 60 * 3)
}
}){
Text("Add 3 hours")
}
.padding()
Spacer()
}
}
}
Another attempt which also fails:
struct InnerView: View {
var date: Date
var interval: TimeInterval
typealias AnimatableData = TimeInterval
var animatableData: TimeInterval{
get{interval}
set{interval = newValue}
}
var body: some View{
Text(date.addingTimeInterval(interval), style: .time)
}
}
struct DateAnimationView: View{
@State var isOn: Bool = false
var body: some View{
VStack{
InnerView(date: Date(), interval: isOn ? 60*60*3 : 0)
Button(action:{
isOn.toggle()
}){
Text("Go")
}
}
}
}