willdale / SwiftUICharts

A charts / plotting library for SwiftUI. Works on macOS, iOS, watchOS, and tvOS and has accessibility features built in.
MIT License
862 stars 107 forks source link

disable animations #121

Open bcyng opened 3 years ago

bcyng commented 3 years ago

is there any way to disable animations? currently I can do it by setting globalAnimation: Animation.linear(duration: 0.0) but this causes the chart to first be displayed as blank then the chart to suddenly appear after a few ms rather than be displayed completed upfront.

adding .animation(nil) modifier to GroupedBarChart works but this has been deprecated in ios15 so is not a long term solution. using the withAnimation and .animation(nil, value: 0) doesn't work.

testing this with grouped bar chart

willdale commented 3 years ago

From what I can see, .animation(nil) has been deprecated but .animation(nil, value: 0) has not.

Also there is a beta version of animation(_:).


Edit

Ticket opened

bcyng commented 3 years ago

.animation(nil, value: 0) doesnt have the same affect as .animation(nil). Ie doesn’t turn off the animation.

the beta https://developer.apple.com/documentation/swiftui/text/animation(_:) is now final in iOS15 - doesn’t have the same affect as .animation(nil). Ie doesn’t turn off the animation.

both of the above start display as the initial frame and then jump to the final frame. The result is the same as setting your .globalanimation modifier to globalAnimation: Animation.linear(duration: 0.0) in that it initially renders as blank and then the chart appears suddenly after rendering the view - after a few ms.

this needs to be done within the your function now - I think you’ll need to provide an option that doesn’t use the animation.

funkenstrahlen commented 2 years ago

I also would like to have the ability to disable the animations. Maybe the best solution is to be able to pass in a custom animation. Then you also add the ability to customise.

willdale commented 2 years ago

Custom animations are coming. Until then a solution is to add:

.transaction { transaction in
    transaction.animation = nil
}

This should disable animations. For more see https://www.avanderlee.com/swiftui/disable-animations-transactions/

iain-reid commented 2 years ago

I have these charts in a scroll view - when they are on-screen there is a noticeable lag as they scroll past (and animation doesn't play until you stop scrolling) - I've turned off the animations as I didn't actually want them, using the .transaction { method shown here - but the lag continues. Not 100% sure if it's the animations - but I can't think what else it might be... Is there any indication when we might be able to completely turn off animations?

bcyng commented 2 years ago

I have these charts in a scroll view - when they are on-screen there is a noticeable lag as they scroll past (and animation doesn't play until you stop scrolling) - I've turned off the animations as I didn't actually want them, using the .transaction { method shown here - but the lag continues. Not 100% sure if it's the animations - but I can't think what else it might be... Is there any indication when we might be able to completely turn off animations?

Put your data processing and fetch in a different thread - eg using Task.init() or something. It’ll be something like that blocking the main thread. Maybe u have those tasks in your onappear which would explain why it’s blocking as u scroll.

bcyng commented 2 years ago

the transaction solution seems to have broken in iOS16 b1 at least for pie charts. not sure if this is a bug in the beta or whether they have intentionally broken it.