UIScrollView的作用原理,实现scrollView传递touch事件给子视图【转】
来源:互联网 发布:淘宝网格子连衣裙 编辑:程序博客网 时间:2024/04/30 10:53
原文:http://blog.csdn.net/backapace/article/details/10897449
UIScrollView的工作原理,当手指touch的时候,UIScrollView会拦截Event,会等待一段时间,在这段时间内,如果没有手指没有移动,当时间结束时,UIScrollView会发送tracking events到子视图上。在时间结束前,手指发生了移动,那么UIScrollView就会进行移动,从而取笑发送tracking。
那么,UIScrollView的子类想要接受touch事件,就是用户点击UIScrollView上的视图时,要先处理视图上的touch,而不发生滚动。这时候就需要UIScrollView的子类重载touchesShouldBegin:withEvent:inContentView: ,从而决定自己是否接受子视图中的touch事件。
上面都是理论的知识,下面看一个简单的例子:
外面红色是一个UIScrollView,黄色是在UIScrollView上添加的UIView。最后的效果是,当在黄色区域内touch时,touch事件会作用到UIView上,当touch红色区域时,整个视图上下滚动。下面是实现的过程。
一、创建工程,然后创建myScrollView,并且myScrollView继承自UIScrollView。
- #import <UIKit/UIKit.h>
- @interface myScrollView : UIScrollView
- @end
- #import "myScrollView.h"
- #import "myView.h"
- @implementation myScrollView
- - (id)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- [self setBackgroundColor:[UIColor redColor]];
- myView * aView = [[myView alloc] initWithFrame:CGRectMake(1, 3, 100, 200)];
- [self addSubview:aView];
- [aView release];
- }
- return self;
- }
- - (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
- NSLog(@"用户触摸了scroll上的视图%@,是否开始滚动scroll", view);
- //返回yes - 将触摸事件传递给相应的subView; 返回no - 直接滚动scrollView,不传递触摸事件到subView
- return YES// NO;
- }
- - (BOOL)touchesShouldCancelInContentView:(UIView *)view{
- NSLog(@"scrollView取消传递触摸事件到视图%@", view);
- //no - 不取消,touch事件由view处理,scrollView不滚动; yes - scrollView取消,touch事件由scrollView处理,可滚动
- return YES// NO;
- }
- @end
重写了方法
- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
和
- (BOOL)touchesShouldCancelInContentView:(UIView *)view。
(BOOL)touchesShouldBegin:withEvent:inContentView:
用户点击黄色区域时,先触发这个方法。
当返回YES时,touch事件作用到黄色视图上;
当返回no时,touch事件不传递到黄色视图,红色视图马上滚动(当然前提是scrollView的scrollEnabled字段为YES)。
(BOOL)touchesShouldCancelInContentView:
只有scrollView的canCancelContentTouches字段为YES 且 (BOOL)touchesShouldBegin:withEvent:inContentView:返回YES时,这个方法才会被调用。
调用前,由于(BOOL)touchesShouldBegin:withEvent:inContentView:返回YES,scrollView上对应的view会接收到touch事件,因此方法(BOOL)touchesShouldCancelInContentView:被调用前,view可以已经触发了touchesBegan:withEvent:方法甚至是touchesMoved:withEvent:方法,至于view最后会调用touchesEnden:withEvent:还是touchesCanceled:withEvent:取决于(BOOL)touchesShouldCancelInContentView:的返回值。
scrollView发送tracking前,调用这个方法。
返回YES时,若scrollView的canCancelContentTouches字段为YES,view会马上调用touchesCanceled:withEvent:,scrollView进行tracking以滚动。
返回NO时,scrollView不进行tracking,也不滚动,touch事件交由view处理。
二、添加mySrollView到根视图上
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- [self layoutCstmView];
- }
- - (void)layoutCstmView
- {
- myScrollView * scrollView = [[myScrollView alloc] initWithFrame:CGRectMake(10, 9, 300, 400)];
- [scrollView setUserInteractionEnabled:YES];
- [scrollView setScrollEnabled:YES];
- //NO - 设置scrollView不能取消传递touch事件,此时就算手指若在subView上滑动,scrollView不滚动; YES - 设置scrollView可取消传递touch事件
- [scrollView setCanCancelContentTouches:YES];
- [scrollView setBounces:NO];
- //NO - 立即通知touchesShouldBegin:withEvent:inContentView
- [scrollView setDelaysContentTouches:NO];
- [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width, 900)];
- [self.view addSubview:scrollView];
- [scrollView release];
- }
三、MyView视图的实现
- #import "myView.h"
- @implementation myView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- self.backgroundColor = [UIColor yellowColor];
- }
- return self;
- }
- - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
- printf("myView touchesBegan \n");
- self.backgroundColor = [UIColor grayColor];
- }
- - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
- printf("myView touchesMoved \n");
- }
- - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
- printf("myView touchesEnded \n");
- }
- - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
- printf("myView touchesCancelled \n");
- self.backgroundColor = [UIColor yellowColor];
- }
- @end
- UIScrollView的作用原理,实现scrollView传递touch事件给子视图【转】
- UIScrollView的作用原理,实现scrollView传递touch事件给子视图
- UIScrollView的作用原理,实现scrollView中touch事件作用子视图
- UIScrollView的作用原理,实现scrollView中touch事件作用子视图
- UIScrollView的作用原理,实现scrollView中touch事件作用子视图
- UIScrollView的作用原理,实现scrollView中touch事件作用子视图
- uiscrollview的touch事件 以及上面子视图touch事件 实现原理
- IOS UI学习 ScrollView中Touch事件作用子视图
- IOS UI学习 ScrollView中Touch事件作用子视图
- 传递UIScrollView的滑动事件到其子视图中
- 传递UIScrollView的滑动事件到其子视图中
- 把scrollView的touch事件传递出去
- [原创]传递UIScrollView的滑动事件到其子视图中
- uiscrollview uicollectionview touch事件传递问题
- UIScrollView的touch事件特点
- touch事件传递原理总结
- touch事件传递原理总结
- 解决Android ScrollView子视图的onTouch事件冲突
- 网上收集的部分JS脚本
- weblogic 在myeclipse中的自动部署配置问题解决示例
- Java基本线程机制(一)
- Ubuntu Tomcat7 Solr4.4 运行
- NULL
- UIScrollView的作用原理,实现scrollView传递touch事件给子视图【转】
- activex中IDispatchEx接口简介
- 解析oracle的rownum
- IOS 移动UI设计中的7种主要导航模式
- directUI的体系架构
- 免费的天气预报API--谷歌,雅虎,中央气象台
- Ubuntu 13.04 小米2S连接Eclipse真机调试
- paip.语义分析--单字动词表.txt
- 黑马程序员_java IO流