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

Changing duration for Slider animation doesn't make any difference

Forums > SwiftUI

(xposted from SO)

I'm trying to animate Slider smoothly. My original problem was animating it for 1 second every second for a small value (building an audio player scrubber), but the animation was a split second instead of a full second.

To isolate a problem, I built a playground where I'm trying to animate Slider change from 0 to maxValue for maxValue number of seconds. However, whatever maxValue is, the animation happens in a fraction of a second.

Here is the code:

struct SliderTest: View {
    @State private var sliderValue = 0.0
    let maxValue = 30.0

    var body: some View {
        VStack(spacing: 10) {
            Slider(value: $sliderValue, in: 0...maxValue)
            Button("Animate Slider") {
                withAnimation(.linear(duration: maxValue-sliderValue)) {
                    sliderValue = maxValue
                }
            }
            Button("Reset to Random") {
                sliderValue = Double.random(in: 0..<maxValue)
            }
            Button("Reset") {
                sliderValue = 0
            }
        }
    }
}

The code with a preview for Swift Playgrounds.

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot 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.