(Standard Touch Delegate 和 Targeted Touch Delegate)CCLayer中的两种Touch类型
来源:互联网 发布:h3交换机 网络聚合命令 编辑:程序博客网 时间:2024/05/16 01:40
Cocos2d 开发中提供了两种touch处理方式,Standard Touch Delegate和 Targeted Touch Delegate方式(参见CCTouchDelegateProtocol.h中源代码),CCLayer默认是采用第一种方式(参见CCLayer的 registerWithTouchDispatcher方法)。
在CCLayer子类中要能接收touch事件,首先需要激活touch支持,在init方法中设置isTouchEnabled值为YES。
Standard Touch Delegate(CCLayer默认采纳这种方式)
Standard方法中用户需要重载四个基本的touch处理方法,如下:
- -(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
当touch事件发生时,会调用该方法响应touch事件。如果是单点touch,则只需要调用 UITouch *touch = [touches anyObject],就可以获取touch对象;如果需要响应多点 touch,则需要调用[[event allTouches] allObjects]返回一个UITouch的NSArray对象,然后使用NSArray的objectAtIndex依次访问各个UITouch对象。
为了获取UITouch对象的坐标(假设该UITouch名称为touch),调用[touch locationInView: [ touch view]]会返回一个UIView相关的坐标viewPoint。
使用Cocos2d的新建应用程序向导创建一个新的cocos2d application时,在xxxAppDelegate类的applicationDidFinishLaunching方法中CCDirector会将UIView转换为支持OpenGL ES的EAGLView。此时,我们还需要将前面获取的UIView中的viewPoint转换为EAGLView坐标,调用[[CCDirector sharedDirector] convertToGL: viewPoint]即可实现。
- -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- -(void) ccTouchesCancelled:(NSSet*)touch withEvent:(UIEvent *)event;
这三个方法和ccTouchesBegan类似。
Targeted Touch Delegate方式
在standard方式中的响应处理事件处理的都是NSSet,而 targeted方式只处理单个的UITouch对象,在多点触摸条件下,应该采纳standard方式。在使用targeted方式之前需要重写CCLayer中的registerWithTouchDispatcher方法:
- //记得在头文件中导入“CCTouchDispatcher.h”
- -(void) registerWithTouchDispatcher {
- [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
- }
targeted方式中用户需要重载4个基本的处理方法,其中ccTouchBegan必须重写,其他三个是可选的。
- - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; (必须实现)
- - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
每次touch事件发生时,先调用ccTouchBegan方法,该方法对每个UITouch进行响应并返回一个BOOL值,若为YES,则后续的ccTouchMoved、ccTouchEnabled和ccTouchCancelled才会接着响应。
多点触摸支持
在xxxAppDelegate类的applicationDidFinishLaunching方法中加入下面代码
- [glView setMultipleTouchEnabled:YES];
addTargetedDelegate:self中的swallowsTouches
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:kCCMenuTouchPriority swallowsTouches:YES];
如果 swallowsTouches:YES && touch begin return yes
那么他的move 和end就接受,,别的类就不再接受了。
如果swallowsTouches:NO &&begin return yes
那么他的move 和end就接受,别的类就仍然可以接受。
- (Standard Touch Delegate 和 Targeted Touch Delegate)CCLayer中的两种Touch类型
- CCLayer中Touch事件(Standard Touch Delegate和Targeted Touch Delegate)
- Cocos2d CCLayer中的touch处理
- cocos2d学习-CCLayer中的touch
- Cocos2D-iphone CCLayer中的两种触摸(touch)处理方式
- CCLayer中Touch事件
- touch
- touch
- Touch
- TOUCH
- touch & >
- touch
- touch
- Touch
- touch
- touch
- touch
- touch
- ftp的port和pasv模式
- KMP字符串模式匹配详解
- table表格的制作
- Linux fork 函数详解
- ORA-01403:no data found 及 select a into b 空值
- (Standard Touch Delegate 和 Targeted Touch Delegate)CCLayer中的两种Touch类型
- 转]log4cxx以时间,大小的备份
- UI设计师必读的13篇文章
- java中readline方法会自动去除换行符
- [转载] 上海交通大学计算机系2013届研究生offer汇总
- STL_map用法
- 十元的耳塞到万元耳塞,之间到底有什么区别
- 99乘法
- 简单日期控件的制作