码迷,mamicode.com
首页 > 移动开发 > 详细

Cocos2d-x3.1下实现相似iOS页面滑动指示圆点

时间:2015-05-06 19:36:04      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

原文地址: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);

}




Cocos2d-x3.1下实现相似iOS页面滑动指示圆点

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4482842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!