【问题标题】:Touch handled by two layers触摸由两层处理
【发布时间】:2012-06-15 18:24:58
【问题描述】:

我有一个CCLayer,其中包含许多其他CCLayers(如文本项等)。我在左侧有另一个CCLayer,我想用它来显示其中一些“场景”的缩略图。

左手CCScrollLayer 应响应其边界内的触摸,而右手层中的元素应响应其各自边界内的触摸。

我看到的问题是,当我在右侧拖动一个图层时,左侧的CCScrollLayer 正在响应并滚动。当我滚动滚动层时,右侧的元素不受影响。好像CCScrollLayer 的边界太大了,这不是因为我什至故意将它们设置为 100 像素宽。工作中是否有无法解释的行为?

效果可见http://imageshack.us/photo/my-images/210/dragd.png/

【问题讨论】:

    标签: iphone ios cocos2d-iphone cclayer


    【解决方案1】:

    默认情况下,CCLayer 注册为标准触摸代理。您必须将其注册为目标代表。在这种情况下,CCLayer 可以声明触摸,而其他可触摸元素将不会收到它。您可以通过覆盖 CCLayer 方法来做到这一点

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

    在此之后,您必须将您的委托方法替换为这些方法

    - (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,则任何其他层都不会收到此触摸。

    【讨论】:

    【解决方案2】:

    谢谢@Morion,就是这样。我的检测方法是这样的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2011-02-18
      • 2012-10-26
      相关资源
      最近更新 更多