UIScrollView(滚动视图)/UIPageControl(分页控件)
2015-01-25 13:09
567 查看
UIScrollView是UITableView和UITextView的超类
下面通过一个相册实例分别总结一下这两个控件的使用
我添加了3张照片在主页面显示,然后点击图片进入浏览图片,并且点击进入的图片是下一页当前的图片,然后可以循环滚动,
- (void)viewDidLoad {
[superviewDidLoad];
[selfcreateView];
}
- (void)createView
{
int m = 1;
for (int i =1; i <
4; i++) {
for (int j =1; j <
2; j++) {
self.imageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,0,
20, 30)];
// 循环便利图片
NSString *name = [NSStringstringWithFormat:@"hh%d",m];
m++;
// 创建一个图像对象
NSString *path = [[NSBundlemainBundle]
pathForResource:nameofType:@"jpg"];
UIImage *aImage = [UIImageimageWithContentsOfFile:path];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(j * 80 +
20, i * 160 -
50 ,150 ,
150);
[button
setBackgroundImage:aImageforState:UIControlStateNormal];
button.tag = i *
4 + j;
[button
addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
[self .viewaddSubview:_imageView];
[_imageView
release];
}
}
}
- (void)buttonAction:(id)sender
{
SecondViewController *second = [[SecondViewControlleralloc]
init];
[selfpresentViewController:second
animated:YEScompletion:nil];
[second
release];
}
- (void)createView
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(0,
20, 80,
20);
[button
setTitle:@"返回"forState:UIControlStateNormal];
[button
setTitleColor:[UIColorredColor]
forState:UIControlStateNormal];
[button
addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
self.scroll = [[UIScrollViewalloc]
initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,self.view.frame.size.height)];
[self.scrollsetBackgroundColor:[UIColorblackColor
]];
// 最小放大比例
_scroll.minimumZoomScale =0.5;
_scroll.maximumZoomScale =2.0;
//ContentSize:滚动范围的大小,也是镜头的大小
[_scrollsetContentSize:CGSizeMake(_scroll.frame.size.width
*5,
0)];
// 滚动条的样式
_scroll.indicatorStyle =UIScrollViewIndicatorStyleBlack;
// 加载后立即横向移动
[_scrollsetContentOffset:CGPointMake(_scroll.frame.size.width,0)];
//滚动的内容将以页面为单位进行滚动
_scroll.pagingEnabled =YES;
[self.viewaddSubview:_scroll];
[_scroll
release];
for (int i =1; i <
5; i++) {
NSString *name = [NSStringstringWithFormat:@"hh%d",i];
NSString *path = [[NSBundlemainBundle]
pathForResource:nameofType:@"jpg"];
UIImage *image = [UIImageimageWithContentsOfFile:path];
UIImageView *imageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,0,_scroll.frame.size.width
,200)];
[imageView
setImage:image];
//这里为什么还要设置一个滚动视图呢,解释一下,,如果不设置,那么我们放大一张图片的时候第二张图片会跑到第一张图片上,为了解决这个问题,所以需要新建一个小的ScrollVIew
UIScrollView *little = [[UIScrollViewalloc]
initWithFrame:CGRectMake((i -1)*_scroll.frame.size.width,150,
_scroll.frame.size.width,300)];
little.minimumZoomScale =
0.5;
little.maximumZoomScale =
3.0;
little.delegate =
self;
[little
addSubview:imageView];
[_scroll
addSubview:little];
[imageView
release];
}
self.page = [[UIPageControlalloc]
initWithFrame:CGRectMake(0,450,
375,
20)];
_page.numberOfPages =3;
[self.viewaddSubview:_page];
[_pageaddTarget:selfaction:@selector(pageAction:)forControlEvents:UIControlEventValueChanged];
[_page
release];
}
- (void)pageAction:(id)sender
{
// 点击事件
self.page = (UIPageControl *)sender;
CGFloat value =_page.currentPage *_scroll.frame.size.width;
[_scrollsetContentOffset:CGPointMake(value,0)];
}
- (void)buttonAction:(id)sender
{
MainViewController *main = [[MainViewControlleralloc]
init];
[selfpresentViewController:main
animated:YEScompletion:nil];
[main
release];
}
#pragma mark scrollViewdelegate协议
//已经滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//打印CGPoint类型的变量
//contentOffset/ 屏幕的宽度就是当前的页
NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView
*)scrollView{
NSLog(@"%s",__func__);
}
// 将要停止拖拽
- (void)scrollViewWillEndDragging:(UIScrollView
*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint *)targetContentOffset
{
NSLog(@"%s",__func__);
}
// 已经停止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView
*)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"%s",__func__);
}
// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView
*)scrollView{
NSLog(@"%s",__func__);
}
//已经停止减速,彻底停止
// 还原放大的效果:有两种方法:第一种通过判断类型(isKindOfClass)
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// if (self.scroll == scrollView) {
// NSArray *array = [scrollView subviews];
// for (UIScrollView *view in array) {
// //判断类型
// if([view isKindOfClass:[UIScrollView class]]){
// view.zoomScale = 1.0;
// }
// }
// }
//}
// 第二种方法:通过tag值
- (void)scrollViewDidEndDecelerating:(UIScrollView
*)scrollView{
if(_scroll == scrollView){
for(int i =0 ;i <
5 ;i++ ){
int tag = 100 + i;
UIScrollView *sc = (UIScrollView *)[self.viewviewWithTag:tag];
[sc
setZoomScale:1.0];
}
}
if(_scroll.contentOffset.x ==375 *
3){
[_scrollsetContentOffset:CGPointMake(0,0)];
}else
if (_scroll.contentOffset.x ==0){
[_scrollsetContentOffset:CGPointMake(375*3,0)];
}
if (_scroll.contentOffset.x/_scroll.frame.size.width
== 5) {
_page.numberOfPages =3;
}elseif
(_scroll.contentOffset.x/_scroll.frame.size.width
== 0){
_page.numberOfPages =1;
}
// currentPage 是当前页码,从0开始
_page.currentPage =_scroll.contentOffset.x/_scroll.frame.size.width;
}
#pragma mark 放大图片的协议
- (UIView *)viewForZoomingInScrollView:(UIScrollView
*)scrollView
{
// 放大第一张图片,找第一张图片,1.设置tag值,2.遍历子视图
NSArray *array = scrollView.subviews;
return [array objectAtIndex:0];
}
#pragma mark实现首页点击那张图片进入下一页时现显示的就是当前点击的图片并且从当前页开始滚动,但问题是怎么把mainViewController的tag值传到SecondViewController呢?
//方法
- (void)method:(NSInteger)temp
{
self.scroll.contentOffset =CGPointMake((temp -
1)*375,
0);
self.page.currentPage = temp -1;
}
下面通过一个相册实例分别总结一下这两个控件的使用
我添加了3张照片在主页面显示,然后点击图片进入浏览图片,并且点击进入的图片是下一页当前的图片,然后可以循环滚动,
- (void)viewDidLoad {
[superviewDidLoad];
[selfcreateView];
}
- (void)createView
{
int m = 1;
for (int i =1; i <
4; i++) {
for (int j =1; j <
2; j++) {
self.imageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,0,
20, 30)];
// 循环便利图片
NSString *name = [NSStringstringWithFormat:@"hh%d",m];
m++;
// 创建一个图像对象
NSString *path = [[NSBundlemainBundle]
pathForResource:nameofType:@"jpg"];
UIImage *aImage = [UIImageimageWithContentsOfFile:path];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(j * 80 +
20, i * 160 -
50 ,150 ,
150);
[button
setBackgroundImage:aImageforState:UIControlStateNormal];
button.tag = i *
4 + j;
[button
addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
[self .viewaddSubview:_imageView];
[_imageView
release];
}
}
}
- (void)buttonAction:(id)sender
{
SecondViewController *second = [[SecondViewControlleralloc]
init];
[selfpresentViewController:second
animated:YEScompletion:nil];
[second
release];
}
- (void)createView
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(0,
20, 80,
20);
[button
setTitle:@"返回"forState:UIControlStateNormal];
[button
setTitleColor:[UIColorredColor]
forState:UIControlStateNormal];
[button
addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
self.scroll = [[UIScrollViewalloc]
initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,self.view.frame.size.height)];
[self.scrollsetBackgroundColor:[UIColorblackColor
]];
// 最小放大比例
_scroll.minimumZoomScale =0.5;
_scroll.maximumZoomScale =2.0;
//ContentSize:滚动范围的大小,也是镜头的大小
[_scrollsetContentSize:CGSizeMake(_scroll.frame.size.width
*5,
0)];
// 滚动条的样式
_scroll.indicatorStyle =UIScrollViewIndicatorStyleBlack;
// 加载后立即横向移动
[_scrollsetContentOffset:CGPointMake(_scroll.frame.size.width,0)];
//滚动的内容将以页面为单位进行滚动
_scroll.pagingEnabled =YES;
[self.viewaddSubview:_scroll];
[_scroll
release];
for (int i =1; i <
5; i++) {
NSString *name = [NSStringstringWithFormat:@"hh%d",i];
NSString *path = [[NSBundlemainBundle]
pathForResource:nameofType:@"jpg"];
UIImage *image = [UIImageimageWithContentsOfFile:path];
UIImageView *imageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,0,_scroll.frame.size.width
,200)];
[imageView
setImage:image];
//这里为什么还要设置一个滚动视图呢,解释一下,,如果不设置,那么我们放大一张图片的时候第二张图片会跑到第一张图片上,为了解决这个问题,所以需要新建一个小的ScrollVIew
UIScrollView *little = [[UIScrollViewalloc]
initWithFrame:CGRectMake((i -1)*_scroll.frame.size.width,150,
_scroll.frame.size.width,300)];
little.minimumZoomScale =
0.5;
little.maximumZoomScale =
3.0;
little.delegate =
self;
[little
addSubview:imageView];
[_scroll
addSubview:little];
[imageView
release];
}
self.page = [[UIPageControlalloc]
initWithFrame:CGRectMake(0,450,
375,
20)];
_page.numberOfPages =3;
[self.viewaddSubview:_page];
[_pageaddTarget:selfaction:@selector(pageAction:)forControlEvents:UIControlEventValueChanged];
[_page
release];
}
- (void)pageAction:(id)sender
{
// 点击事件
self.page = (UIPageControl *)sender;
CGFloat value =_page.currentPage *_scroll.frame.size.width;
[_scrollsetContentOffset:CGPointMake(value,0)];
}
- (void)buttonAction:(id)sender
{
MainViewController *main = [[MainViewControlleralloc]
init];
[selfpresentViewController:main
animated:YEScompletion:nil];
[main
release];
}
#pragma mark scrollViewdelegate协议
//已经滚动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//打印CGPoint类型的变量
//contentOffset/ 屏幕的宽度就是当前的页
NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
}
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView
*)scrollView{
NSLog(@"%s",__func__);
}
// 将要停止拖拽
- (void)scrollViewWillEndDragging:(UIScrollView
*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint *)targetContentOffset
{
NSLog(@"%s",__func__);
}
// 已经停止拖拽
- (void)scrollViewDidEndDragging:(UIScrollView
*)scrollView willDecelerate:(BOOL)decelerate{
NSLog(@"%s",__func__);
}
// 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView
*)scrollView{
NSLog(@"%s",__func__);
}
//已经停止减速,彻底停止
// 还原放大的效果:有两种方法:第一种通过判断类型(isKindOfClass)
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
// if (self.scroll == scrollView) {
// NSArray *array = [scrollView subviews];
// for (UIScrollView *view in array) {
// //判断类型
// if([view isKindOfClass:[UIScrollView class]]){
// view.zoomScale = 1.0;
// }
// }
// }
//}
// 第二种方法:通过tag值
- (void)scrollViewDidEndDecelerating:(UIScrollView
*)scrollView{
if(_scroll == scrollView){
for(int i =0 ;i <
5 ;i++ ){
int tag = 100 + i;
UIScrollView *sc = (UIScrollView *)[self.viewviewWithTag:tag];
[sc
setZoomScale:1.0];
}
}
if(_scroll.contentOffset.x ==375 *
3){
[_scrollsetContentOffset:CGPointMake(0,0)];
}else
if (_scroll.contentOffset.x ==0){
[_scrollsetContentOffset:CGPointMake(375*3,0)];
}
if (_scroll.contentOffset.x/_scroll.frame.size.width
== 5) {
_page.numberOfPages =3;
}elseif
(_scroll.contentOffset.x/_scroll.frame.size.width
== 0){
_page.numberOfPages =1;
}
// currentPage 是当前页码,从0开始
_page.currentPage =_scroll.contentOffset.x/_scroll.frame.size.width;
}
#pragma mark 放大图片的协议
- (UIView *)viewForZoomingInScrollView:(UIScrollView
*)scrollView
{
// 放大第一张图片,找第一张图片,1.设置tag值,2.遍历子视图
NSArray *array = scrollView.subviews;
return [array objectAtIndex:0];
}
#pragma mark实现首页点击那张图片进入下一页时现显示的就是当前点击的图片并且从当前页开始滚动,但问题是怎么把mainViewController的tag值传到SecondViewController呢?
//方法
- (void)method:(NSInteger)temp
{
self.scroll.contentOffset =CGPointMake((temp -
1)*375,
0);
self.page.currentPage = temp -1;
}
相关文章推荐
- iOS 6编程-UIScrollView滚动视图和UIPageControl分页控件的简单应用
- iOS 6编程-UIScrollView滚动视图和UIPageControl分页控件实现图像分页显示(2)
- iOS 6编程-UIScrollView滚动视图和UIPageControl分页控件实现图像分页显示
- iOS开发之 滚动视图 + 页面控件(UIScrollView + UIPageControl)
- 第二十篇:滚动视图UIScrollView ,分页控制器UIPageControl 定时器 NSTimer
- 用UIScrollView,UIPageControl来实现滚动视图。
- 滚动视图UIScrollView、UIPageControl
- UI开发----UIScrollView和UIPageControl简单模拟滚动视图
- UIscrollView和UIPageControl的循环滚动
- 基于UIScrollView和UIPageControl控件做的用户引导界面
- UIScrollView+UIPageControl实现图片分页
- IOS的基本控件的使用-UIScrollView滚动视图的高级功能
- IOS基础UI之(九) UIPageControl结合UIScrollView实现分页效果
- UIScrollView+UIPageControl+NSTimer 图片自动滚动的两种实现方法
- UIUIScrollView滚动视图与UIPageControl
- UIScrollView和UIPageControl的分页 && NSTimer【图片轮播器】
- iOS:UIScrollView控件和UIPageControl控件的详解
- UIScrollView+UIpageControl实现图片的循环滚动切换
- Snail—UI学习之滚动视图UIScrollViewControl
- UIScrollView 和UIPageControl结合实现分页