fermoya / SwiftUIPager

Native Pager in SwiftUI
MIT License
1.27k stars 166 forks source link

[FEAT] An scrolling animation when changing page programmaticaly #290

Closed pblanchetteNventive closed 2 years ago

pblanchetteNventive commented 2 years ago

Maybe I'm not getting the animation settings right, but I think that the transition from one page to another could be animated in the same way a dragging gesture does.

Maybe adding a modifier to opt-in to perform swipe animation on page change.

fermoya commented 2 years ago

Hi @pblanchetteNventive , can I see your code to change the page? Are you calling it inside a withAnimation bock and using the Page.update method? Check out the examples I provide in the repo and you'll see an example of how to do this

pblanchetteNventive commented 2 years ago

Here's how i'm using it

struct ContentView: View {
    var body: some View {
        VStack {
            Pager(
                page: page,
                data: array,
                id: \.id,
                content: { index in
                    VStack{
                        Text("Item Content")
                    }
                    .background(index.color)
            })
            .itemAspectRatio(1)
            .draggingAnimation(.steep(duration: 1.0))
            .pagingPriority(.simultaneous)
            .loopPages()
            .onReceive(timer, perform: {_ in
                withAnimation {
                    page.update(.next)
                }
            })
            .frame(width: 400, height: 200, alignment: .center)
            .background(Color.gray.opacity(0.2))
        }    
    }
}
fermoya commented 2 years ago

What's timer and is it being updated in the main thread? Check out ColorExampleView to see how it works

pblanchetteNventive commented 2 years ago

Thanks, I checked your code in the ColorExampleView and managed to make it work !