UIScrollView官方API分析

一 定义:

  • UIScrollView的类继承关系:其父类为UIView,其子类包括:UITableView,UICollectionView,UITextView.
  • 可知该类的特点是支持展示内容超过应用的窗口时.
  • 其能确保使用者通过轻扫手势和缩放手势等使内容随手势滚动.

二 状态保持:

  • 通过查找发现UIView,UIViewController都有这个属性,当然UIView的子类UIImageView,UIScrollView当然也有这个属性.
  • 这个属性预示着在重用的进程中,UIView,UIViewController和其内容应该被保存,并且用来区分其自身.
  • 这个属性默认置为nil,即其不需要被保存,这样保证不需要的就将其销毁,把内存节省出来,这大概也是为什么1G内存却能保证流畅的原因之一吧.
  • 当然不排除一些特殊的情况,其用法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
To make the state preservation and restoration work there are two steps that are always required:
The App delegate must opt-in
Each view controller or view to be preserved/restored must have a restoration identifier assigned.
You should also implement encodeRestorableStateWithCoder: and decodeRestorableStateWithCoder: for views and view controllers that require state to be saved and restored.
Add the following methods to the view controller of your UIImageView.
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
[coder encodeObject:UIImagePNGRepresentation(_imageView.image)
forKey:@"YourImageKey"];
[super decodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
_imageView.image = [UIImage imageWithData:[coder decodeObjectForKey:@"YourImageKey"]];
[super encodeRestorableStateWithCoder:coder];
}
State preservation and restoration is an optional feature so you need to have the application delegate opt-in by implementing two methods:
- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
Useful article about state preservation: http://useyourloaf.com/blog/2013/05/21/state-preservation-and-restoration.html

三 管理内容的展示

1
2
3
4
setContentOffset:animated:
contentOffset
contentSize
contentInset

四 管理滚动相关

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
scrollEnabled
directionalLockEnabled
scrollsToTop
- scrollRectToVisible:animated:
pagingEnabled
bounces
alwaysBounceVertical
alwaysBounceHorizontal
-touchesShouldBegin:withEvent:inContentView:
-touchesShouldCancelInContentView:
canCancelContentTouches
delaysContentTouches
decelerationRate
dragging
tracking
decelerating

五 管理滚动指示器

1
2
3
4
5
indicatorStyle
scrollIndicatorInsets
showsHorizontalScrollIndicator
showsVerticalScrollIndicator
- flashScrollIndicators

六 缩放相关

1
2
3
4
5
6
7
8
9
10
panGestureRecognizer
pinchGestureRecognizer
-zoomToRect:animated:
zoomScale
setZoomScale:animated:
maximumZoomScale
minimumZoomScale
zoomBouncing
zooming
bouncesZoom

七管理代理

1 Responding to Scrolling and Dragging

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
scrollViewDidScroll:
//Tells the delegate when the user scrolls the content view within the receiver.
scrollViewWillBeginDragging:
//Tells the delegate when the scroll view is about to start scrolling the content.
scrollViewWillEndDragging:withVelocity:targetContentOffset:
//Tells the delegate when the user finishes scrolling the content.
scrollViewDidEndDragging:willDecelerate:
//Tells the delegate when dragging ended in the scroll view.
scrollViewShouldScrollToTop:
//Asks the delegate if the scroll view should scroll to the top of the content.
scrollViewDidScrollToTop:
//Tells the delegate that the scroll view scrolled to the top of the content.
scrollViewWillBeginDecelerating:
//Tells the delegate that the scroll view is starting to decelerate the scrolling movement.
scrollViewDidEndDecelerating:
//Tells the delegate that the scroll view has ended decelerating the scrolling movement.

2 Managing Zooming

1
2
3
4
5
6
7
8
9
10
11
viewForZoomingInScrollView:
//Asks the delegate for the view to scale when zooming is about to occur in the scroll view.
scrollViewWillBeginZooming:withView:
//Tells the delegate that zooming of the content in the scroll view is about to commence.
scrollViewDidEndZooming:withView:atScale:
//Tells the delegate when zooming of the content in the scroll view completed.
scrollViewDidZoom:
//Tells the delegate that the scroll view’s zoom factor changed.

3 Responding to Scrolling Animations

1
scrollViewDidEndScrollingAnimation:

八 管理键盘

1
2
3
keyboardDismissMode
//The manner in which the keyboard is dismissed when a drag begins in the scroll view.
  • 这个特性我估计很多人都不知道,可以实现滚动的时候,隐藏键盘.

九 不常用的一些

1
2
directionalPressGestureRecognizer
refreshControl

十 关于Auto Layout的相关用法

一般如果你只是使用纯代码约束做适配,没有使用autolayout 或者 Masonry 等第三方做适配的话,这方面基本涉及不到,但是如果你在项目中使用了比如现在流行的Masonry来做适配的话,当在UIScrollView上布局字View时,就需要注意这个点了.

  • 首先你需要了解,frame是以父视图左上角位置为(0,0)点, 而bounds是以自身的左上角为(0.0)点算起.
  • 对于 UIScrollViewsubview 来说,它的leading/trailing/top/bottom space 是相对于 UIScrollViewcontentSize 而不是 bounds 来确定的,所以当你尝试用 UIScrollView 和它 subview 的 leading/trailing/top/bottom 来互相决定大小的时候,就会出现「Has ambiguous scrollable content width/height」warning
  • 正确的姿势是用 UIScrollView 外部的 view 或 UIScrollView 本身的 width/height 确定 subview 的尺寸,进而确定 contentSize。因为 UIScrollView 本身的 leading/trailing/top/bottom 变得不好用.
  • 所以我习惯的做法是在 UIScrollView 和它原来的 subviews 之间增加一个 content view.

这样做的好处有:

  • 不会在 storyboard 里留下 error/warning
  • 为 subview 提供 leading/trailing/top/bottom,方便 subview 的布局
  • 通过调整 contentview 的 size(可以是 constraint 的 IBOutlet)来调整 contentSize
  • 不需要 hardcode 与屏幕尺寸相关的代码
  • 更好地支持 rotation

十一 关于其在实际项目中的深层次应用

请点击iOS性能优化之UIScrollView实践经验

坚持原创技术分享,您的支持将鼓励我继续创作!