iOS:ScrollView无限分页-重复的端盖 [英] iOS: ScrollView infinite paging - Duplicate end caps
问题描述
我对ScrollView中的无限分页有疑问.在我的应用程序中,ScrollView中只有3个子视图.每个子视图都是从xib文件加载的.通常,它看起来像ScrollView中的ABC.我想进行无限分页,所以我添加了端盖,现在看起来像CABCA.如果用户在第一个C上,则跳转到常规C;如果用户在最后一个A上,则跳转到常规A.这是代码:
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];
}
}
它现在可以完美运行了.但是我为每个子视图都有ViewController,这就是我将它们添加到ScrollView的方式:
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];
问题是A和C视图有一个副本,所以现在我有5个控制器,而不是3个.如果要在A视图中添加某些内容,我也必须将其添加到A视图的副本中.
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.
有没有一种方法可以使用一个控制器控制视图A和A的副本,因此我不必创建一个控制器的两个实例?谢谢.
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.
推荐答案
更好的是,您不需要具有重复的视图A和视图C,只需在操作contentOffset
的同时将它们在- (void)scrollViewDidScroll:(UIScrollView *)scrollView
中移动即可.
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 the contentOffset
.
设置:可能与您已经完成的操作非常相似.
Setup: probably very similar to how you already do it.
将UIScrollView
设置为contentSize
是其边界宽度的3倍.确保分页功能已打开并关闭.
Set your UIScrollView
to have contentSize
3 times it's bounds width. Make sure paging is turned on and bouncing off.
从左到右将ABC子视图添加到UIScrollView.
Add your ABC subviews to the UIScrollView from left to right.
您的ViewController中还有一个名为_contentViews
的数组
包含您的UIViews
ABC.
Also have an array in your ViewController called _contentViews
that contains your UIViews
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:
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if(scrollView.contentOffset.x == 0) {
CGPoint newOffset = CGPointMake(scrollView.bounds.size.width+scrollView.contentOffset.x, scrollView.contentOffset.y);
[scrollView setContentOffset:newOffset];
[self rotateViewsRight];
}
else if(scrollView.contentOffset.x == scrollView.bounds.size.width*2) {
CGPoint newOffset = CGPointMake(scrollView.contentOffset.x-scrollView.bounds.size.width, scrollView.contentOffset.y);
[scrollView setContentOffset:newOffset];
[self rotateViewsLeft];
}
}
-(void)rotateViewsRight {
UIView *endView = [_contentViews lastObject];
[_contentViews removeLastObject];
[_contentViews insertObject:endView atIndex:0];
[self setContentViewFrames];
}
-(void)rotateViewsLeft {
UIView *endView = _contentViews[0];
[_contentViews removeObjectAtIndex:0];
[_contentViews addObject:endView];
[self setContentViewFrames];
}
-(void) setContentViewFrames {
for(int i = 0; i < 3; i++) {
UIView * view = _contentViews[i];
[view setFrame:CGRectMake(self.view.bounds.size.width*i, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
}
}
这篇关于iOS:ScrollView无限分页-重复的端盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!