1.类名:UIScrollView,多个页面实现的原理,位置不变,内容改变。
用滚动视图点击查看大图片
// 1.查看大图片
UIScrollView *scoll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"3333" ofType:@"png" ]];
// 2.设置滚动视图的contentSize(视图的范围)跟图片的尺寸一样 如果contentSize小于滚动视图的bounds 就不会滚动
scoll.contentSize = image.size;
// 1⃣️:放大图片
// 1.需要用到 滚动视图的代理方法
// 2.设置放大缩小图片的最大最小倍数
// 3.让滚动视图 知道要放大缩小哪个视图
scoll.minimumZoomScale = 0.5;//设置最小倍数
scoll.maximumZoomScale = 2;// 设置最大倍数
scoll.delegate = self; //挂上代理,需先导入代理
// 3.在滚动视图上添加图片视图 在滚动视图上添加视图
view = [[UIImageView alloc]initWithFrame:self.view.frame];
// 4.添加图片
scoll.backgroundColor = [UIColor brownColor];
[self.view addSubview:scoll];
view.image = image;
[scoll addSubview:view];
}
#pragma mark ----scrollViewDelegate
// 设置在滚动视图中 放大的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return view;
}
当一个页面显示不全,但是该内容只能在同一个页面时,比如:修改个人资料等可以用滚动视图
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:self.view.frame];
scroll.contentSize = CGSizeMake(0, CGRectGetHeight(self.view.frame)+10); //视图的范围一定要大于硬件的屏幕
// 屏蔽滚动条 showsVerticalScrollIndicator(竖向滚动条) showsHorizontalScrollIndicator(横向滚动条)
scroll.showsVerticalScrollIndicator = NO;//设置是否隐藏纵向滚动条,这里是不隐藏
scroll.delegate = self;// 使用了代理,同样也得挂上代理,导入代理。
// 设置滚动视图 是否允许点击顶部 滚动到初始位置
scroll.scrollsToTop = YES;
[self.view addSubview:scroll];
text = [[UITextField alloc]initWithFrame:CGRectMake(80, 100, 200, 60)];
text.backgroundColor = [UIColor whiteColor];
[scroll addSubview:text];
}
/**
* 滚动到屏幕的最顶部的时候调用
*
* @param scrollView
*/
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
[text resignFirstResponder];// 回收键盘
}
UIPageControl的使用
// UIPageControl (常见的屏幕下方的小圆点)
UIPageControl *page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-40, CGRectGetWidth(self.view.frame), 20)];
// 点的 个数
page.numberOfPages = photos.count; // 设置个数和图片的count值一样
page.tag = 1000;
// 设置指示当前页面
page.currentPage = 1;
// 当只有一个页面的时候 隐藏page
page.hidesForSinglePage = YES;
// 设置空心小圆点的颜色
page.pageIndicatorTintColor = [UIColor brownColor];
// 设置当前小圆点的颜色
page.currentPageIndicatorTintColor = [UIColor redColor];
[self.view addSubview:page];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// 得到一个当前页数
// 滚动视图的偏移量 (持有的数据)屏幕的宽 (持有的数据)
// 滚动视图X的偏移量
CGFloat X = scrollView.contentOffset.x;
// 屏幕的宽
CGFloat W = CGRectGetWidth(self.view.frame);
// 偏移量除以宽 得到当前页面的宽
NSInteger curPage = X/W;
UIPageControl *page = (UIPageControl *)[self.view viewWithTag:1000];
// 设置page的当前页面
page.currentPage = curPage;
NSLog(@"*****%ld",(long)curPage);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;{
NSLog(@"%f",scrollView.contentOffset.x);
// 在哪一个页面 通过偏移的X轴的位置除以屏幕的宽度得到当前的页数
}