设置标签 ‘Touch’

Cocos2D for iPhone 的触摸事件

除了 Layer 可以接受触摸事件, 在Cocos2D 0.8以后加入一个新的特性,从而让所有的对象都可以接受触摸事件. 发现大家都不怎么用这个方法,这儿简单介绍一下.

首先添加事件接收者:

[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];

//self为接收者, 优先级参数从0开始 数字越小优先级越高,就会越先接收到事件, 最后一个参数表示是否阻止此次事件冒泡

然后实现3个方法:

#pragma mark TouchDispatcherDelegate

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

//你的代码

return YES; //这儿如果返回NO 此次触摸将被忽略

}

- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event

{

//你的代码

}

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{

//你的代码

}

这样,就可以像处理UIView里的事件一样去处理coco2d了.

Enjoy ur coding :)

Edit: 别忘了删除监听者, 要不然……

[[TouchDispatcher sharedDispatcher] removeDelegate:self];

Suspend or Ignore Touch Events

There are times when you may need to temporarily stop receiving touch events. For example, when doing an animation or a screen transition rather than disable user enabled views (buttons, images, etc), you can tell the application to ignore user interaction events as shown below:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

Turning event interaction back on is as follows:

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

[From Suspend or Ignore Touch Events]

回到顶部

关于我:

  • iPhone 开发者. 自由职业者.
  • 苹果忠实用户. 完美主义者.
  • Email/iChat/MSN/GTalk: i@imi.im
  • Twitter/Sina: @TraWor
  •  

    Switch to our mobile site