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


[cpp] view plaincopy

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




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

[cpp] view plaincopy

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






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

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

原文链接:https://www.f2er.com/cocos2dx/344127.html

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