Jordan
Jordan

Reputation: 221

iCarousel infinite scroll and index reset

I am working with a carousel in iOS and I am trying to recreate the scrolling banners (featured images) that are present in the iOS app store and iTunes. I have a carousel variable and I would like to get that to scroll exactly like on the apps mentioned above. If you are not familiar with how they scroll, they slide the image and then stop for a period of time.

I have tried carousel.scrollByNumberOfItems(5, duration: 20) which just creates a constant scroll until it is finished. I have played around with different scrolling functions and cannot seem to achieve the desired effect. Again, I am looking for the image to slide, then pause, then slide the next, etc.

My second question is, when the images slide to the end of my images, how can I reset the index if it is passed in as a constant? In other words I am trying to achieve an infinite scrolling effect that will just keep cycling through my images one at a time, similar to the app store.

I am working in swift, but welcome objective c examples as well.

Any help is appreciated! Thanks!

Upvotes: 1

Views: 1582

Answers (1)

Paulw11
Paulw11

Reputation: 115031

You can do this pretty easily with an NSTimer to initiate the scrolling.

For example this will scroll every 20 seconds, taking 5 seconds to scroll -

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.scrollTimer=[NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(scrollCarousel) userInfo:nil repeats:YES];
}

-(void) scrollCarousel {
   NSInteger newIndex=self.carousel.currentItemIndex+1;
   if (newIndex > self.carousel.numberOfItems) {
      newIndex=0;
   }

   [self.carousel scrollToItemAtIndex:newIndex duration:5];
}

as long as your carousel is set to wrap then this will give you what you want.

Upvotes: 4

Related Questions