Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make uiscrollview infinite in iOS?

I want to scroll like that 1 2 3 1 2 3

I have some buttons suppose 10 which i want to show on endless scroll.

 numbercolors=[[NSMutableArray alloc] init];

 //total count of array is  49 

 numbercolors = [NSMutableArray arrayWithObjects:@"25",@"26",@"27",@"28",@"29",@"31",@"32",@"33",@"34",@"35", @"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35", @"0",@"1",@"2",@"3",nil];

  int x=2500;

for (NSInteger index = 0; index < [numbercolors count]; index++)
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(x ,0,29.0,77.0);

    button.tag = index;

    [button setTitle:[numbercolors objectAtIndex:index] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(didTapButton:) 

    forControlEvents:UIControlEventTouchUpInside];

    [coloringScroll addSubview:button];

    x=x+70+29;
}
 [coloringScroll setContentSize:CGSizeMake(5000+ (29+70)*[numbercolors count], 1)];

 [coloringScroll setContentOffset:CGPointMake(2500+(29+70)*11, 0)];

This is my code for make butttons on scrollview.

How can I set in - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender this method for infinite scroll.

like image 244
virantporwal Avatar asked Oct 09 '13 09:10

virantporwal


3 Answers

Just need to do set setContentOffset count

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x > 2500+(29+70)*4 + ((29+70)*36)) {
        [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x-((29+70)*36),  0)];  
    }
    if (scrollView.contentOffset.x < 2500+(29+70)*4){
       [scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x+((29+70)*36), 0)];
    }
}
like image 179
virantporwal Avatar answered Oct 02 '22 21:10

virantporwal


Main idea is to reposition your scrollview back to some constant value, when it has been scrolled to some value and then auto-reposition your "1" , "2", "3" items correctly.

For example - You would have scrollview with contentwidth 5000. By default You set it to position 2500.

then You simply check in - (void)scrollViewDidCScroll:(UIScrollView *)scrollview - if scrollview.contentoffset.x > 3500 - then decrease it's position to scrollview.contentoffset.x -=1000;

and the same about other side. This will result in infinite scrolling.

But content will not follow. So - You would need to implement extra content offset value checking to reorder and reposition correctly "1", "2", "3", items.

I usually use 3 elements and then dynamically preload necessary gallery image for them.

If You don't want to re-invent the "wheel" , check out these solutions:

https://www.cocoacontrols.com/controls/dmcircularscrollview

https://www.cocoacontrols.com/controls/infinitescrollview

https://www.cocoacontrols.com/controls/iainfinitegridview

like image 42
Guntis Treulands Avatar answered Oct 02 '22 22:10

Guntis Treulands


You can check out this solution based on Apple StreetScroller iOS sample code:

https://github.com/gblancogarcia/GBInfiniteScrollView

I hope it helps!

like image 31
Gerardo Blanco García Avatar answered Oct 02 '22 23:10

Gerardo Blanco García