展会信息港展会大全

[cocos2d-x ]点击空白隐藏键盘
来源:互联网   发布日期:2015-09-27 07:49:26   浏览:2537次  

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

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;

int w = mTelNumber->getContentSize().width;

int h = mTelNumber->getContentSize().height;

CCRect rect = CCRect(0, -h/2, w, h);

onClickedTextField(rect.containsPoint(point));

}

} while (0);

DialogLayer::ccTouchEnded(pTouch, pEvent);

}

/** 点击推广码输入框 */

void onClickedTextField(bool b)

{

if (b) {

mTelNumber->attachWithIME();

} else {

mTelNumber->detachWithIME();

}

}

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;

int w = mTelNumber->getContentSize().width;

int h = mTelNumber->getContentSize().height;

CCRect rect = CCRect(0, -h/2, w, h);

onClickedTextField(rect.containsPoint(point));

}

} while (0);

DialogLayer::ccTouchEnded(pTouch, pEvent);

}

/** 点击推广码输入框 */

void onClickedTextField(bool b)

{

if (b) {

mTelNumber->attachWithIME();

} else {

mTelNumber->detachWithIME();

}

}

赞助本站

人工智能实验室

相关热词: cocos2d 游戏开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港