http://blog.csdn.net/teng_ontheway/article/details/9162781
cocos2dx edit编辑框点击后显示一个键盘,但是非常的不灵活,点return才能隐藏,如果我们需要点键盘外的背景,实现隐藏键盘,那就方便多了
方法:
1. 到EGLView.mm下 注释2个reurn,这样就能保证显示软键盘的时候,还能将点击事件传送到最底层
[cpp] view plaincopy
- //Passthetouchestothesuperview@H_502_50@
- #pragmamarkEAGLView-TouchDelegate@H_502_50@
- -(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event@H_502_50@
- {@H_502_50@
- if(isKeyboardShown_)@H_502_50@
- {@H_502_50@
- [selfhandleTouchesAfterKeyboardShow];@H_502_50@
- @H_502_50@
- //WARNING:commentedbyTeng.点触背景隐藏软键盘@H_502_50@
- //return;@H_502_50@
- }@H_502_50@
- @H_502_50@
- intids[IOS_MAX_TOUCHES_COUNT]={0};@H_502_50@
- floatxs[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
- floatys[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
- @H_502_50@
- inti=0;@H_502_50@
- for(UITouch*touchintouches){@H_502_50@
- ids[i]=(int)touch;@H_502_50@
- xs[i]=[touchlocationInView:[touchview]].x*view.contentScaleFactor;;@H_502_50@
- ys[i]=[touchlocationInView:[touchview]].y*view.contentScaleFactor;;@H_502_50@
- ++i;@H_502_50@
- }@H_502_50@
- cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i,ids,xs,ys);@H_502_50@
- }@H_502_50@
[cpp] view plaincopy
- -(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event@H_502_50@
- {@H_502_50@
- if(isKeyboardShown_)@H_502_50@
- {@H_502_50@
- <strong><spanstyle="color:#ff0000;">//WARNING:commentedbyTeng.点触背景隐藏软键盘</span>@H_502_50@
- //return;</strong>@H_502_50@
- }@H_502_50@
- @H_502_50@
- intids[IOS_MAX_TOUCHES_COUNT]={0};@H_502_50@
- floatxs[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
- floatys[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
- @H_502_50@
- inti=0;@H_502_50@
- for(UITouch*touchintouches){@H_502_50@
- ids[i]=(int)touch;@H_502_50@
- xs[i]=[touchlocationInView:[touchview]].x*view.contentScaleFactor;;@H_502_50@
- ys[i]=[touchlocationInView:[touchview]].y*view.contentScaleFactor;;@H_502_50@
- ++i;@H_502_50@
- }@H_502_50@
- cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesEnd(i,ys);@H_502_50@
- }@H_502_50@
[cpp] view plaincopy
- voidccTouchEnded(cocos2d::CCTouch*pTouch,cocos2d::CCEvent*pEvent)@H_502_50@
- {@H_502_50@
- do@H_502_50@
- {@H_502_50@
- if(mTelNumber){@H_502_50@
- CCPointendPos=pTouch->getLocation();@H_502_50@
- @H_502_50@
- floatdelta=5.f;@H_502_50@
- if(::abs(mBeginPos.x-endPos.x)>delta@H_502_50@
- ||::abs(mBeginPos.y-endPos.y)>delta){@H_502_50@
- break;@H_502_50@
- }@H_502_50@
- @H_502_50@
- //看编辑框是否被点中@H_502_50@
- CCPointpoint=mTelNumber->getParent()->convertTouchToNodeSpaceAR(pTouch);@H_502_50@
- @H_502_50@
- //锚点(0.f,0.5f)@H_502_50@
- //intx=mTextField->getParent()->getPosition().x;@H_502_50@
- //inty=mTextField->getParent()->getPosition().y;@H_502_50@
- intw=mTelNumber->getContentSize().width;@H_502_50@
- inth=mTelNumber->getContentSize().height;@H_502_50@
- CCRectrect=CCRect(0,-h/2,w,h);@H_502_50@
- @H_502_50@
- onClickedTextField(rect.containsPoint(point));@H_502_50@
- }@H_502_50@
- }while(0);@H_502_50@
- @H_502_50@
- DialogLayer::ccTouchEnded(pTouch,pEvent);@H_502_50@
- }@H_502_50@
- @H_502_50@
- /**点击推广码输入框*/@H_502_50@
- voidonClickedTextField(boolb)@H_502_50@
- {@H_502_50@
- if(b){@H_502_50@
- mTelNumber->attachWithIME();@H_502_50@
- }else{@H_502_50@
- mTelNumber->detachWithIME();@H_502_50@
- }@H_502_50@
- }@H_502_50@
参考文章:Cocos2d-x游戏开发之2.x后弹出键盘后无法响应除键盘外的触摸事件解决
参考文章:[iOS] UITextField隐藏软键盘心得(隐藏自身软键盘、点击Return自动转到下个文本框、轻触背景隐藏软键盘)