gh123man/SwiftUI-LazyPager

Add ability to animate page change when page index is changed programmatically

Closed this issue · 11 comments

Would it be possible to animate the page change when the index is changed programmatically? Right now if I change the index, the page updates without animation, however there are cases here I would like to animate the page changing.

Do you envision animating only previous and next page? Or skipping n pages. For example, going form 0 -> 5 and having it scroll past 4 images?

I would envision being able to skip n pages. In my head I'm picturing using the LazyPager for a photo viewer, but also having a custom horizontal ScrollView where the user can display the thumbnail photos which the user can click on a thumbnail to view the photo.

Makes sense!
here's what I got so far:

Screen.Recording.2024-11-25.at.9.37.49.PM1.mov

It works for arbitrary offsets. However if you skip > preloadAmount, it will load the next set of images first before animating, which is hardly noticeable unless you slow the animation down.

Let me know if this is good enough for your use case.
The problem with animating truly n images is if n = 10000 it would be computationally expensive and the animation would likely lag or freeze. With the current implementation you can increase preloadAmount to smooth it out if you want.

That does look like it will work for what I need. It does make sense that you won't want to load all of the images if n = 1000.

Awesome. Thanks for the feedback. ill clean this up and put it in the next release!

Released in 1.1.3

Full example here

You can now use withAnimation when setting the page and LazyPager will animate the transition.

Thank you, but one small item is with the updated build there is a blank space below the LazyPager where there wasn't before.

Darn. This is likely from me trying to fix content inset adjustments. .

Are you by any chance using .ignoresSafeArea()?

I am using .ignoresSafeArea(), but noticed when I also remove that from the parent view it updates the size accordingly. so I'll check more of my code and see what can be the issue.

Edit:
The adding of contentInsetAdjustmentBehavior = .scrollableAxes did cause the effect I was seeing.

You are right.
Ill continue to investigate but I released 1.1.4 with the inset changes removed.
For now the view doesn't work correctly when the device is in landscape.

I figured it out. Released in 1.1.5.
Thanks again