一、UIScrollView 是什么
从官方文档上看(其实我也不喜欢看文档,哎,就看一次嘛,做个记录)
The UIScrollView class provides support for displaying content that is larger than the size of the application’s window. It enables users to scroll within that content by making swiping gestures, and to zoom in and back from portions of the content by making pinching gestures.
好,既然上了英文,那就得翻译。
它说,UIScrollView 这个类呢,可以 展示内容的尺寸超过了应用窗口(屏幕)的尺寸,并且可以让用户通过滑动手势去滚动那些内容 或者 拉近或者拉远部分内容通过捏。
用我的话理解就是,手机的屏幕尺寸是有限的,可是再有限,我们也不能通过减小字体、减小控件等影响体验的方式来展示内容,所以 UIScrollView 就出现了。UIScrollVIew 是用来展示一屏展示不下的时候,通过左右、上下等滑动展示内容的控件。
UIScrollView is the superclass of several UIKit classes including UITableView and UITextView.
二、UIScrollView 的使用场景
UIScrollView 的使用场景:一屏显示不下的都可以使用,不管是水平方向不够还是垂直方向不够,都可以使用。
三、UIScrollView 的基本用法
3.1 UIScrollView 属性
@property(nonatomic) CGPoint contentOffset; // default CGPointZero
@property(nonatomic) CGSize contentSize; // default CGSizeZero
@property(nonatomic) UIEdgeInsets contentInset; // default UIEdgeInsetsZero. add additional scroll area around content
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate; // default nil. weak reference
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled; // default NO. if YES, try to lock vertical or horizontal scrolling while dragging
@property(nonatomic) BOOL bounces; // default YES. if YES, bounces past edge of content and back again
@property(nonatomic) BOOL alwaysBounceVertical; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
@property(nonatomic) BOOL alwaysBounceHorizontal; // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled __TVOS_PROHIBITED;// default NO. if YES, stop on multiples of view bounds
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled; // default YES. turn off any dragging temporarily
@property(nonatomic) BOOL showsHorizontalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
@property(nonatomic) BOOL showsVerticalScrollIndicator; // default YES. show indicator while we are tracking. fades out after tracking
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets; // default is UIEdgeInsetsZero. adjust indicators inside of insets
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle; // default is UIScrollViewIndicatorStyleDefault
@property(nonatomic) CGFloat decelerationRate NS_AVAILABLE_IOS(3_0);
@property(nonatomic,readonly,getter=isTracking) BOOL tracking; // returns YES if user has touched. may not yet have started dragging
@property(nonatomic,readonly,getter=isDragging) BOOL dragging; // returns YES if user has started scrolling. this may require some time and or distance to move to initiate dragging
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating; // returns YES if user isn't dragging (touch up) but scroll view is still moving
@property(nonatomic) BOOL delaysContentTouches; // default is YES. if NO, we immediately call -touchesShouldBegin:withEvent:inContentView:. this has no effect on presses
@property(nonatomic) BOOL canCancelContentTouches; // default is YES. if NO, then once we start tracking, we don't try to drag if the touch moves. this has no effect on presses
下面
@property(nonatomic) CGSize contentSize;
官方文档给了一句话解释 The size of the content view
contentview 的尺寸,是指scrollview 可以滚动的范围,例如 scrollview 的frame 是(0,0,320,640),contentSize 是(640,1280) 则代表 scrollview 可以左右滚动一屏幕,上下滚动一屏幕。
@property(nonatomic) CGPoint contentOffset;
The point at which the origin of the content view is offset from the origin of the scroll view.
是scrollview当前显示区域顶点相对于frame顶点的偏移量,在上个例子中,如果当前 scrollview 向下滚动了一屏,则当前的 contentOffset 是(0,640)
@property(nonatomic) UIEdgeInsets contentInset;
是 scrollview 的内边距
@property(nullable,nonatomic,weak) id<UIScrollViewDelegate> delegate;
scrollview 的代理(协议),类似于 java 中的接口(interface),有java 基础的很好理解,这里理解为回调就可以了。
@property(nonatomic,getter=isDirectionalLockEnabled) BOOL directionalLockEnabled;
scrollview 的滚动方向控制,如果设置为true,则当用户垂直或者水平滚动的时候,另一个方向会被锁定,如果用户滚动的放心是斜着的,则这个属性失效
@property(nonatomic) BOOL bounces;
scrollview 滑动到边界的时候,是否有 回弹效果
@property(nonatomic) BOOL alwaysBounceVertical;
scrollview 的垂直滚动,当 bounces 属性设置为true,且alwaysBounceVertical 设置为true,则即使内容 比边界小,依然可以垂直滚动
@property(nonatomic) BOOL alwaysBounceHorizontal;
同上
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled
scrollview 滚动的翻页效果,效果类似于安卓的viewpager
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled
scrollView 是否支持滚动
@property(nonatomic) BOOL showsHorizontalScrollIndicator
scrollview 是否显示水平滚动条
@property(nonatomic) BOOL showsVerticalScrollIndicator
是否显示垂直滚动条
@property(nonatomic) UIEdgeInsets scrollIndicatorInsets
滚动条到边界的距离
@property(nonatomic) UIScrollViewIndicatorStyle indicatorStyle
滚动条的风格
@property(nonatomic) CGFloat decelerationRate
减速率,值用户滑动后,scrollview滚动的减速度,有两个常量可以用
UIScrollViewDecelerationRateNormal
UIScrollViewDecelerationRateFast
@property(nonatomic,readonly,getter=isTracking) BOOL tracking
返回true 代表用户已经触碰了scrollview,即使没有滑动
@property(nonatomic,readonly,getter=isDragging) BOOL dragging
用户是否已经在滚动 scrollview
@property(nonatomic,readonly,getter=isDecelerating) BOOL decelerating
是否是 用户滑动后,scrollview 的减速状态
@property(nonatomic) BOOL delaysContentTouches
如果为YES,就会延迟处理这个触摸手势的意图,直到确定了在极短时间内是否发生了滚动,如果没有滚动 ,就把触摸事件传递给触摸的subview处理 (可以响应事件的控件) ,如果滚动了,则scrollView就会滚动 自身响应触摸事件
如果为NO,scrollView立马触发- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view方法 ,交给用户自己判断,此方法返回NO,则scrollView 不会将事件传递给view, 返回YES ,表示让view响应这个触摸事件 。注意:默认返回YES
@property(nonatomic) BOOL canCancelContentTouches
如果为YES,当用户触摸手势已经被确定交给subview响应的时候 ,会立即调用- (BOOL)touchesShouldCancelInContentView:(UIView *)view ,交给此方法处理接下来的动作,如果此方法返回NO,则继续传递给subview,scrollView不会滚动
@property(nonatomic) CGFloat minimumZoomScale
最小缩放因子
@property(nonatomic) CGFloat zoomScale
缩放比例
@property(nonatomic) BOOL bouncesZoom
控制缩放的时候是否会反弹
@property(nonatomic,readonly,getter=isZooming) BOOL zooming
当前是否处于缩放状态
@property(nonatomic,readonly,getter=isZoomBouncing) BOOL zoomBouncing
当前是否处于缩放反弹状态
@property(nonatomic) BOOL scrollsToTop
触摸状态栏是否返回顶部
@property(nonatomic, readonly) UIPanGestureRecognizer *panGestureRecognizer
拖动手势
@property(nullable, nonatomic, readonly) UIPinchGestureRecognizer *pinchGestureRecognizer
向里或向外捏的手势
@property(nonatomic, readonly) UIGestureRecognizer *directionalPressGestureRecognizer
@property(nonatomic) UIScrollViewKeyboardDismissMode keyboardDismissMode
键盘消失模式
UIScrollViewKeyboardDismissModeOnDrag
ScrollView拖动的时候消失UIScrollViewKeyboardDismissModeInteractive
交互式效果,拖动ScrollView可以使键盘移出屏幕范围,可取消
@property (nonatomic, strong, nullable) UIRefreshControl *refreshControl
刷新控件