0

I have a scrollview which automatically pass an image to another. It has 7 images, I want to that when you get to the seventh image pass me a first in the same way as the rest of transitions and not moving quickly to the first position.

It is what is commonly called infinite scroll. Any help is appreciated.

- (void)viewDidLoad
{

for (int i = 1; i < 8 ; i++) {

    UIImageView *imagen = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"C%d.png",i]]];

    imagen.frame = CGRectMake((i-1)*580,35, 580, 300);

    [_scroller addSubview:imagen];

    _scroller.indicatorStyle = UIScrollViewIndicatorStyleWhite;
}


_scroller.delegate = self;
_scroller.contentSize = CGSizeMake(580*7, 300);
_scroller.pagingEnabled = YES;

if (scrollingTimer == nil)
{
    scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:6
                                                      target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];
}
}

-(void)scrollToPage:(NSInteger)aPage{
float myPageWidth = [_scroller frame].size.width;

[_scroller setContentOffset:CGPointMake (aPage * myPageWidth, 0) animated:YES];

}

-(void)scrollPages{
[self scrollToPage:currentPage%7];
currentPage++;
}
Steph Sharp
  • 11,462
  • 5
  • 44
  • 81
user1908661
  • 89
  • 3
  • 9

1 Answers1

1

One way to implement infinite scolling in a paging UIScrollView is to put a duplicate of the first page at the end of the scroll view (and vice versa, if you want infinite scrolling in both directions).

This article uses a UICollectionView instead, but the concept is the same for a paging UIScrollView. When the user scrolls to the duplicate of the first page at the end of the scroll view, set the content offset back to the actual first page (without animation, so the user doesn't know it's happened).

Here's some other resources I found useful:

Community
  • 1
  • 1
Steph Sharp
  • 11,462
  • 5
  • 44
  • 81