原文地址:http://blog.csdn.net/hakusan/article/details/38942279
UIScrollView是iOS中的滑动控件,用来解决当前要显示的内容超出视图的范围时,可以通过滑动方式查看视图的所有内容
1.创建一个UIScrollView对象,并改变背景颜色
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 20, 280, 528)];
scrollView.backgroundColor = [UIColor orangeColor];
scrollView.tag = 200;
[self.viewaddSubview:scrollView];
[scrollViewrelease];
2.设置内容页的大小(如果想要视图滑动,内容页大小必须要比scrollView的大小大)
scrollView.contentSize = CGSizeMake(500, 600);
3.创建一个UIImageView,用来显示图片
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 280, 528)];
image.tag = 100;
image.image = [UIImage imageNamed:@"1.jpg”];("1.jpg”,图片名)
[scrollViewaddSubview:image];[imagerelease];
4.设置内容区域的偏移量
scrollView.contentOffset = CGPointMake(300, 0);
5.设置视图能否滑动(YES,能滑动;NO不能滑动)
scrollView.scrollEnabled = NO;
6.内容区域相对于frame在哪显示
scrollView.contentInset = UIEdgeInsetsMake(100, 100, 0, 0);
7.隐藏水平滑动指示器scrollView.showsHorizontalScrollIndicator=NO;
8.隐藏垂直滑动指示器scrollView.showsVerticalScrollIndicator=NO;
9.关闭滑动效果的回弹效果scrollView.bounces = NO;
10.设置当点击状态条时,scrollView能否滑动到最顶端scrollView.scrollsToTop=YES;
11.设置scrollView是否可以整屏滑动(一次滑动整个scrollView的大小)
scrollView.pagingEnabled = YES;
12.设置最大缩放比例scrollView.maximumZoomScale=4.0;
13.设置最小缩放比例scrollView.minimumZoomScale=1.0;
协议中方法的触发时机
如果想要哪种效果,只需在相应方法里做操作
只要scrollView滑动就会触发(会触发多次)
- (void)scrollViewDidScroll:(UIScrollView*)scrollView
只要scrollView缩放就会触发
- (void)scrollViewDidZoom:(UIScrollView*)scrollViewNS_AVAILABLE_IOS(3_2)
当将要拖拽scrollView时触发,手指结束scrollView并且将要滑动时触发
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
当结束拖拽时触发(手指将要离开屏幕)
- (void)scrollViewWillEndDragging:(UIScrollView*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inoutCGPoint*)targetContentOffsetNS_AVAILABLE_IOS(5_0)
当结束拖拽时触发(手指已经离开屏幕)
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate
当scrollView滑动将要减速时触发(将要停止)
- (void)scrollViewWillBeginDecelerating:(UIScrollView*)scrollView
当scrollView结束减速时触发(停止滑动)
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
当设置scrollView,有一个动画效果时触发
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView*)scrollView
设置要缩放的scrollView上面的哪一个子视图,只能是子视图,不能是scrollView本身.
- (UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView
当将要开始缩放时触发- (void)scrollViewWillBeginZooming:(UIScrollView*)scrollView withView:(UIView*)viewNS_AVAILABLE_IOS(3_2)
当结束缩放时触发
- (void)scrollViewDidEndZooming:(UIScrollView*)scrollView withView:(UIView*)view atScale:(CGFloat)scale
只有当scrollsToTop属性设置为YES时,该方法才会触发,进一步询问点击状态条是否有效
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView*)scrollView
当点击状态条并且scrollView滑动到顶端时触发
- (void)scrollViewDidScrollToTop:(UIScrollView*)scrollView