0
votes

I'm using iCarousel in my Swift+Sprite Kit game, where the user has unlocked and locked items.

The locked items would display some info like current user coins, and some text that says "UNLOCK THIS FOR: X amount of coins", if the carousel item isn't locked, it won't display any of that information, just a button that says "Select".

Now, I got that working, but it will only work when i start swiping the items and not the first time the carousel shows up.

To make that work, I do it inside carouselCurrentItemIndexDidChange() method. And that obviously changes when I swipe.

How can I set the current carousel index at start so then I can force to show or not show the information depending if the item is locked or unlocked and not only when I start swiping?

To add more information about this, imagine that the user selected the item at index 4, i save that index locally. So if the user closes and opens the game, the carousel should begin at 4 not 0.

Thanks in advance.

1
You should set it up in viewForItemAtIndex, not the delegate method you are usingPaulw11
which one??? this func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView { ? but that isn't only called when you create the carousel and never called again?msqar
No, that method is called each time a view needs to be displayed. It works like cellForRowAtIndexPath; when views scroll of screen they are discarded and retrieved from this method when they come back onto the screen. If there is a change to the underlying data that affects a view you can call reloadItemAtIndex and then the carousel view will call viewForItemAtIndex to get the updated view.Paulw11
I checked yesterday if i had a method called viewForItemAtIndex and i couldn't find it in iCarousel. Is it possible?msqar
When i said viewForItemAtIndex I was just saving typing. The full name is the method you said, viewForItemAtIndex: reusingView:. To start from item 4, you would call scrollToItemAtIndex:animated: probably somewhere like viewWillAppearPaulw11

1 Answers

1
votes

You should set up your carousel views in viewForItemAtIndex: reusingView:. The view returned by this method should represent the current state of that carousel item; so locked or "Select" as appropriate.

If the state of an item changes then you can call reloadItemAtIndex: to have iCarousel request an updated view for that item by calling viewForItemAtIndex: reusingView:.

You can scroll the carousel to a particular item by calling scrollToItemAtIndex:animated: