您的位置:首页 > 产品设计 > UI/UE

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: