0

Why isn't Canvas getting updates on animated @State variables?

For example when I animate the rotationAngle as seen here:

@State private var rotationAngle: Double = 90.0

HStack {
    Canvas() {
        print(rotationAngle) //prints 90.0 at the start, 0.0 when the animation starts
    }
}
.onTapGesture {
    withAnimation(.easeIn(duration: 2)) {
        rotationAngle = 0.0
    }
}

The same animation works fine in any other case at the same time. All views containing reference to the rotationAngle get updated, except Canvas.

Is there any other solution, can I access the value of rotationAngle while it's being animated?

h4master
  • 31
  • 3
  • https://www.hackingwithswift.com/quick-start/swiftui/how-to-create-custom-animated-drawings-with-timelineview-and-canvas – SPatel Jun 13 '23 at 16:25
  • Wrap Canvas into `TimelineView(.animation) { timeline in }` – SPatel Jun 13 '23 at 16:25
  • Canvas body is none declarative so its work like normal uikit code – SPatel Jun 13 '23 at 16:27
  • Already animating with timeline, the idea was to access the variable from inside the Canvas, but whenever I read it - it is the non-transitional value; in this case 90.0 or 0.0. That's why I'm trying to figure out if it's possible to track the value "in transition" in any way. – h4master Jun 13 '23 at 16:42
  • I mean interpolated values. @SPatel – h4master Jun 13 '23 at 16:58
  • When you implement Canvas, your are responsible for the interpolated values yourself. It is the a related idea as the `Animatable` protocol – Yrb Jun 13 '23 at 22:35

0 Answers0