【问题标题】:UIScrollView paging from center to left and rightUIScrollView 从中心到左右分页
【发布时间】:2016-04-29 14:53:08
【问题描述】:

我做了一个水平的 UIScrollView,启用了分页。这使我可以“滑动”某个视图。我遇到的问题是滚动视图位于左侧。我只能向左滚动(阅读:滑动)我的特定视图。我也想让它向右滑动,所以两边。我认为这是将 UIScrollView 居中的问题。我尝试了很多东西,但都没有成功。

这是我的代码(vertScroll 是水平滚动视图):

    - (void)viewDidLoad {
    [super viewDidLoad];


     vertScroll.contentSize = CGSizeMake(3*375, 232);
    vertScroll.frame = CGRectMake(0, 0, 375, 232);
    vertScroll.pagingEnabled = YES;

    vertScroll.scrollEnabled = YES;
    vertScroll.showsHorizontalScrollIndicator = YES;

    vertScroll.contentOffset = CGPointMake(375, 0);


    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];


    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:255.0 green:125.0 blue:71.0 alpha:1.0]];
    self.navigationController.navigationBar.topItem.title = @"blabla";

    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor],
       NSFontAttributeName:[UIFont fontWithName:@"StoneSansBold" size:21]}];

    self.scrollView.showsVerticalScrollIndicator=YES;
    self.scrollView.scrollEnabled=YES;
    self.scrollView.userInteractionEnabled=YES;

    self.scrollView.contentSize = CGSizeMake(320, 800);

    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

    // add effect to an effect view
    self.effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
    self.effectView.alpha = 0.9;
    self.effectView.frame = CGRectMake(self.coverImage.frame.origin.x, self.coverImage.frame.origin.y+108, self.coverImage.frame.size.width, 34);

    // add the effect view to the image view
[self.coverImage addSubview:self.effectView];

   [self.cellView addSubview:self.seismo];


    self.seismo = [[UIImageView alloc]initWithFrame:CGRectMake(2, self.coverImage.frame.size.height/2, 40, 40)];
    self.seismo.animationImages = [NSArray arrayWithObjects:
                                         [UIImage imageNamed:@"tmp-0.gif"],
                                         [UIImage imageNamed:@"tmp-1.gif"],
                                         [UIImage imageNamed:@"tmp-2.gif"],
                                         [UIImage imageNamed:@"tmp-3.gif"],[UIImage imageNamed:@"tmp-4.gif"], nil];
    self.seismo.animationDuration = 1.0f;
    self.seismo.animationRepeatCount = 0;
    [self.seismo startAnimating];
    [self.cellView addSubview:self.seismo];
    self.seismo.alpha = 0.0;

    [self.cellView setFrame:CGRectMake(375, 0, 375, 232)];




           // Do any additional setup after loading the view, typically from a nib.
}

插图说明:

【问题讨论】:

    标签: ios objective-c uiscrollview


    【解决方案1】:

    您应该制作 3 页,因此 contentSize 应该是一页大小的 3 倍,据我所知,这是正在发生的。

    现在确保用户始终从第 2 页(中心页面)开始,通过将 contentOffset 设置为 x 坐标的滚动视图宽度的 1 倍和 y 坐标的 0 来执行此操作。

    之后,用户应该可以左右滚动了。

    PS:通过查看您的代码,您似乎假设每个人都使用 iPhone 6(宽度为 375pt),您可能想要使用计算出的宽度(通过使用 self.view.frame.size.width 或相关的东西)

    【讨论】:

    • 做到了,但“某些视图”现在位于滚动视图的左侧(所以第 1 页)。如何在 scrollView 中居中(或放在第 2 页)?
    • 只要确保框架设置正确,所以对于第 1 页,框架将是:CGRectMake(0,0,width,height),对于第 2 页(中心),它将是:CGRectMake(width,0,width,height),对于第 3 页它将是CGRectMake(width * 2,0,width,height)
    • 也这样做了,但它仍然留在滚动视图中,所以在第 1 页。
    • 你能把你用来填充scrollView的代码贴出来吗?
    • "cellView" = "某些视图"