UIScrollView之isTracking delaysContentTouches canCancelContentTouches
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 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)
如果属性值为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】
参考链接:
UIScrollView之isTracking delaysContentTouches canCancelContentTouches的更多相关文章
- UITableView或UIScrollVIew上的UIButton的高亮效果
UITableView或UIScrollVIew上的UIButton的高亮效果 原文地址:http://www.jianshu.com/p/b4331f06bd34 最近做项目的时候发现,UIScro ...
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang
转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...
- iOS开发 日常错误积累
1.ios7 tableviewcell上面加入一个view,view上面有button,点击button不运行button的点击事件 解决的方法: self.view.userInteraction ...
- UItableViewCell上的button点击无响应的办法
由于IOS7中添加了滑动后出现编辑按钮的操作,所以使用scrollView来处理,UITableViewCellScrollView有对触摸的相应处理,导致按钮的点击效果被屏蔽了,但是点击事件还是在的 ...
- UIButton高亮状态卡顿
童鞋们有么有遇到过一个tableview或者是scrollview上放置一个button然后点击button,但是button的高亮状态切换不过来呢? 解决方案: 新建一个类继承自UITableVie ...
- UITableView上添加按钮,按钮点击效果延迟的解决办法
在自定义的TableView的初始化方法做如下操作 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame: ...
- UIScrollView的delaysContentTouches与canCencelContentTouches属性
UIScrollView有一个BOOL类型的tracking属性,用来返回用户是否已经触及内容并打算开始滚动,我们从这个属性开始探究UIScrollView的工作原理: 当手指触摸到UIScrollV ...
- UIScrollView 的 delaysContentTouches
UIScrollView 的一段说明: Because a scroll view has no scroll bars, it must know whether a touch signals a ...
- 你真的了解UIScrollView吗?
一:首先查看一下关于UIScrollView的定义 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding& ...
随机推荐
- python数据类型2
一 文件格式补充 在python3中,除字符串外,所有数据类型在内存中的编码格式都是utf-8,而字符串在内存中的格式是Unicode的格式. 由于Unicode的格式无法存入硬盘中,所以这里还有一种 ...
- 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)
传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...
- Unable to load tag handler class "com.showId.Id.ShowId" for tag "ShowId:ShowId"] with root cause错误的解决方案
严重: Servlet.service() for servlet [jsp] in context with path [/Biaoqian] threw exception [/1.jsp (l ...
- 使用LVM对系统盘进行扩容
不知道大家有没有碰到在安装CentOS时个,对系统每个挂载点分配多大容量比较合适的问题?如果挂载点容量分配大小,在某天不够用的时候怎么办:分配太大又存在浪费的情况.特别是在遇到系统盘特别小的时 ...
- (并查集 贪心思想)Supermarket -- POJ --1456
链接: http://poj.org/problem?id=1456 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82830#probl ...
- idea 优化
http://www.iyunv.com/thread-348537-1-1.html
- codevs 1012
题目描述 Description 给出n和n个整数,希望你从小到大给他们排序 输入描述 Input Description 第一行一个正整数n 第二行n个用空格隔开的整数 输出描述 Output De ...
- Oracle SQL性能优化技巧大总结
http://wenku.baidu.com/link?url=liS0_3fAyX2uXF5MAEQxMOj3YIY4UCcQM4gPfPzHfFcHBXuJTE8rANrwu6GXwdzbmvdV ...
- Android-intent.addFlags-Activity启动模式
之前写的Android-Activity启动模式(launchMode),Android-Activity启动模式-应用场景,讲解的都是在AndroidManifest.xml配置launchMode ...
- Python学习-29.Python中列表的一些操作
in关键字: 注意这个是关键字,用来判断元素是否在集合中存在. list = ['a','b','c'] print('a' in list) print('f' in list) 将依次输出 Tru ...