iOS:ScrollView 无限分页 - 重复的结束帽 [英] iOS: ScrollView infinite paging - Duplicate end caps

查看:22
本文介绍了iOS:ScrollView 无限分页 - 重复的结束帽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 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,你只需在 - (void)scrollViewDidScroll 中移动它们:(UIScrollView *)scrollView 同时操作 contentOffset.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆