触摸由两层处理 [英] Touch handled by two layers

查看:197
本文介绍了触摸由两层处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CCLayer 包含许多其他 CCLayer (像文字项目等)。我在左侧还有另一个 CCLayer ,用于显示这些场景的缩略图。

I have a CCLayer containing a number of other CCLayers (like items of text etc). I have another CCLayer to the left hand side with which I want to display thumbnails of a number of these 'scenes'.

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

The left hand CCScrollLayer should respond to touches which are within its bounds, while elements in the right hand layer should respond to touches inside their individual bounds.

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

The problem I'm seeing is that when I drag for example a layer on the right, the CCScrollLayer to the left is responding and scrolling. The elements on the right are unaffected, when I scroll the scroll layer though. It's as though the CCScrollLayer's bounds are too big, which they're not because I've even set them purposely to 100 pixels wide. Is there an unexplained behaviour at work here?

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

推荐答案

默认情况下,CCLayer注册为标准触摸委托。您必须将其注册为目标委托。在这种情况下CCLayer可以声明触摸和其他可触摸元素将不会收到它。你可以通过覆盖CCLayer方法来实现。

By default, CCLayer is register as standard touch delegate. You must register it as targeted delegate. In this case CCLayer can claim touch and other touchable elements will not receive it. You can do it by overriding CCLayer method

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

之后,必须用这些替换您的委托方法

after this you must replace your delegate methods with these ones

- (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;
}

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

just don't forget to convert touch's UI coordinates to GL. If this method returns YES, this touch will not be received by any other layer.

这篇关于触摸由两层处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆