您的位置:首页 > 产品设计 > UI/UE

UIScrollView的delaysContentTouches与canCencelContentTouches属性

2016-06-15 09:52 543 查看
    UIScrollView有一个BOOL类型的tracking属性,用来返回用户是否已经触及内容并打算开始滚动,我们从这个属性开始探究UIScrollView的工作原理:
    当手指触摸到UIScrollView内容的一瞬间,会产生下面的动作:
• 拦截触摸事件
• tracking属性变为YES
• 一个内置的计时器开始生效,用来监控在极短的事件间隔内是否发生了手指移动

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

case2:当检测到时间间隔内手指没有移动,tracking属性保持YES,手指触摸下如果有(可以响应触摸事件的)内部控件,则将触摸事件传递给控件进行处理。
 
    有很多新闻类的App顶部都有一个滑动菜单栏,主要模型可能是由一个UIScrollView包含多个UIButton控件组成;当你操作的时候,手指如果是很迅速的在上面划过,会发现即使手指触摸的地方有UIButton,但是并没有触发该UIButton的任何触摸事件,这就是上面提到的case1;当你手指是缓慢划过或根本就没动,才会触发UIButton的触摸事件,这是case2的情况。
    上面的工作原理其实有一个属性开关来控制:delaysContentTouches。默认值为YES;如果设置为NO,则无论手指移动的多么快,始终都会将触摸事件传递给内部控件;设置为NO可能会影响到UIScrollView的滚动功能。
再看另一个BOOL类型的属性canCencelContentTouches,从字面上理解是“可以取消内容触摸“,默认值为YES。文档里的解释是这样的:
A Boolean value that controls whethertouches in the content view always lead to tracking.
If the value of this property is YES and aview in the content has begun tracking a finger touching it, and if the userdrags the finger enough to initiate a scroll, the view receives atouchesCancelled:withEvent: message
and the scroll view handles the touch as ascroll. If the value of this property is NO, the scroll view does not scrollregardless of finger movement once the content view starts tracking.
翻译为中文大致如下:
这个BOOL类型的值控制content view里的触摸是否总能引发跟踪(tracking)
如果属性值为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'stouch cancelled】");
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [super touchesBegan:touches withEvent:event];
   
   NSLog(@"【Button'stouch began】");
}
 
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   [super touchesMoved:touches withEvent:event];
   
   NSLog(@"【Button'stouch moved】");
}
 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   [super touchesEnded:touches withEvent:event];
 
   NSLog(@"【Button'stouch 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】
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS uiscrollview