HomeiOS DevelopmentHow do the parameters of interpolatingSpring() work

How do the parameters of interpolatingSpring() work


I’m attempting to construct a spring animation in SwiftUI that may carry over the rate of a pan gesture. For this I’m utilizing the following initializer.

static func interpolatingSpring(
    mass: Double = 1.0,
    stiffness: Double,
    damping: Double,
    initialVelocity: Double = 0.0
) -> Animation

Nevertheless, I do not actually perceive how the initialVelocity actually modifications the animation. I’ve made a small experimental Playground venture under.

import SwiftUI

struct ContentView: View {
    @State var pos: CGFloat = 40
    
    var physique: some View {
        Circle()
            .body(width: 50)
            .place(x: pos, y: 350)
            .onTapGesture {
                withAnimation(.interpolatingSpring(stiffness: 2500, damping: 600, initialVelocity: 60)) {
                    if pos == 40 {
                        pos = 340
                    } else {
                        pos = 40
                    }
                }
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

From experementing with completely different values I’ve discovered that if you wish to improve initialVelocity above a given treshold, irrespective of how a lot you improve the damping, it will not be sufficient to cease it from bouncing. If you wish to go together with a excessive initialVelocity, you even have to extend the stiffness and mess around with that as damping appears to be clamped(?) by some means to the stifness.

I might be significantly grateful for somebody that may shed some gentle on how these properties truly relate to at least one one other because the documenation could be very scarce. For instance, it goes on to say that initialVelocity needs to be within the vary [0,1], however that doesn’t appear to be the case…

Additionally, I might actually respect if somebody may assist work out the way to get a sound initialVelocity worth out of a pan gesture.

Thanks!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments