Cocos2d-x游戏开发之2.x后弹出键盘后无法响应除键盘外的触摸事件解决

来源:互联网 发布:淘宝上pets3包过骗局 编辑:程序博客网 时间:2024/05/07 19:06

此博客基于Cocos2d-2.1rc0-x-2.1.3  API

本站文章转载务必在明显处注明:原文链接: http://blog.csdn.net/cjsen/article/details/9029865

问题

在使用自定义输入框时,有个功能是点击输入框内弹出键盘,触摸外面隐藏键盘,但实例在cocos2d-x 1.x版本上没有问题的,但在2.x版本后却在出现键盘后没有调用TouchBegin事件,无法响应除键盘外的触摸事件。。。

网上找了些资料,看到了在cocos2d-x源码的EAGLView.mm文件上的响应触摸开始事件上

#pragma mark EAGLView - Touch Delegate- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    if (isKeyboardShown_)    {        [self handleTouchesAfterKeyboardShow];        return;    }        int ids[IOS_MAX_TOUCHES_COUNT] = {0};    float xs[IOS_MAX_TOUCHES_COUNT] = {0.0f};    float ys[IOS_MAX_TOUCHES_COUNT] = {0.0f};        int i = 0;    for (UITouch *touch in touches) {        ids[i] = (int)touch;        xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;        ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;        ++i;    }    cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i, ids, xs, ys);}
相比1.x版本多了一个条件判断

    if (isKeyboardShown_){

        [selfhandleTouchesAfterKeyboardShow];

        return;

    }

在相应的touchesMoved, touchesEnded,touchesCancelled判断

    if (isKeyboardShown_)

    {

        return;

    }

这就是为什么出现键盘后无法响应其他事件。

而看看touchesBegan上的

[selfhandleTouchesAfterKeyboardShow];方法

-(void) handleTouchesAfterKeyboardShow{    NSArray *subviews = self.subviews;        for(UIView* view in subviews)    {        if([view isKindOfClass:NSClassFromString(@"CustomUITextField")])        {            if ([view isFirstResponder])            {                [view resignFirstResponder];                return;            }        }    }}
其中CustomUITextField是继承UITextField,这里只是在键盘弹出状态时,并且屏幕上有CustomUITextField控件时自动隐藏键盘。

所以想在键盘弹出后仍然响应其他触摸事件的话可以直接将判断语句注释掉。。(如果有其他方法,请高手在此回复留言)

ps:在cocos2d-x 2.x之后的版本新添加的CCEditBox输入框,功能十分完善,使用方法也简单:

    CCEditBox *m_pEditName = CCEditBox::create(CCSize(200, 35), CCScale9Sprite::create("btn_right_white.9.png"));    m_pEditName->setPosition(ccp(size.width/2, size.height/2 + 100));    m_pEditName->setFontColor(ccRED);    m_pEditName->setPlaceHolder("Name:");    m_pEditName->setMaxLength(8);    m_pEditName->setReturnType(kKeyboardReturnTypeDone);    m_pEditName->setDelegate(this);    addChild(m_pEditName);





原创粉丝点击