NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Stuttering Animations

Forums > SwiftUI

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 {
        .offset(x: xx, y: 0)
            Animation.linear(duration: 30),
            value: xx





Firstly Wafffle, fried eggs, jelly and butter sounds great 😊

You have to do UI work on the main thread, so what I would do is comment out all but the train then add each one back in to see when it start stuttering. You could also look at pushing the getting data eg, calendar, weather to a background thread, just make sure you do any update for UI on main thread


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.