UIScrollView高级用法:delaysContentTouches与canCencelContentTouches属性

来源:互联网 发布:伦理网络法电影 编辑:程序博客网 时间:2024/06/05 20:06

需求:UIScrollView+btns。点击btn高亮(Highlighted),滑动UIScrollView时取消高亮;研究了一下,整理如下:

1.实现:点击btn高亮(Highlighted),滑动UIScrollView时保持高亮状态,是不合理的,无法实现的。

2.实现:点击btn高亮(Highlighted), 滑动UIScrollView时取消高亮状态,是可以做到的。


按照从不同的要素去解释原理:

一、UIScrollView原理,以时间为轴线

从你的手指touch屏幕开始,scrollView开始一个timer,如果:

1.  150ms内如果你的手指没有任何动作,消息就会传给subView。

2.  150ms内手指有明显的滑动(一个swipe动作),scrollView就会滚动,消息不会传给subView。

3.  150ms内手指没有滑动,scrollView将消息传给subView,但是之后手指开始滑动,scrollView传送touchesCancelled消息给subView,然后开始滚动。


二、UIScrollView原理,tracking属性为轴线

UIScrollView有一个BOOL类型的tracking属性,用来返回用户是否已经触及内容并打算开始滚动,我们从这个属性开始探究UIScrollView的工作原理:

当手指触摸到UIScrollView内容的一瞬间,会产生下面的动作:

  • 拦截触摸事件
  • tracking属性变为YES

  • 一个内置的计时器开始生效,用来监控在极短的事件间隔内是否发生了手指移动

    case1:当检测到时间间隔内手指发生了移动,UIScrollView自己触发滚动,tracking属性变为NO,手指触摸下即使有(可以响应触摸事件的)内部控件也不会再响应触摸事件。

    case2:当检测到时间间隔内手指没有移动,tracking属性保持YES,手指触摸下如果有(可以响应触摸事件的)内部控件,则将触摸事件传递给控件进行处理。

     

当你手指是缓慢划过或根本就没动,才会触发UIButton的触摸事件,这是case1的情况;

有很多新闻类的App顶部都有一个滑动菜单栏,主要模型可能是由一个UIScrollView包含多个UIButton控件组成;当你操作的时候,手指如果是很迅速的在上面划过,会发现即使手指触摸的地方有UIButton,但是并没有触发该UIButton的任何触摸事件,这就是上面提到的case2。



上面的工作原理其实有一个属性开关来控制:delaysContentTouches

官方解释:

A Boolean value that determines whether the scroll view delays the handling of touch-down gestures.

中文翻译:

一个布尔值,该值决定了滚动视图是否延迟了触控手势的处理。

默认值为YES;如果设置为NO,则无论手指移动的多么快,始终都会将触摸事件传递给内部控件;设置为NO可能会影响到UIScrollView的滚动功能。

delaysContentTouches的作用:

这个标志默认是YES,使用上面的150ms的timer,如果设置为NO,touch事件立即传递给subView,不会有150ms的等待。

Discussion

If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:. The default value is YES.

默认YES;如果设置为NO,会马上执行touchesShouldBegin:withEvent:inContentView:


- (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches
                 withEvent:(UIEvent *)event 

inContentView:(UIView *)view

Discussion

The default behavior of UIScrollView is to invoke the UIResponder event-handling methods of the target subview that the touches occur in.

系统默认是允许UIScrollView,按照消息响应链向子视图传递消息的。(即返回YES)

Return Value

Return NO if you don’t want the scroll view to send event messages to view. If you want view to receive those messages, return YES (the default).

如果你不想UIScrollView的子视图接受消息,返回NO。

应用描述(作者注释):这个方法是最先接收到滑动事件的(优先于button的

UIControlEventTouchDown,以及- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event),

如果返回YES,touche事件沿着消息响应链传递;

如果返回NO,表示UIScrollView接收这个滚动事件,不必沿着消息响应链传递了。



- (BOOL)touchesShouldCancelInContentView:(UIView *)view

应用描述(作者注释):

如果返回YES:(系统默认)是允许UIScrollView,按照消息响应链向子视图传递消息的

如果返回NO:UIScrollView,就接收不到滑动事件了。




再看另一个BOOL类型的属性canCancelContentTouches,从字面上理解是“可以取消内容触摸“,默认值为YES。文档里的解释是这样的:

A Boolean value that controls whether touches in the content view always lead to tracking.

If the value of this property is YES and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled:withEvent: message and the scroll view handles the touch as a scroll. If the value of this property is NO, the scroll view does not scroll regardless of finger movement once the content view starts tracking.

翻译为中文大致如下:

这个BOOL类型的值控制content view里的触摸是否总能引发跟踪(tracking)

cancelsTouches的作用:

应用描述(作者注释):

 默认设置为YES,

 如果设置为NO,这消息一旦传递给subView,这scroll事件不会再发生。


如果属性值为YES并且跟踪到手指正触摸到一个内容控件,这时如果用户拖动手指的距离足够产生滚动,那么内容控件将收到一个touchesCancelled:withEvent:消息,而scroll view将这次触摸作为滚动来处理。如果值为NO,一旦content view开始跟踪(tracking==YES),则无论手指是否移动,scrollView都不会滚动。

简单通俗点说,如果为YES,就会等待用户下一步动作,如果用户移动手指到一定距离,就会把这个操作作为滚动来处理并开始滚动,同时发送一个touchesCancelled:withEvent:消息给内容控件,由控件自行处理。如果为NO,就不会等待用户下一步动作,并始终不会触发scrollView的滚动了。



可以用一段代码来验证并观察一下,定义一个MyScrollView继承自UIScrollView,一个MyButton继承自UIButton,然后重写部分方法:

MyScrollView.m

复制代码
- (BOOL)touchesShouldCancelInContentView:(UIView *)view{    [super touchesShouldCancelInContentView:view];        NSLog(@"touchesShouldCancelInContentView");        return YES;}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    [super touchesCancelled:touches withEvent:event];        NSLog(@"touchesCancelled");}
复制代码

MyButton.m

复制代码
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    [super touchesCancelled:touches withEvent:event];        NSLog(@"【Button's touch cancelled】");}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [super touchesBegan:touches withEvent:event];        NSLog(@"【Button's touch began】");}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    [super touchesMoved:touches withEvent:event];        NSLog(@"【Button's touch moved】");}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    [super touchesEnded:touches withEvent:event];    NSLog(@"【Button's touch ended】");}
复制代码

其实就是在各个方法执行时打印出一个标记,当canCencelContentTouches值为YES时,用户触摸并移动手指再放开:

【Button's touch began

【Button's touch moved

  ……

【Button's touch moved

touchesShouldCancelInContentView

【Button's touch cancelled

当canCencelContentTouches值为NO时,用户触摸并移动手指再放开:

【Button's touch began】

【Button's touch moved】

  ……

【Button's touch moved】

【Button's touch ended】


资料来源:
http://blog.sina.com.cn/s/blog_a3dbd02a0101749m.html
http://www.cnblogs.com/wayne23/p/4011266.html
http://blog.csdn.net/opentogether/article/details/52185419


0 0
原创粉丝点击