效果图 :
1、UIScrollView :滚动视图
滚动视图 多个页面实现的原理 滚动视图位置不变 内容的位置发生改变
1⃣️分页查看图片 2⃣️查看大图片 3⃣️当内容过多,需要在一个页面上显示的时候(如注册、修改个人信息等) 4⃣️当不希望感觉咱们的界面是一张图片的时候 contentSize height+1 or width+1
查看大图片
2、初始化UIScrollView,设置背景颜色
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
3、设置滚动视图的尺寸
NSString *path = [[NSBundle mainBundle]pathForResource:@"大图" ofType:@"png"];
UIImage *bgimage = [UIImage imageWithContentsOfFile:path];
scrollView.contentSize = bgimage.size;
4、设置滚动视图缩放的最小倍数和最大倍数
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 20;
[self.view addSubview:scrollView];
5、在滚动视图上添加视图
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, bgimage.size.width, bgimage.size.height)];
imageView.image = bgimage;
[scrollView addSubview:imageView];
6、设置需要在滚动视图中放大的视图
在。h文件里添加代理,在初始化UIScrollView的时候挂上代理
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
当内容过多,需要一个页面显示
7、初始化,挂上代理,设置背景颜色,设置滚动视图的尺寸
UIScrollView *myscrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
myscrollView.delegate = self;
myscrollView.backgroundColor = [UIColor grayColor];
myscrollView.contentSize = CGSizeMake(0, CGRectGetHeight(self.view.frame)+10);
8、屏蔽横向纵向滚动条
myscrollView.showsVerticalScrollIndicator = NO;
myscrollView.showsHorizontalScrollIndicator = NO;
9、设置滚动视图 是否允许点击顶部 滚动到初始位置 添加
myscrollView.scrollsToTop = YES;
[self.view addSubview:myscrollView];
1 0、 设置分页效果,桉叶滚动 设置滚动条的样式 是否有反弹效果
myScrollView.pagingEnabled = YES;
myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
myScrollView.bounces = YES;
11、给滑动视图添加视图
NSArray *imageName = @[@“1",@"2",@"3"];
for (int i =0; i<imageName.count; i++) {
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
imageView.image = [UIImage imageNamed:imageName[i]];
[myScrollView addSubview:imageView];
}
[self.view addSubview:myScrollView];
页面控件 UIPageControl
12、初始化页面控件,添加到self。view
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 20)];
[self.view addSubview:pageControl];
13、页面控件属性
1⃣️设置页数
pageControl.numberOfPages = imageName.count;
2⃣️设置指示的当前页面
pageControl.currentPage = 0;
3⃣️当只有一个页面的时候隐藏
pageControl.hidesForSinglePage = YES;
4⃣️设置小圆点的颜色
pageControl.pageIndicatorTintColor = [UIColor yellowColor];
pageControl.currentPageIndicatorTintColor = [UIColor redColor];//当前小圆点pageControl.tag = 119; //设置tag值
14、页面控件与滚动视图融合
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat x = scrollView.contentOffset.x;//得到滚动视图的偏移量
CGFloat w = CGRectGetWidth(self.view.frame);
UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:119];
pageControl.currentPage = x/w;//设置PageControl的当前页面
}