UIScrollView从中心向左和向右分页

钢笔

我制作了一个水平UIScrollView,并启用了分页。这使我可以“擦除”某个视图。我的问题是,滚动视图位于左侧。我只能向左滚动(阅读:轻扫)我的特定视图。我也想让它向右滑动,所以两边都滑动。我认为将UIScrollView居中存在问题。我尝试了很多事情,但都没有解决。

这是我的代码(vertScroll是水平scrollView):

    - (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.
}

带有插图的说明: 在此处输入图片说明

埃里克·特万(Erik Terwan)

您应该制作3页,所以据我所知,contentSize应该是一页大小的3倍。

现在,确保用户始终从页面2(中心页面)开始,方法是将contentOffset设置为x坐标的scrollView宽度的1x和y坐标的0。

在那之后,用户应该能够向左和向右滚动。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章