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

前端之家收集整理的这篇文章主要介绍了【COCOS2DX-游戏开发之八】点击空白隐藏键盘前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://blog.csdn.net/teng_ontheway/article/details/9162781


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

cocos2dx隐藏键盘点击空白隐藏键盘

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


方法

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

[cpp] view plaincopy

  1. //Passthetouchestothesuperview@H_502_50@
  2. #pragmamarkEAGLView-TouchDelegate@H_502_50@
  3. -(void)touchesBegan:(NSSet*)toucheswithEvent:(UIEvent*)event@H_502_50@
  4. {@H_502_50@
  5. if(isKeyboardShown_)@H_502_50@
  6. {@H_502_50@
  7. [selfhandleTouchesAfterKeyboardShow];@H_502_50@
  8. @H_502_50@
  9. //WARNING:commentedbyTeng.点触背景隐藏软键盘@H_502_50@
  10. //return;@H_502_50@
  11. }@H_502_50@
  12. @H_502_50@
  13. intids[IOS_MAX_TOUCHES_COUNT]={0};@H_502_50@
  14. floatxs[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
  15. floatys[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
  16. @H_502_50@
  17. inti=0;@H_502_50@
  18. for(UITouch*touchintouches){@H_502_50@
  19. ids[i]=(int)touch;@H_502_50@
  20. xs[i]=[touchlocationInView:[touchview]].x*view.contentScaleFactor;;@H_502_50@
  21. ys[i]=[touchlocationInView:[touchview]].y*view.contentScaleFactor;;@H_502_50@
  22. ++i;@H_502_50@
  23. }@H_502_50@
  24. cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i,ids,xs,ys);@H_502_50@
  25. }@H_502_50@


[cpp] view plaincopy

  1. -(void)touchesEnded:(NSSet*)toucheswithEvent:(UIEvent*)event@H_502_50@
  2. {@H_502_50@
  3. if(isKeyboardShown_)@H_502_50@
  4. {@H_502_50@
  5. <strong><spanstyle="color:#ff0000;">//WARNING:commentedbyTeng.点触背景隐藏软键盘</span>@H_502_50@
  6. //return;</strong>@H_502_50@
  7. }@H_502_50@
  8. @H_502_50@
  9. intids[IOS_MAX_TOUCHES_COUNT]={0};@H_502_50@
  10. floatxs[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
  11. floatys[IOS_MAX_TOUCHES_COUNT]={0.0f};@H_502_50@
  12. @H_502_50@
  13. inti=0;@H_502_50@
  14. for(UITouch*touchintouches){@H_502_50@
  15. ids[i]=(int)touch;@H_502_50@
  16. xs[i]=[touchlocationInView:[touchview]].x*view.contentScaleFactor;;@H_502_50@
  17. ys[i]=[touchlocationInView:[touchview]].y*view.contentScaleFactor;;@H_502_50@
  18. ++i;@H_502_50@
  19. }@H_502_50@
  20. cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesEnd(i,ys);@H_502_50@
  21. }@H_502_50@




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

[cpp] view plaincopy

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






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

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

猜你在找的Cocos2d-x相关文章