Had the same issue - the solution is to stop the opengl layer from rendering while this is happening. Here’s what I did:
1) Register for keyboard frame change events at start of application. I did this in AppController.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:nil];//UIKeyboardWillChangeFrameNotification
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardDidChangeFrame:)
name:UIKeyboardDidChangeFrameNotification
object:nil];
...
}
2) Started and stopped rendering during keyboard animation.
-(void)keyboardWillChangeFrame:(NSNotification *)notification {
NSLog(@"keyboardWillChangeFrame");
// stop openGL animation while animating keyboard
cocos2d::CCDirector::sharedDirector()->stopAnimation();
}
-(void)keyboardDidChangeFrame:(NSNotification *)notification {// restart openGL animation when keyboard stops animating
cocos2d::CCDirector::sharedDirector()->startAnimation();
}
上面是从google上搜出来的答案,百度找了很久没找到答案。 :《 google经常不好用。。。
okay,到目前为止,键盘分拆,浮动,收回动作可以完美展现。但是出现了一点瑕疵,键盘收起时,会出现输入框中的字有闪烁,以挑剔的眼光来说,这是个问题:
看上面代码,在键盘出现变化时,停止所有的动画,键盘结束时继续,debug后发现就是在动画停止的时候文字消失,开始动画后出现。判断是因为控件儿在停止编辑事件/键盘收起事件中有重新设置文字信息。
经过查找:在CCEditBoxImplIOS.mm中发现了---
- (BOOL)textFieldShouldEndEditing:(UITextField *)sender
{
CCLOG("textFieldShouldEndEditing...");
editState_ = NO;
getEditBoxImplIOS()->setText(getEditBoxImplIOS()->getText());
cocos2d::extension::CCEditBoxDelegate* pDelegate = getEditBoxImplIOS()->getDelegate();
if (pDelegate != NULL)
{
pDelegate->editBoxEditingDidEnd(getEditBoxImplIOS()->getCCEditBox());
pDelegate->editBoxReturn(getEditBoxImplIOS()->getCCEditBox());
}
cocos2d::extension::CCEditBox* pEditBox= getEditBoxImplIOS()->getCCEditBox();
if (NULL != pEditBox && 0 != pEditBox->getScriptEditBoxHandler())
{
cocos2d::CCScriptEngineProtocol* pEngine = cocos2d::CCScriptEngineManager::sharedManager()->getScriptEngine();
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "ended",pEditBox);
pEngine->executeEvent(pEditBox->getScriptEditBoxHandler(), "return",pEditBox);
}
if(editBox_ != nil)
{
// getEditBoxImplIOS()->onEndEditing(); //就是这里,onEndEditing重新给设置内容了,似乎内容没有任何的变化
}
return YES;
}
void CCEditBoxImplIOS::onEndEditing()
{
m_systemControl.textField.hidden = YES;
if(strlen(getText()) == 0)
{
m_pLabel->setVisible(false);
m_pLabelPlaceHolder->setVisible(true);
}
else
{
m_pLabel->setVisible(true);
m_pLabelPlaceHolder->setVisible(false);
setInactiveText(getText());
}
}
暂时还没有发现有什么问题,欢迎大家提意见!