I found this web page:
https://swiftui-lab.com/swiftui-animations-part3/
Specifically, the end section where it talks about Animatable Views. Quote:
If we want the body of the view to be recomputed for every frame of the animation, then we adopt the Animatable protocol, by adding Animatable, and the animatableData property.
So I've replaced the CustomCanvas above with this:
struct CustomCanvas: View, Animatable
{
var fg_colour: Color.Resolved;
var animatableData: Color.Resolved {
get { fg_colour }
get { fg_colour = newValue }
};
var body: some View {
Canvas { context, size in
context.draw(image: Image(name: "foo"), toFitRect: some_rectangle);
context.draw(text: Text("foo"), toFitRect: another_rectangle);
}
.foregroundStyle(fg_colour)
}
};
and now I pass fg_colour from the MainView:
struct MainView: View
{
@Environment(\.self) var environment;
var body: some View {
PhaseAnimator([false,true], trigger: foo) { flash in
ZStack {
Capsule()
.foregroundStyle(flash ? .red : .green)
CustomCanvas(fg_colour: (flash ? .black : .white) .resolved(in: environment)
}
} animation: { flash in
return .linear(duration: 0.5);
}
};
(Note Color isn't animatable, but Color.Resolved is; I need some extra stuff to make that work.)
But this still doesn't work. The Canvas closure is still only called twice per update, not continuously to update with intermediate colours.
Advice would be much appreciated.