类似头条上下翻页效果 oc

效果图:

  • 创建UIScrollView

    infoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(imgV.viewX+imgV.viewWidth+Blank*1.5, 0, ViewWeight-(imgV.viewX+imgV.viewWidth+Blank*1.5), CellHeight)];

    infoScrollView.bounces = NO;

    infoScrollView.showsVerticalScrollIndicator = NO;

    infoScrollView.showsHorizontalScrollIndicator = NO;

    infoScrollView.pagingEnabled = YES;

    infoScrollView.delegate = self;

    infoScrollView.contentSize = CGSizeMake(0, CellHeight*[infoAry count]+1);

    [view addSubview:infoScrollView];

  •    创建UIScrollView每一页的内容

    for (int i=0; i<[infoAry count]+1; i++) {

        NSString *str;

        if (i == [infoAry count]) {

            str = infoAry[0];//每当内容循环完就重置,从第一个再开始循环

        } else {

            str = infoAry[i];

        }

        UILabel *l = [self setupLabel:CGRectMake(0, CellHeight*i, ViewWeight, CellHeight) withText:str withSuperView:infoScrollView];

        l.textColor = ErrorAwardColor;

        l.font = LittelFont;

    }

  • 创建定时器,自动翻页

    infoPageIndex = 0;//第几页,确定数据展示的位置

    [NSTimer scheduledTimerWithTimeInterval:2.0 repeats:YES block:^(NSTimer * _Nonnull timer) {

        infoPageIndex ++;

        [infoScrollView setContentOffset:CGPointMake(0, infoScrollView.viewHeight*infoPageIndex) animated:YES];

    }];

  • UIScrollView展示完最后一页,重置页面位置到第一页

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    scrollView.panGestureRecognizer.enabled = NO;

    if (infoPageIndex == [infoAry count]+1) {

        infoPageIndex = 0;

        [scrollView setContentOffset:CGPointMake(0, 0) animated:NO];

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_42012181/article/details/86590922