0

I am using a custom ScrollView as I need scroll started and ended events, reference: https://stackoverflow.com/a/73612181/2768604

Below is my own wrapper to use ScrollViewOffsetReader from above reference.

struct TrackableScrollView<Content>: View where Content: View {
    let axes: Axis.Set
    let showIndicators: Bool
    private let onScrollingStarted: () -> Void
    private let onScrollingFinished: () -> Void
    let content: Content

    public init(_ axes: Axis.Set = .vertical,
                showIndicators: Bool = true,
                @ViewBuilder content: () -> Content,
                onScrollingStarted: @escaping () -> Void = {},
                onScrollingFinished: @escaping () -> Void = {}) {
        self.axes = axes
        self.showIndicators = showIndicators
        self.content = content()
        self.onScrollingStarted = onScrollingStarted
        self.onScrollingFinished = onScrollingFinished
    }

    public var body: some View {
        ScrollView(axes, showsIndicators: showIndicators) {
            ZStack {
                ScrollViewOffsetReader(onScrollingStarted: {
                    onScrollingStarted()
                }, onScrollingFinished: {
                    onScrollingFinished()
                })
                LazyVStack {
                    self.content
                }
            }
        }
    }
}

Its working fine except for one thing. I have added LottieAnimationView on the very top inside the ScrollView, and when I scroll, the animation resets to its initial state and play again every time. I have set it to play just once.

So, it seems like ScrollView is unable to maintain content state properly.

Any idea on what could be the reason?

Edit:

I was able to figure out the cause of the issue and that is scroll start and end events. If I remove onScrollingStarted and onScrollingFinished callbacks, I dont get the issue. However, those callbacks are needed to perform some actions on the View.

Thanks

Paras Gorasiya
  • 1,295
  • 2
  • 13
  • 33

0 Answers0