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

来源:互联网 发布:护士资格证题库软件 编辑:程序博客网 时间:2024/05/29 09:09



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


方法:

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

[cpp] view plaincopy
  1. // Pass the touches to the superview  
  2. #pragma mark EAGLView - Touch Delegate  
  3. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  4. {  
  5.     if (isKeyboardShown_)  
  6.     {  
  7.         [self handleTouchesAfterKeyboardShow];  
  8.           
  9.         //WARNING:commented by Teng.点触背景隐藏软键盘  
  10.         //return;  
  11.     }  
  12.       
  13.     int ids[IOS_MAX_TOUCHES_COUNT] = {0};  
  14.     float xs[IOS_MAX_TOUCHES_COUNT] = {0.0f};  
  15.     float ys[IOS_MAX_TOUCHES_COUNT] = {0.0f};  
  16.       
  17.     int i = 0;  
  18.     for (UITouch *touch in touches) {  
  19.         ids[i] = (int)touch;  
  20.         xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;  
  21.         ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;  
  22.         ++i;  
  23.     }  
  24.     cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i, ids, xs, ys);  
  25. }  

[cpp] view plaincopy
  1. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event  
  2. {  
  3.     if (isKeyboardShown_)  
  4.     {  
  5.         <strong><span style="color:#ff0000;">//WARNING:commented by Teng.点触背景隐藏软键盘</span>   
  6.         //return;</strong>  
  7.     }  
  8.       
  9.     int ids[IOS_MAX_TOUCHES_COUNT] = {0};  
  10.     float xs[IOS_MAX_TOUCHES_COUNT] = {0.0f};  
  11.     float ys[IOS_MAX_TOUCHES_COUNT] = {0.0f};  
  12.       
  13.     int i = 0;  
  14.     for (UITouch *touch in touches) {  
  15.         ids[i] = (int)touch;  
  16.         xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;  
  17.         ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;  
  18.         ++i;  
  19.     }  
  20.     cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesEnd(i, ids, xs, ys);  
  21. }  



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

[cpp] view plaincopy
  1. void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  2. {  
  3.     do   
  4.     {  
  5.         if (mTelNumber) {  
  6.             CCPoint endPos = pTouch->getLocation();  
  7.   
  8.             float delta = 5.f;  
  9.             if (::abs(mBeginPos.x - endPos.x) > delta  
  10.                 || ::abs(mBeginPos.y - endPos.y) > delta) {  
  11.                     break;  
  12.             }  
  13.   
  14.             // 看编辑框是否被点中  
  15.             CCPoint point = mTelNumber->getParent()->convertTouchToNodeSpaceAR(pTouch);  
  16.   
  17.             // 锚点(0.f, 0.5f)  
  18.             //int x = mTextField->getParent()->getPosition().x;  
  19.             //int y = mTextField->getParent()->getPosition().y;  
  20.             int w = mTelNumber->getContentSize().width;  
  21.             int h = mTelNumber->getContentSize().height;  
  22.             CCRect rect = CCRect(0, -h/2, w, h);  
  23.   
  24.             onClickedTextField(rect.containsPoint(point));  
  25.         }  
  26.     } while (0);  
  27.   
  28.     DialogLayer::ccTouchEnded(pTouch, pEvent);  
  29. }  
  30.   
  31. /** 点击推广码输入框 */  
  32. void onClickedTextField(bool b)  
  33. {  
  34.     if (b) {  
  35.         mTelNumber->attachWithIME();  
  36.     } else {  
  37.         mTelNumber->detachWithIME();  
  38.     }  
  39. }  

转载自:http://blog.csdn.net/teng_ontheway/article/details/9162781
0 0
原创粉丝点击