ccui.pageView基础应用

var PageView = cc.Layer.extend({
    ctor : function(){
        this._super();

        // 案例一
        this.demoOne();

        return true;
    },

    demoOne : function(){
        var pageView = new ccui.PageView();
        pageView.setTouchEnabled(true);
        pageView.setContentSize(cc.size(500, 400));
        pageView.x = (cc.winSize.width  - 500) / 2;
        pageView.y = (cc.winSize.height - 400) / 2;

        for(var i = 0; i < 3; i++){
     
            var layout = new ccui.Layout(); // layout 的type默认为ccui.Layout.ABSOLUTE
            layout.setContentSize(cc.size(500, 400));
            var layoutRect = layout.getContentSize();

            var imgView = new ccui.ImageView();
            imgView.setTouchEnabled(true);
            imgView.setScale9Enabled(true);
            imgView.loadTexture(res.imgBox);
            imgView.setContentSize(cc.size(500, 400));
            imgView.x = layoutRect.width  / 2;
            imgView.y = layoutRect.height / 2;
            layout.addChild(imgView);

            var text    = new ccui.Text();
            text.string = "page" + (i + 1);
            text.font   = "30px'Marker Felt'";
            text.color  = cc.color(192, 192, 192);
            text.x      = layoutRect.width  / 2;
            text.y      = layoutRect.height / 2;
            layout.addChild(text);

            pageView.addWidgetToPage(layout, i, true);
        }

        pageView.scrollToPage(0);  // 调到指定页面,可用作点击按钮进行翻页
        pageView.addEventListenerPageView(this.pageViewEvent, this);
        this.addChild(pageView);
    },

    // 响应拖动,点击事件
    pageViewEvent : function(sender, type){
        cc.log("type = ", type);
        switch(type){
            case ccui.PageView.EVENT_TURNING   :
                var pageView = sender;
                cc.log("EVENT_TURNING page = " + (pageView.getCurPageIndex().valueOf() - 0 + 1));
                break;
            default : cc.log("none");
        }
    }
});
  • 说明:再编程过程中,发现ccui.pageView只有getCurPageIndex函数,并没有setCurPageIndex,本人用的是cocos2d-js-v3.0版本,但是官方给的案例中是直接用的setCurPageIndex,官方demo为cocos2d-x-3版本,遇到此问题,可以不设置pageIndex,或者使用scrollToPage来设置,或者默认展示的第一页为代码创建的第一页

你可能感兴趣的:(cocos2d-js,cocos2d-js,PageView)