Cocos2d-x3.1下实现相似iOS页面滑动指示圆点
原文地址:http://blog.csdn.net/qqmcy/article/details/37612457
代码下载:http://download.csdn.net/detail/qqmcy/7613835
SliderIndicatorLayout.h
// // SliderIndicatorLayout.h // ht_mobile_cpp // // Created by 杜甲 on 14-7-9. // // #ifndef __ht_mobile_cpp__SliderIndicatorLayout__ #define __ht_mobile_cpp__SliderIndicatorLayout__ #include "cocos2d.h" #include "ui/CocosGUI.h" USING_NS_CC; class SliderIndicatorLayout:public ui::Layout { public: CREATE_FUNC(SliderIndicatorLayout); virtual bool init(); //加入指示圆点个数 void addIndicator(int num); //选中的第几个 void changeIndicator(int index); private: Size winSize; float radius; }; #endif /* defined(__ht_mobile_cpp__SliderIndicatorLayout__) */
SliderIndicatorLayout.cpp
// // SliderIndicatorLayout.cpp // ht_mobile_cpp // // Created by 杜甲 on 14-7-9. // // #include "SliderIndicatorLayout.h" #include "SliderIndicator.h" bool SliderIndicatorLayout::init() { bool bRet = false; do { CC_BREAK_IF(!ui::Layout::init()); setLayoutType(cocos2d::ui::Layout::Type::VERTICAL); winSize = Director::getInstance()->getWinSize(); radius = winSize.height / 130; bRet = true; } while (0); return bRet; } void SliderIndicatorLayout::addIndicator(int num) { setSize(Size(radius * 2, radius*3 * num)); for (int i = 0 ; i < num; i++) { auto indicator = SliderIndicator::create(); indicator->setSize(Size(radius, radius)); indicator->setCircleColor(Color4B(255, 40, 255, 255)); indicator->setTag(i); addChild(indicator); auto lp_indicator = ui::LinearLayoutParameter::create(); lp_indicator->setGravity(cocos2d::ui::LinearLayoutParameter::LinearGravity::TOP); lp_indicator->setMargin(ui::Margin(0,radius * 2.0f,0,0)); if (i == 0) { lp_indicator->setMargin(ui::Margin(0, 0,0,0)); } indicator->setLayoutParameter(lp_indicator); } changeIndicator(0); } void SliderIndicatorLayout::changeIndicator(int index) { for (int i = 0; i < getChildren().size(); i++) { auto indicator = dynamic_cast<SliderIndicator*>(getChildByTag(i)); indicator->setCircleColor(Color4B(255, 40, 255, 25)); if (i == index) { indicator->setCircleColor(Color4B(255, 40, 255, 255)); } } }
SliderIndicator.h
// // SliderIndicator.h // ht_mobile_cpp // // Created by 杜甲 on 14-7-9. // // #ifndef __ht_mobile_cpp__SliderIndicator__ #define __ht_mobile_cpp__SliderIndicator__ #include "cocos2d.h" #include "ui/CocosGUI.h" USING_NS_CC; class SliderIndicator:public ui::Layout { public: CREATE_FUNC(SliderIndicator); virtual bool init(); CC_SYNTHESIZE(Color4B, _circleColor, CircleColor); protected: void onDraw(const Mat4 &transform, bool transformUpdated); void draw(Renderer *renderer, const Mat4 &transform, bool transformUpdated); CustomCommand _customCommand; }; #endif /* defined(__ht_mobile_cpp__SliderIndicator__) */
SliderIndicator.cpp
// // SliderIndicator.cpp // ht_mobile_cpp // // Created by 杜甲 on 14-7-9. // // #include "SliderIndicator.h" bool SliderIndicator::init() { bool bRet = false; do { CC_BREAK_IF(!ui::Layout::init()); bRet = true; } while (0); return bRet; } void SliderIndicator::draw(Renderer *renderer, const Mat4 &transform, bool transformUpdated) { _customCommand.init(_globalZOrder); _customCommand.func = CC_CALLBACK_0(SliderIndicator::onDraw, this,transform,transformUpdated); renderer->addCommand(&_customCommand); } void SliderIndicator::onDraw(const cocos2d::Mat4 &transform, bool transformUpdated) { Director* director = Director::getInstance(); CCASSERT(nullptr != director, "Director is null when seting matrix stack"); director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform); DrawPrimitives::setDrawColor4B(_circleColor.r, _circleColor.g, _circleColor.b, _circleColor.a); DrawPrimitives::drawSolidCircle( Vec2(0,0), director->getWinSize().height / 130, CC_DEGREES_TO_RADIANS(90), 50, 1.0f, 1.0f); //end draw director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。