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

SOLVED: Animating SKUniforms?

Forums > Swift


Hey guys!

I'm currently trying to animate an SKUniform, is there any way to achieve this? I tried to put the value in a @State var and call the changes in an withAnimation but that didn't do the trick, the shader just "pops" to the new value.

func zoomingOut() { withAnimation { shaderScaling = 1.0 self.shaderUniform.floatValue = shaderScaling } }

This this my code, more or less. If someone has an idea on how to animate value changes here, I'd really appreciate it!

Best regards LilaQ



I wrote my own Animation class for this in the end. In case someone stumbles upon the same issue:

class AnimationHelper {

    static func easeInOutSine(_ x: Double)->Double {
        return -(cos(Double.pi * x) - 1) / 2;

    static func easeInOutCubic(_ x: Double)->Double {
        return x < 0.5 ? 4 * x * x * x : 1 - pow(-2 * x + 2, 3) / 2;

    enum EASING {
        case none, easeInOut, easeInOutCubic

    static func animate<T:BinaryFloatingPoint>(startValue: T, targetValue: T, targetTime: Double = 1.0, easing: EASING = .none, onFractionalUpdate: @escaping (T)->()) {
        let resolution = 0.01
        var currentTime = 0.0
        _ = Timer.scheduledTimer(withTimeInterval: resolution, repeats: true) { timer in
            currentTime += resolution

            if currentTime >= targetTime {

            //  TODO:   We could add some easing funcionality here
            var timeFraction = currentTime / targetTime
            switch easing {
            case .easeInOut:
                timeFraction = easeInOutSine(timeFraction)
            case .easeInOutCubic:
                timeFraction = easeInOutCubic(timeFraction)
            default: ()

            let valueFraction = targetValue - startValue
            onFractionalUpdate(T(timeFraction) * T(valueFraction) + startValue)





Please mark your own answer as "Solved"


Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.