UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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

}

`

Screenshot

2      

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

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.