1

I'm trying to mimic ScrollView behaviour in my own custom component.

The thing I'm having trouble with is scroll animation. As far I understand I can use predictedEndTranslation object to get the predicted position of the drag gesture. But I don't know which easing function I should use to mimic default ScrollView easing.

Here is my code

struct ViewContent: View {
    @State var offsetY: CGFloat = 0

    var body: some View {
        View {

        }
        .offset(y: offsetY)
        .gesture(
          DragGesture()
            .onChange { value in
                offsetY = value.translation.height
            }
            .onEnded { value in 
                let nextScrollPosition = value.predictedEndTranslation.height

                withAnimation(???) { // What easing to use?
                    offsetY = nextScrollPosition
                }
            }
        )
    }
}
1

1 Answer 1

1

I would use .spring() as this preset animation will get you the closes to the generic scrollview animation.

withAnimation(.spring()) { // User .spring()
    offsetY = nextScrollPosition
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I tried to use this .spring() but wasn't sure if this easing is the best one
Can I ask why you cannot use a regular scrollview?
I would like to implement snap interval (stop at the closest row or column, not at the middle or somewhere else) which is not supported in default ScrollView.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.