UIScrollView方法 属性详解

 

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

--参考文章: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 
ios

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

======经常使用属性======
iphone

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

--CGSize contentSize:contentView的大小;测试

--UIEdgeInsets contentInset:contentView四周的扩展大小,至关改变ContentView的大小,但不是改变contentSize(UIEdgeInsets是个结构体,里面的数值多是负数,因此扩展的结果多是contentView被蚕食了一部分。但无论contentView变大了仍是变小了,contentOffSet的值仍是相比原来大小的偏移);ui

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

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

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

--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了,反之若是是yestracking后,手指移动,会调用touchesShouldCancelInContentView方法;

--备注:注意顺序,因此当delaysContentTouchesnocanCancelContentTouchesno的时候,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方法,若是返回值为yestouch事件传给子视图;若是为notouch事件不传给子视图,进入状态n。(跳转) 

步骤3touch move的,若是canCancelContentTouchesno,不调用touchesShouldCancelInContentView方法,类也不scroll,进入状态m,若是canCancelContentTouchesyes,调用touchesShouldCancelInContentView,根据touchesShouldCancelInContentView方法的返回值,若是是yes,进入状态m。若是是no,进入状态s   

状态ntracking为真,若是touch move的就scrolltouch松开,进入状态

状态stouch move中,类scroll若是touch 松开,进入状态

状态mtouch move中,类不scroll若是touch 松开,进入状态

状态e:结束  

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

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

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

--float zoomScale缩放的比例(minimumZoomScalemaximumZoomScale之间变化);若是设置的话,写在 [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

相关文章
相关标签/搜索