1.获取ipad平台下屏幕适配过程中添加的下面那张图的高度,保存到值posY中
#include"user_default/CCUserDefault.h"
void SystemData::addAppFrame()
{
CCEGLView* eglView = CCDirector::sharedDirector()->getOpenGLView();
CCSize frameSize = eglView->getFrameSize();
float scaleX = eglView->getScaleX();
float scaleY = eglView->getScaleY();
CCRect sc_rect = CCRectMake((frameSize.width - SystemData::size_x * scaleX) /2.0f,
(frameSize.height - SystemData::size_y * scaleY) /2.0f,
SystemData::size_x * scaleX,
SystemData::size_y * scaleY);
float ratioX = SystemData::size_x / frameSize.width;
float ratioY = SystemData::size_y / frameSize.height;
CCRect rs_rect = CCRectMake(sc_rect.getMinX() * ratioX,
sc_rect.getMinY() * ratioY,
sc_rect.size.width * ratioX,
sc_rect.size.height * ratioY);
CCSprite* imgTop = CCSprite::create("data-a/ui/unplist/appframe.jpg");
CCSprite* imgBottom = CCSprite::create("data-a/ui/unplist/appframe.jpg");
if(!imgTop||!imgBottom)return;
CCDirector* director = CCDirector::sharedDirector();
director->addRenderExtra(imgTop);
director->addRenderExtra(imgBottom);
imgBottom->setFlipY(true);
imgTop->setAnchorPoint(ccp(0.5,0));
imgBottom->setAnchorPoint(ccp(0.5,1));
imgTop->setPosition(ccp(rs_rect.getMidX(), rs_rect.getMaxY()-1));
imgBottom->setPosition(ccp(rs_rect.getMidX(), rs_rect.getMinY()));
//保存PosY的值
CCUserDefault::CCUserDefault::sharedUserDefault()->setFloatForKey("ipadAppFrameY",rs_rect.getMinY());
}
2.CCTextFieldTTF中重写keyboardWillShow和keyboardWillHide
#include"CCScene.h"
#include"CCApplication.h"
#include"CCUserDefault.h"
static CCRect getRect(CCNode * pNode)
{
CCSize contentSize = pNode->getContentSize();
CCRect rect = CCRectMake(0,0, contentSize.width, contentSize.height);
return CCRectApplyAffineTransform(rect, pNode->nodeToWorldTransform());
}
void CCTextFieldTTF::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)
{
// CCLOG("CCEditBox::keyboardWillShow");
CCRect rectTracked = getRect(this);
// some adjustment for margin between the keyboard and the edit box.
rectTracked.origin.y -= 4;
// if the keyboard area doesn't intersect with the tracking node area, nothing needs to be done.
if (!rectTracked.intersectsRect(info.end))
{
CCLOG("needn't to adjust view layout.");
return;
}
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
int m_fAdjustHeight = info.end.getMaxY() - rectTracked.getMinY();
// CCLOG("CCEditBox:needAdjustVerticalPosition(%f)", m_fAdjustHeight);
CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
TargetPlatform target = CCApplication::sharedApplication()->getTargetPlatform();//获取当前设备类型
if (target == kTargetIpad)//如果是Ipad
scene->setPosition(CCPoint(0,m_fAdjustHeight-CCUserDefault::CCUserDefault::sharedUserDefault()->getFloatForKey("ipadAppFrameY")));
else
scene->setPosition(CCPoint(0,m_fAdjustHeight));
}
void CCTextFieldTTF::keyboardWillHide(CCIMEKeyboardNotificationInfo& info)
{
CCScene *scene = CCDirector::sharedDirector()->getRunningScene();
scene->setPosition(CCPoint(0,0));
}
3.实现点击空白处键盘隐藏功能
这个在网上查到一些方法。下面先介绍一下。
第一个:
想实现这么一种文本输入框,用户点击到输入框时弹出键盘,如果要关闭键盘,触摸其他的空白区域就可以了。打开和关闭键盘很简单,调用attachIME和deattachIME。但是发现键盘打开后再触摸界面,layer就不响应触摸了,这样就没法判断何时关闭键盘~
看了一下源码,发现在2.x中,EAGLView.mm文件跟1.0不一样,有这么一个判断:
if(isKeyboardShown_)
{
[self handleTouchesAfterKeyboardShow];
return;
}
当打开键盘的时候屏蔽了其他的触摸输入,就不能监听到层的触摸了。
[self handleTouchesAfterKeyboardShow];就是它的问题,把这行代码注释掉就ok了,而且触摸移动、抬起、取消 里相应的判断都给注释了。
这样就可以通过触摸判断有没有触摸到空白区域来关闭键盘了~
第二个:
cocos2dx edit编辑框点击后显示一个键盘,但是非常的不灵活,点return才能隐藏,如果我们需要点键盘外的背景,实现隐藏键盘,那就方便多了
方法:
1. 到EGLView.mm下 注释touchesBegan和touchesEnded的2个reurn,这样就能保证显示软键盘的时候,还能将点击事件传送到最底层
2.最底层的layer类中添加处理:显示和隐藏键盘就OK了
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();
}
}
这两种现在都不能完成现在的功能,需要做的就是将detachWithIME()实现的键盘隐藏功能和场景恢复拿出来,实现于是将touchesEnded方法做如下修改
if (isKeyboardShown_)
{
// bool bRet = cocos2d::CCIMEDispatcher::sharedDispatcher()->detachDelegateWithIME(this);
// if (bRet)
// {
// close keyboard
cocos2d::CCEGLView * pGlView =cocos2d::CCDirector::sharedDirector()->getOpenGLView();
if (pGlView)
{
pGlView->setIMEKeyboardState(false);
}
// }
cocos2d::CCScene *scene =cocos2d::CCDirector::sharedDirector()->getRunningScene();
scene->setPosition(cocos2d::CCPoint(0,0));
return;
}