[cocos2d-x ]点击空白隐藏键盘
cocos2dx edit编辑框点击后显示一个键盘,但是非常的不灵活,点return才能隐藏,如果我们需要点键盘外的背景,实现隐藏键盘,那就方便多了
方法:
1. 到EGLView.mm下 注释2个reurn,这样就能保证显示软键盘的时候,还能将点击事件传送到最底层
[cpp]
// 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);
}
// 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);
}
[cpp]
(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);
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (isKeyboardShown_)
{
//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()->handleTouchesEnd(i, ids, xs, ys);
}
2.最底层的layer类中添加处理:显示和隐藏键盘就OK了
[cpp]
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;
&n
补充:移动开发 , 其他 ,