UIScrollView控件的一些属性
// 创建一个ScrollView
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:Rect];
scrollView.backgroundColor = [UIColor colorWithRed:0.993 green:0.996 blue:1.000 alpha:1.000];
// scrollView要想滚动,必须使得内容区域的大小大于当前屏幕的大小 这里所谓的屏幕大小指的是scrollView本身的大小
scrollView.contentSize = CGSizeMake(5*kWidth, kHeight);
// 设置内容区域的偏移量,即内容区域相对于视图左上角的坐标
scrollView.contentOffset = CGPointMake(50, 0);
// 当点击状态条的时候,scrollView是否滚动到顶部.(PS. 如果有多个scrollView同时存在,该属性可能会失效)
scrollView.scrollsToTop = YES;
// 每次滚动是否滚动整个屏幕那么大,这里所谓的屏幕指的是scrollView的大小
scrollView.pagingEnabled = YES;
// 滑到边界是否回弹,默认是yes
// scrollView.bounces = NO;
/***********************缩放************************************/
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 3;
scrollView.delegate = self;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 50, 100, 30)];
label.text = @"hahahahah";
// label.textColor = [UIColor whiteColor];
[scrollView addSubview:label];
[label release];
[self.view addSubview:scrollView];
[scrollView release];
#pragma mark - 缩放相关的方法
// 要想实现缩放 必须要实现viewForZoomingInScrollView方法 因为在进行缩放的时候 需要告知要缩放那个视图
// 该方法需要一个UIView类型的返回值 返回值必须是scrollView里面的一个子视图.如果返回的是scrollView里面的label,那么就会对label进行缩放,如果反悔了imageView就会对imageView进行缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return scrollView.subviews[0];
}
UIPageControl的一些属性
// 1.UIPageControl继承自谁
// 2.UIPageControl的创建
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(30, 450, 300, 30)];
// pageControl.backgroundColor =[UIColor colorWithRed:1.000 green:0.230 blue:0.110 alpha:1.000];
// numberOfPages表示一共有多少页
pageControl.numberOfPages = 5;
// currentPage表示当前是第几页,页数是从0开始计算的
// pageControl.currentPage = 3;
pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.129 green:0.363 blue:1.000 alpha:1.000];
pageControl.pageIndicatorTintColor = [UIColor colorWithRed:1.000 green:0.903 blue:0.504 alpha:1.000];
[pageControl addTarget:self action:@selector(doTapPageControl:) forControlEvents:(UIControlEventValueChanged)];
[self.view addSubview: pageControl];
[pageControl release];
// 3.UIPageControl能否添加事件
- (void)doTapPageControl:(UIPageControl *)pageControl
{
NSLog(@"%ld", pageControl.currentPage + 1);
[[self.view.subviews objectAtIndex:0] setContentOffset:CGPointMake(pageControl.currentPage*kWidth, 0)];
}
这两个控件通常结合使用
/**
* 需求如下:
1.创建一个与屏幕等大的scrollView.
2.创建一个pageControl放在屏幕偏下方的位置
3.创建5个label,放在scrollView上,每一个label的大小都与scrollView等大,只是这5个label,平在scrollView上面
4.在scrollView滑动结束后 让pageControl选中的点与label上展示的内容对应
5.在点击pageControl的时候,让scrollView滑动到对应的地方
6.让scrollView实现缩放
注意:scrollView在缩放的过程中会改变自身的contentSize
*
*/
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:Rect];
scrollView.contentSize = CGSizeMake(KWidth * 5, KHeight);
scrollView.pagingEnabled = YES;
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 3;
scrollView.delegate = self;
for (int i = 0; i < 5; i++) {
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i * KWidth, 0, KWidth, KHeight)];
label.text = [NSString stringWithFormat:@"%d", i ];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:100];
label.backgroundColor = [UIColor colorWithRed:1.000 green:0.897 blue:0.474 alpha:1.000];
[scrollView addSubview:label];
[label release];
}
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, KHeight - 80, KWidth, 30)];
pageControl.numberOfPages = 5;
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
pageControl.pageIndicatorTintColor = [UIColor grayColor];
[pageControl addTarget:self action:@selector(doTapPageControl:) forControlEvents:(UIControlEventValueChanged)];
[self.view addSubview:scrollView];
[self.view addSubview:pageControl];
[scrollView release];
[pageControl release];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x < 0) {
scrollView.contentOffset = CGPointMake(4 * KWidth, 0);
}
else if (scrollView.contentOffset.x + KWidth > scrollView.contentSize.width)
{
scrollView.contentOffset = CGPointMake(0, 0);
}
[self.view.subviews[1] setCurrentPage:(scrollView.contentOffset.x/KWidth)];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 1.获取pageControl
// UIPageControl *pageControl = self.view.subview[1];
// 2.修改pageControl的currentPage(通过scrollView.contentOffSet来计算出currentPage)
// NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
[self.view.subviews[1] setCurrentPage:(scrollView.contentOffset.x/KWidth)];
}
- (void)doTapPageControl:(UIPageControl *)pageControl
{
// 1.获取scrollView
// 2.让scrollView 滚动到对应的位置
[self.view.subviews[0] setContentOffset:CGPointMake(pageControl.currentPage * KWidth, 0)];
}
#pragma mark - 缩放代码
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
int i = scrollView.contentOffset.x/KWidth;
return scrollView.subviews[i];
}