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);

}
时间: 2024-08-19 09:44:29

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

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__SliderInd

iOS 页面之间的传值总结

iOS 页面之间的传值总结   1.属性传值 (1): 属性传值第一步需要用到什么类型就定义什么样的属性 (2): 从上一个页面到一个页面的选中方法里面将要传的值传到来(上一个页面)备注:这种方法只适用于上一个页面推到下一个页面. 如:MainViewController与SecondViewController两个视图控制器,点击MainViewController中的按钮将跳转到SecondViewController视图,同时想要传递一个值过去.这时可以利用属性传值. 首先SecondVi

iOS页面间传值的六种方式

一般ios页面间的传值方式分为6种:1.属性传值:2.block:3.delegate:4.UserDefault:5.单例:6.通知. 0&1.block 先说我最常用的block吧,属性传值就很简单了,主要用于顺传,我们在这里包括下面都主要讲逆传.属性传值放在block里一起写了. 下面上代码: //secondVc.h typedef void (^TestBlock) (NSString *str); @interface ATNextViewController : UIViewCon

iOS 页面间几种传值方式(属性,代理,block,单例,通知)

iOS 页面间几种传值方式(属性,代理,block,单例,通知) 姜糖水 2015-05-03 52 阅读 iOS 移动开发 第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视图控制器的部分信息 例如:第一个界面中的lable显示第二个界面textField中的文本 这就需要使用代理传值 页面间传值有八大传值方式,下面我们就简单介绍下页面间常用的五

IOS页面UILable显示多行文本

IOS页面UILable显示多行文本 最近有遇到过多行显示文本的问题 都是使用的uitextview来做到的  处理麻烦需要关闭滚动 还有输入等响应 而且本身uitextview控件比label大几倍 偶然无疑间发现了别人博客中的一个方法 转过来分享下 UILabel*label; //设置换行label.lineBreakMode = UILineBreakModeWordWrap; label.numberOfLines = 0; 换行符还是\n比如NSString * [email pro

IOS 页面间跳转

常用的就两种 一种通过导航,一种直接跳 第一种 直接跳转 思路大致就是new一个目的页面,然后设置下页面跳转动画 中间还可以做点目的页面的数据初始化: ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView"bundle:[NSBundle mainBundle]]; valueView.delegate = self; [valueView setModalTransit

iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)

iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notification方式:3.block方式:4.UserDefault或者文件方式:5.单例模式方式:6.通过设置属性,实现页面间传值 在iOS开发中,我们经常会遇到页面间跳转传值的问题,现归纳总结一下: 情况1:A页面跳转到B页面 方法: 在B页面的控制器中,编写对应的属性,在A页面跳转到B页面的地方,给

iOS 页面间传值 之 单例传值 , block 传值

ios 页面间传值有许多,前边已经分享过属性传值和代理传值,今天主要说一下单例传值和 block 传值 单例传值:单例模式一种常用的开发的模式,单例因为在整个程序中无论在何时初始化对象,获取到的都是同一个对象,对象的属性相同,所以可以用来传值. block 传值 与 代理传值相似,主要用于第二个页面向第一个页面传值,block 传值具体步骤: 在第二个页面: 1.声明: block typedef void(^SendMessagerBlock) (NSString *str); 2.创建方法:

struts下ajax提交与页面进行提示 返回值为null

@Override    public String execute() throws Exception {        if ("none".equals(task)) {            HttpServletResponse response = ServletActionContext.getResponse();            response.setContentType("text/html;charset=GBK"); PrintW