cocos2dx之彩色编辑输入框的设计(3)
来源:未知 责任编辑:责任编辑 发表时间:2015-09-17 09:43 点击:次
然后是触摸函数的实现:
bool ColorEdit::onTouchBegan(Touch* touch, Event* pEvent) { auto touchPoint = touch->getLocation(); //如果是全屏显示类型,并且键盘已弹出 if (m_editLocation == Location_Screen && m_isKeyShow) { auto key_upEdit = (Scale9Sprite *)m_pTarget->getChildByTag(101); //如果点击除了顶层输入框和键盘的其他地方,则键盘消失 if (!m_keyBg->getBoundingBox().containsPoint(touchPoint) && !key_upEdit->getBoundingBox().containsPoint(touchPoint)) { this->moveAction(false); return true; } return false; } //将触摸点转为在当前子层下的坐标 touchPoint = this->convertToNodeSpace(touchPoint); if (!m_pEditBg->getBoundingBox().containsPoint(touchPoint)) { if (m_isKeyShow) { if (!m_keyBg->getBoundingBox().containsPoint(Vec2(touch->getLocation().x, touch->getLocation().y - (m_editLocation == Location_Down ? 1 : 0) * m_keyBg->getContentSize().height))) { this->moveAction(false); } } return false; } //如果点击了输入框,并且键盘还未弹出 if (!m_isKeyShow) { this->updateText(); this->moveAction(true); } return true; }
这里有个要注意的地方:
if (!m_keyBg->getBoundingBox().containsPoint(Vec2(touch->getLocation().x, touch->getLocation().y - (m_editLocation == Location_Down ? 1 : 0) * m_keyBg->getContentSize().height))) { this->moveAction(false); }
当键盘的弹出方式是效果图第二张时,因为背景图向上移动了一段距离,touch->getLocation()的y坐标要做一下修改,否则getBoundingBox的判断不正确。
再来看看对于键盘点击的响应函数:
void ColorEdit::onNumBtnCallback(Ref * obj) { int tag = ((Node *)obj)->getTag(); //点击按钮的简单动画 ((MenuItemSprite *)obj)->runAction(Sequence::create(ScaleTo::create(0.1,10 / 8.0),ScaleTo::create(0.1,1),NULL)); char temp[3]; sprintf(temp,%d,tag); m_text += temp; //更新内容 this->updateText(); log(keyboard------->%d,tag); } void ColorEdit::onFunBtnCallback(Ref * obj) { ((MenuItemSprite *)obj)->runAction(Sequence::create(ScaleTo::create(0.1,10 / 8.0),ScaleTo::create(0.1,1),NULL)); if (((Node *)obj)->getTag() == Key_Sure) { this->moveAction(false); } else { auto n = m_text.size(); if (n > 0) { m_text = m_text.substr(0,n - 1); } this->updateText(); } }
接着是键盘的出现与消失:
void ColorEdit::moveAction(bool isShow) { if (m_editLocation == Location_Screen) { auto keyLayer = (LayerColor *)m_pTarget->getChildByTag(100); auto key_upEdit = (Scale9Sprite *)m_pTarget->getChildByTag(101); keyLayer->setVisible(isShow); key_upEdit->runAction(MoveBy::create(0.35,Vec2(0,(isShow ? -1 : 1) * key_upEdit->getContentSize().height))); m_keyBg->runAction(MoveBy::create(0.35,Vec2(0,(isShow ? 1 : -1) * m_keyBg->getContentSize().height))); } else if (m_editLocation == Location_Down) { Director::getInstance()->getRunningScene()->runAction(MoveBy::create(0.35,Vec2(0,(isShow ? 1 : -1) * m_keyBg->getContentSize().height))); } else if (m_editLocation == Location_Nature) { m_keyBg->runAction(MoveBy::create(0.35,Vec2(0,(isShow ? 1 : -1) * m_keyBg->getContentSize().height))); } m_isKeyShow = isShow; if (!isShow) { m_text = ; } }
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>