I have a question about infinite paging in a ScrollView. In my app I have only 3 subviews in ScrollView. Each subview is loaded from xib file. Normally it looks like ABC in ScrollView. I wanted to make infinite paging so I added end caps and now it looks like CABCA. If the user is on the first C, it jumps to regular C and if user is on the last A, it jumps to regular A. Here is a code:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
if (scrollView.contentOffset.x == 0)
{
[scrollView scrollRectToVisible:CGRectMake
((scrollView.frame.size.width * 3), 0,
scrollView.frame.size.width,
scrollView.frame.size.height) animated:NO];
}
else if (scrollView.contentOffset.x == scrollView.frame.size.width * 4)
{
[scrollView scrollRectToVisible:CGRectMake
(scrollView.frame.size.width, 0,
scrollView.frame.size.width,
scrollView.frame.size.height) animated:NO];
}
}
It works perfectly now. But I have ViewController for each subview and this is how I am adding them to ScrollView:
subViewController1 = [[SubViewController1 alloc] initWithNibName:@"SubView" bundle:nil];
subViewController1.view.frame =
CGRectMake(0, 0, scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView addSubview:subViewController1.view];
Problem is that there is one duplicate of A and C view so now I have 5 controllers instead of 3. And If I want to add something into a A view, I have to add it also into duplicate of A view.
Is there a way how to control view A and duplicate of A with one controller so I don't have to create two instances of one controller? Thank you.
Better still, you don't need to have duplicate view A's and duplicate view C's, you just move them around in
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
while manipulating thecontentOffset
.Setup: probably very similar to how you already do it.
Set your
UIScrollView
to havecontentSize
3 times it's bounds width. Make sure paging is turned on and bouncing off.Add your ABC subviews to the UIScrollView from left to right.
Also have an array in your ViewController called
_contentViews
that contains yourUIViews
ABC.Then implement this which will reset the content offset and move your subviews around at the same time when the scrollview reaches the edges: