淘先锋技术网

首页 1 2 3 4 5 6 7

UIScrollView滚动分页步骤

1.用storyboard创建UIScrollView和Page View Controller

1
- 设置UIScrollView的代理
- 在控制器中拿到这两个控件


2.创建UIImageView,添加所要展示的图片

    for (int i = ; i < count; i ++) {
        //创建
        UIImageView * imageView = [[UIImageView alloc] init];
        NSString * name = [NSString stringWithFormat:@"img_0%d",i];
        imageView.image = [UIImage imageNamed:name];
        //frame
        imageView.frame = CGRectMake(i * w, , w, h);
        [self.scrollView addSubview:imageView];
    }

3.设置scrollView和分页效果

    //设置内容大小
    CGFloat contentW = count * w;
    self.scrollView.contentSize = CGSizeMake(contentW, );
    //去掉水平滚动条
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //开启分页功能
    self.scrollView.pagingEnabled = YES;
    //总页数
    self.pageCon.numberOfPages = count;

4.执行以下方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    //小数四舍五入为整数:(int)(小数+)
    int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + );
    self.pageCon.currentPage = page;
}