I'm working on a status board for our kitchen, and I wanted to animate a train on it. The code all works, the train is set to animate from an x offset of negative screenwidth to positive screen with with a duration of 30 seconds. The timing is not important, it doesn't represent anything, it's only there to make people smile as it moves across.
the problem is that the train stutters about every 9/10s of a second. so it's not a smooth animation across the screen. The app does display a bit of info from a bunch of sources, and that all works just fine. 3 Calendars, 2 sections with weather info from the web, a secton that gets quotes from the web, and a section based on my Clockinator (check it out at the app store!). I've reduced the timers all of the other sections so that they are only happening every 1 minute (the calendar), every 10 minutes (the weather) and every 20 minutes (online quotes), but no matter what I set them to, the train stutters as it runs across the screen.
When i made another project that had only the TrainView() file and the external settings class that I used, the train is smooth like butter. So I'm pretty sure something in the rest of my code is interfering and making the stutter happen.
My question is this: is it possible to spawn the animation of the train on an asynchronus thread or something so that it runs regardless of what the rest of the app is doing? It really doesn't matter if it's running on time (no Amtrak or British Rail jokes, please!), it only matters that it's not stuttering. Any ideas?
`
struct TrainView: View {
@ObservedObject var settings: Settings
let screenWidth: CGFloat = 1080
var xx: CGFloat {
if settings.clockMinute % 2 == 0 {
return screenWidth * 2
} else {
return -screenWidth
}
}
var whichTrain: String {
if settings.clockMinute % 2 == 0 {
return "circusTrain"
} else {
return "circusTrainReverse"
}
}
var body: some View {
Image(whichTrain)
.offset(x: xx, y: 0)
.animation(
Animation.linear(duration: 30),
value: xx
)
}
}
`