// 定义成员变量
{
UIScrollView *scrollView;
UIPageControl *page;
}
设置滚动视图的协议:<UIScrollViewDelegate>
设置滚动视图
// 创建滚动视图
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180)];
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 7, 180);
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor redColor];
scrollView.bounces = NO;
scrollView.delegate = self;
[cell addSubview:scrollView];
设置图片
// 创建图片数组
NSArray *imgArr = @[@"0.jpg",@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg"];
// for 循环加入图片
for (int i = 0; i < imgArr.count; i ++) {
// 创建图片展示框
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 180)];
imgView.image = [UIImage imageNamed:imgArr[i]];
[scrollView addSubview:imgView];
}
设置小圆点
page = [[UIPageControl alloc] initWithFrame:CGRectMake((self.view.frame.size.width-200)/2, 150, 100, 30)];
page.numberOfPages = imgArr.count;
page.currentPageIndicatorTintColor = [UIColor redColor];
page.pageIndicatorTintColor = [UIColor whiteColor];
[cell addSubview:page];
实现滚动视图的协议方法 -- 图片与小圆点关联
// 滚动视图的协议方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
page.currentPage = scrollView.contentOffset.x/self.view.frame.size.width;
}
网友评论