iOS – 滚动视图(UIScrollView)详解
移动的设备的屏幕大小有限,当一个视图要显示的内容大于屏幕尺寸的时候就要用到滚动视图,比如一个网页的内容通常比屏幕尺寸大,那么浏览器就使用了滚动视图。
UIScrollView有一个contentSize属性,声明如下:
@property(nonatomic) CGSize contentSize;
这个属性表示滚动视图的内容有多大。
创建滚动视图实例:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIScrollView *sv = [[UIScrollView alloc] initWithFrame:self.view.frame]; sv.contentSize = CGSizeMake(1000, 1000); sv.backgroundColor = [UIColor redColor]; [self.view addSubview:sv]; }
效果图:
contentSize 属性为{1000,1000},可以很明显的看到横向和纵向滚动条。
1. 滚动
视图的滚动也没什么好说的,主要是设置一些属性:
scrollEnabled 是否开启滚动
scrollsToTop 默认为YES,用户可以单击状态栏滚动至顶部
bounces 默认为YES,当滚动至边界时还可以进一步滚动,松开时迅速弹回至原位
directionalLockEnabled 如果为YES,仅可以同时在一个方向上滚动
decelerationRate 在轻弹屏幕后,滚动的速度逐渐衰减,较低的值衰减越快
showsHorizontalScrollIndicator、showsVerticalScrollIndicator 默认为YES,滚动时滚动指示条可见
indicatorStyle 滚动条的样式(UIScrollViewIndicatorStyleBlack,UIScrollViewIndicatorStyleDefaultUIScrollViewIndicatorStyleWhite)
contentInset 指定内容四周的余量(上 左 下 右的顺序)
scrollIndicatorInsets 指定了滚动指示条在位置的切换
contentOffset 内容的定位点,当前显示内容的左上角
2. 分页
当内容大于屏幕时就可以分页,设置pagingEnabled属性为YES,滚动视图就不允许用户自由滑动,只能通过滑动至相邻的部分。
3. 缩放
使滚动视图可以缩放需要时设置minimumZoomScale和maximumZoomScale两个属性的值(默认为1),还需要在滚动视图的委托中实现viewForZoomingInScrollView:方法,该方法返回滚动视图中哪个子视图是可以缩放的,如果滚动视图有多个子视图,但是通常我们需要缩放整个滚动视图的内容,那么将多个视图放在一个视图即可,在将这个视图成为滚动视图的子视图。
滚动视图通过对捏合手势的响应进行缩放,如果缩放超出了我们设置的限制,当手势结束时,尺寸退回到我们设置的限制。如果想要严格的限制缩放不超过我们设置的限制,将bouncesZoom设置为NO就可以了。
在某种情况下,比如我们双击需要进行缩放的时候,就要用代码进行缩放,使用下面两个方法:
- (void)setZoomScale:(CGFloat)scale animated:(BOOL)animated NS_AVAILABLE_IOS(3_0); - (void)zoomToRect:(CGRect)rect animated:(BOOL)animated NS_AVAILABLE_IOS(3_0);
setZoomScale:animated: 根据比例缩放
zoomToRect:animated: 给定矩形的大小进行缩放
4. 委托
UIScrollViewDelegate可以接收大量消息开帮助你跟踪滚动视图的工作
下面是协议的定义
@protocol UIScrollViewDelegate<NSObject> @optional - (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes - (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes // called on start of dragging (may require some time and or distance to move) - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView; // called on finger up if the user dragged. velocity is in points/millisecond. targetContentOffset may be changed to adjust where the scroll view comes to rest - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0); // called on finger up if the user dragged. decelerate is true if it will continue moving afterwards - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate; - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; // called on finger up as we are moving - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; // called when scroll view grinds to a halt - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; // return a view that will be scaled. if delegate returns nil, nothing happens - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view NS_AVAILABLE_IOS(3_2); // called before the scroll view begins zooming its content - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale; // scale between minimum and maximum. called after any 'bounce' animations - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; // return a yes if you want to scroll to the top. if not defined, assumes YES - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView; // called when scrolling animation finished. may be called immediately if already at top @end
这些方法的意思就不解释了,有注释的,学习最快的方法就是看这些头文件。
posted on 2014-11-19 15:19 ′ Visitors 阅读(1919) 评论(0) 编辑 收藏 举报