【COCOS2DX-游戏开发之八】点击空白隐藏键盘

来源:互联网 发布:邪恶gif啪啪啪软件 编辑:程序博客网 时间:2024/05/22 13:54



cocos2dx edit编辑框点击后显示一个键盘,但是非常的不灵活,点return才能隐藏,如果我们需要点键盘外的背景,实现隐藏键盘,那就方便多了


方法:

1. 到EGLView.mm下 注释2个reurn,这样就能保证显示软键盘的时候,还能将点击事件传送到最底层

// Pass the touches to the superview#pragma mark EAGLView - Touch Delegate- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    if (isKeyboardShown_)    {        [self handleTouchesAfterKeyboardShow];                //WARNING:commented by Teng.点触背景隐藏软键盘        //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);}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    if (isKeyboardShown_)    {        <strong><span style="color:#ff0000;">//WARNING:commented by Teng.点触背景隐藏软键盘</span>         //return;</strong>    }        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()->handleTouchesEnd(i, ids, xs, ys);}



2.最底层的layer类中添加处理:显示和隐藏键盘就OK了

void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){    do     {        if (mTelNumber) {            CCPoint endPos = pTouch->getLocation();            float delta = 5.f;            if (::abs(mBeginPos.x - endPos.x) > delta                || ::abs(mBeginPos.y - endPos.y) > delta) {                    break;            }            // 看编辑框是否被点中            CCPoint point = mTelNumber->getParent()->convertTouchToNodeSpaceAR(pTouch);            // 锚点(0.f, 0.5f)            //int x = mTextField->getParent()->getPosition().x;            //int y = mTextField->getParent()->getPosition().y;            int w = mTelNumber->getContentSize().width;            int h = mTelNumber->getContentSize().height;            CCRect rect = CCRect(0, -h/2, w, h);            onClickedTextField(rect.containsPoint(point));        }    } while (0);    DialogLayer::ccTouchEnded(pTouch, pEvent);}/** 点击推广码输入框 */void onClickedTextField(bool b){    if (b) {        mTelNumber->attachWithIME();    } else {        mTelNumber->detachWithIME();    }}





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

参考文章:[iOS] UITextField隐藏软键盘心得(隐藏自身软键盘、点击Return自动转到下个文本框、轻触背景隐藏软键盘)