UIScrollView方法 属性详解

--前言:UIScrollView使用非常广,本文研究UIScrollView各属性和方法,明白它们的意义、作用。在后面的一篇文章有整理UIScrollView一些常见用法以及一些效果的实现思路。

--参考文章:http://www.cocoachina.com/iphonedev/sdk/2010/1224/2503.html  &&  http://zjqzy03080312.blog.163.com/blog/static/18574280720121121105928687  &&  http://blog.csdn.net/wzzvictory/article/details/9264335  
--官方查阅文档  https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html

--介绍:UIScrollView用于在一个小范围里显示很大的内容的控件。通过用户平滑、手捏手势,在这个小区域里查看不同内容。是UITableView和UITextView的父类。它是视图,但是比较特殊,可以看成把它看成2层的结构。上面是它的frame层,跟一般试图一样,是它的可见区域,下面层是contentView,可以滑动。

======常用属性======

--CGPoint contentOffSet:contentView的偏移值(正常contentOffSet.x/y值都为正数,但有时(bounces = YES)拽出边界了或者contentInset设置了,还是会出现负数,伴随着contentView滑动,总在改变);

--CGSize contentSize:contentView的大小;

--UIEdgeInsets contentInset:contentView四周的扩展大小,相当改变ContentView的大小,但不是改变contentSize(UIEdgeInsets是个结构体,里面的数值可能是负数,所以扩展的结果可能是contentView被蚕食了一部分。但不管contentView变大了还是变小了,contentOffSet的值还是相比原来大小的偏移);

--id<UIScrollerViewDelegate> delegate:它的代理;

--BOOL directionalLockEnabled:默认是NO,可以在垂直和水平方向同时运动。当值是YES时,假如一开始是垂直或者是水平运动,那么接下来会锁定另外一个方向的滚动。假如一开始是对角方向滚动,则不会禁止某个方向(测试锁定不了,难道是contentsize跟frame高度或宽度一样时用的)

--BOOL bounces:默认是 yes,就是滚动超过边界会反弹有反弹回来的效果。假如是 NO,那么滚动到达边界会立刻停止

--UIScrollViewIndicatorStyle  indicatorStyle:设定滚动条的样式;

--BOOL alwaysBounceVertical:默认no,控制垂直方向遇到边框是否反弹(但bounces为NO时,它为yes,也不反弹。/if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically,测试不好使);

--BOOL alwaysBounceHorizontal:默认no,控制水平方向遇到边框是否反弹(但bounces为NO时,它为yes,也不反弹);

--BOOL pagingEnabled:default NO,contentView是否整页翻动;

--BOOL scrollEnabled:default YES,contentView是否能滚动;

--BOOL showsHorizontalScrollIndicator:default YES,是否显示水平方向的滚动条;

--BOOL showsVerticalScrollIndicator:default YES  是否显示垂直方向的滚动条;

--UIEdgeInsets scrollIndicatorInsets:滚动条在scrollerView中的位置的扩展;

--float  decelerationRate:手指放开后的减速率(测试发现设定这个值没用);

============交互相关============

--Scrolling with no scroll bars is a bit complex. on touch down, we don't know if the user will want to scroll or track a subview like a control. 
on touch down, we start a timer and also look at any movement. if the time elapses without sufficient change in position, we start sending events to  the hit view in the content subview. if the user then drags far enough, we switch back to dragging and cancel any tracking in the subview. 
 the methods below are called by the scroll view and give subclasses override points to add in custom behavior.  
you can remove the delay in delivery of touchesBegan:withEvent: to subviews by setting delaysContentTouches to NO. 

--UIScrollView的事件响应顺序跟一般的不同,一般的见参考文档, UIScrollView的工作原理,当手指touch的时候,UIScrollView会拦截Event,会等待一段时间,在这段时间内,如果没有手指没有移动,当时间结束时,UIScrollView会发送tracking events到子视图上。在时间结束前,手指发生了移动,那么UIScrollView就会进行移动,从而取笑发送tracking顺序说明: 当手指touch的时候,如果scrollView上面有可交互的视图,track,->或滑动或点击

--BOOL tracking:(readonly)returns YES if user has touched. may not yet have started dragging,即用户按上了,不管滑没滑

--BOOL dragging:(readonly)returns YES if user has started scrolling. this may require some time and or distance to move to initiate dragging,用户在手指在scrolll时,松开了即为no了

--BOOL decelerating:(readonly) returns YES if user isn't dragging (touch up) but scroll view is still moving

 --BOOL delaysContentTouches:就是手指点击到类的时候,如果它为yes,则按之前讲的处理,如果为no,并且点在了“可交互的视图”,立马调用touchesShouldBegin

--BOOL canCancelContentTouches:是if touches have already been delivered to a subview of the scroll view之后发生的事,如果no,即使touch move,也不scroll了,反之如果是yes,tracking后,手指移动,会调用touchesShouldCancelInContentView方法;

--备注:注意顺序,所以当delaysContentTouches为no,canCancelContentTouches为no的时候,touchesShouldBegin方法的返回值不同,滑动在“可交互的视图”的效果也不同,一个滑的动,一个滑不动(注意是touch move了之后的事情)

------------相关方法------------

 // override points for subclasses to control delivery of touch events to subviews of the scroll view
 // called before touches are delivered to a subview of the scroll view. if it returns NO the touches will not be delivered to the subview 

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view;// default returns YES

--调用地方:方法在UIScrollView的子类中被重写,被调用,

--调用时候:如果delaysContentTouches为yes,则手指点击的时候,会捕获到Event,等待一段时间,在这段时间内,如果没有手指没有移动,并且手指点击在了一个“可交互的视图”则调用touchesShouldBegin,否则类scroll。如果delaysContentTouches为no,只要手指点击了,并且点在一个“可交互的视图”,立刻调用touchesShouldBegin方法。

--备注:当点击子类中的“可交互的视图”上时,如果返回值为no,点击事件不会传递给子视图了。(例如上面加了一个button,如果它返回no,点击button,没有效果)。yes反之;

 // called before scrolling begins if touches have already been delivered to a subview of the scroll view. if it returns NO the touches will continue to be delivered to the subview and scrolling will not occur 
// not called if canCancelContentTouches is NO. default returns YES if view isn't a UIControl 

- (BOOL)touchesShouldCancelInContentView:(UIView *)view; 

--调用时候,上面已讲, --返回值说明:yes,发生滚动,touch不在传递给子视图,no,不滚动,touch传递给子视图

总结: 用户touch--》tracking真--》如果点击一个可交互视图上,发生后面的事,如果没有,一般情况--》

步骤1:根据delaysContentTouches的值,判断什么时候步骤2(立刻还是判断一定条件);

步骤2:调用touchesShouldBegin方法,如果返回值为yes,touch事件传给子视图;如果为no,touch事件不传给子视图,进入状态n。(跳转)

步骤3:touch move的,如果canCancelContentTouches为no,不调用touchesShouldCancelInContentView方法,类也不scroll,进入状态m,如果canCancelContentTouches为yes,调用touchesShouldCancelInContentView,根据touchesShouldCancelInContentView方法的返回值,如果是yes,进入状态m。如果是no,进入状态s。

状态n:tracking为真,如果touch move的就scroll,touch松开,进入状态e

状态s:touch move中,类scroll,如果touch 松开,进入状态e

状态m:touch move中,类不scroll,如果touch 松开,进入状态e

状态e:结束

============缩放相关============

--float minimumZoomScale缩放的最小比例;

--float maximumZoomScale:缩放的最大比例;

--float zoomScale缩放的比例(在minimumZoomScale和maximumZoomScale之间变化);如果设置的话,写在 [self.view addSubview:_scrollView];之后才能好使

 --BOOL bouncesZoom :控制缩放到边界的时是否会反弹;

--BOOL zooming(readonly):判断控件的大小是否正在缩放;

--BOOL zoomBouncing(readonly):判断控件是否缩放到了最大/最小;

--备注:如果scollView设置了缩放,但又设置contentszie等,刚开始没缩放的时候能scroll,但一但缩放过,就只能缩放,不能scroll了

// When the user taps the status bar, the scroll view beneath the touch which is closest to the status bar will be scrolled to top, but only if its `scrollsToTop` property is YES, its delegate does not return NO from `shouldScrollViewScrollToTop`, and it is not already at the top. 
// On iPhone, we execute this gesture only if there's one on-screen scroll view with `scrollsToTop` == YES. If more than one is found, none will be scrolled.

设置单击状态栏控件是否滚动到顶部

--BOOL scrollsToTop

上一篇:linux poll函数


下一篇:简述UIScrollView的属性和用法