JWAutumn / ACarousel

A carousel view for SwiftUI | SwiftUI 旋转木马效果
MIT License
406 stars 81 forks source link

Any way to run a closure when an item is the primary one shown? #2

Closed t9mike closed 3 years ago

t9mike commented 3 years ago

Cool view. Thanks for sharing!

I would like to use ACarousel to select an item, but the button is not an item view, it is a fixed button.

So running a closure when the primary item is moved would be helpful.

t9mike commented 3 years ago

I was able to make viewModel.activeIndex public and then access this in my button's handler. This seems to return the right offscreen index so I did:

                    var i = carousel.viewModel.activeIndex - 1
                    if (i < 0)
                    {
                        i = items.count-1
                    }

To map this back to my data.

t9mike commented 3 years ago

I somehow missed the index binding option. Apologies.