CAPageView(頁(yè)面切換)

2018-09-08 15:36 更新

類說(shuō)明

CAPageView用于實(shí)現(xiàn)分頁(yè)、翻頁(yè)效果。CAPageView為我們提供了橫向和豎直兩個(gè)方向的樣式。我也可以通過(guò)繼承CAPageViewDelegate來(lái)實(shí)現(xiàn)對(duì)CAPageView的監(jiān)聽(tīng)。


CAPageView 屬性(點(diǎn)擊查看方法介紹)

屬性說(shuō)明
PageViewDirection頁(yè)面瀏覽方向
PageViewDelegate頁(yè)面視圖代表
CurrPage當(dāng)前頁(yè)
Spacing間距


CAPageView 方法(點(diǎn)擊查看方法介紹)

方法說(shuō)明
createWithFrame創(chuàng)建,并指定其Frame,默認(rèn)Frame為(0,0,0,0)
createWithCenter創(chuàng)建,并指定其Center,默認(rèn)Center為(0,0,0,0)
setCurrPage設(shè)置顯示指定頁(yè)
getPageCount獲取頁(yè)面總數(shù)
setViews添加存放View的CADeque容器
setViews添加存放View的CAVector容器
getSubViewAtIndex通過(guò)索引獲取子視圖
setShowsScrollIndicators設(shè)置顯示滾動(dòng)條
init
初始化


我來(lái)看一下代碼實(shí)例:
首先我們需要繼承CAPageViewDelegate實(shí)現(xiàn)監(jiān)聽(tīng)函數(shù)。我們?cè)?h文件中添加如下代碼:

#include <iostream>
#include "CrossApp.h"
 
USING_NS_CC;
 
class FirstViewController : public CAViewController, public  CAPageViewDelegate
{
     
public:
    FirstViewController();
     
    virtual ~FirstViewController();
     
    //切換開(kāi)始時(shí)調(diào)用
    virtual void pageViewDidBeginTurning(CAPageView* pageView);
     
    //切換結(jié)束時(shí)調(diào)用
    virtual void pageViewDidEndTurning(CAPageView* pageView);
     
    //選擇當(dāng)前的切換頁(yè)時(shí)調(diào)用
    virtual void pageViewDidSelectPageAtIndex(CAPageView* pageView, unsigned int index, const DPoint& point);
protected:
     
    void viewDidLoad();
     
    void viewDidUnload();
     
};

然后在.cpp中實(shí)現(xiàn)邏輯:

void FirstViewController::viewDidLoad()
{
    //生命一個(gè)CAVector作為添加到PageView的容器
    CAVector<CAView*> viewVector;
     
    //獲得屏幕的rect
    DRect winRect = this->getView()->getBounds();
         
    //創(chuàng)建CALabel
    CALabel* labelView = CALabel::createWithFrame(winRect);
     
    //設(shè)置為居中
    labelView->setTextAlignment(CATextAlignmentCenter);
    labelView->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
    labelView->setFontSize(75);
     
    //設(shè)置文本內(nèi)容
    labelView->setText(UTF8("第一個(gè)View"));
         
    //創(chuàng)建CAImageView
    CAImageView* imageView = CAImageView::createWithImage(CAImage::create("HelloWorld.png"));
     
    //設(shè)置顯示區(qū)域
    imageView->setFrame(winRect);
     
    //創(chuàng)建一個(gè)藍(lán)色的View
    CAView* view = CAView::createWithColor(CAColor_blue);
         
    //設(shè)置顯示區(qū)域
    view->setFrame(winRect);
     
    //創(chuàng)建CALabel
    CALabel* lastLabel = CALabel::createWithFrame(winRect);
         
    //設(shè)置居中
    lastLabel->setTextAlignment(CATextAlignmentCenter);            
    lastLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
    lastLabel->setFontSize(75);
     
    //設(shè)置顯示文本
    lastLabel->setText(UTF8("最后一個(gè)View"));
         
    //將lastLabel添加到view
    view->addSubview(lastLabel);
         
    //將上面的三個(gè)控件放入到CAVector容器內(nèi)
    viewVector.pushBack(labelView);
    viewVector.pushBack(imageView);
    viewVector.pushBack(view);
         
    /*創(chuàng)建一個(gè)CAPageView并設(shè)置為水平滾動(dòng)
    CAPageViewDirectionHorizontal:水平
    CAPageViewDirectionVertical:豎直
    */
         
    CAPageView* pageViewTest = CAPageView::createWithCenter(DRect(winRect.size.width*0.5, winRect.size.height*0.5, winRect.size.width, winRect.size.height), CAPageView::CAPageViewDirectionHorizontal);
        
    //設(shè)置監(jiān)聽(tīng)
    pageViewTest->setPageViewDelegate(this);
     
    //將CAVector添加到pageViewTest
    pageViewTest->setViews(viewVector);
    pageViewTest->getPageCount();
         
    //將pageViewTest添到屏幕顯示
    this->getView()->addSubview(pageViewTest);
     
}
 
void FirstViewController::viewDidUnload()
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
 
void FirstViewController::pageViewDidBeginTurning(CAPageView* pageView)
{
    CCLog("Begin--->");
}
 
void FirstViewController::pageViewDidEndTurning(CAPageView* pageView)
{
    CCLog("End--->");
}
 
void FirstViewController::pageViewDidSelectPageAtIndex(CAPageView* pageView, unsigned int index, const DPoint& point)
{
    CCLog("Index:%d",index);
}

我們創(chuàng)建了一個(gè)含有三個(gè)CAView的CAPageView,橫向滑動(dòng)可以切換到不同的CAView,在我們切換時(shí)候會(huì)調(diào)用pageViewDidBeginTurning函數(shù)和pageViewDidEndTurning函數(shù),當(dāng)我們點(diǎn)擊某個(gè)頁(yè)面的時(shí)候pageViewDidSelectPageAtIndex會(huì)被調(diào)用。


CAPageView 屬性說(shuō)明

PageViewDirection

類型:CAPageViewDirection

解釋:頁(yè)面瀏覽方向。get{}。

    

PageViewDelegate

類型:CAPageViewDelegate*

解釋:頁(yè)面視圖代表。set/get{}。


CurrPage

類型:int

解釋:當(dāng)前頁(yè)。get{}。


Spacing

類型:int

解釋:間距。set/get{}。


CAPageView 方法說(shuō)明

static CAPageView* createWithFrame(const CCRect& rect, const CAPageViewDirection& type);

返回值:CAPageView*

參數(shù):

類型參數(shù)名說(shuō)明
CCRect&rect大小
CAPageViewDirection&
type類型

解釋:創(chuàng)建,并指定其Frame,默認(rèn)Frame為(0,0,0,0)

    

static CAPageView* createWithCenter(const CCRect& rect, const CAPageViewDirection& type);

返回值:CAPageView*

參數(shù):

類型參數(shù)名說(shuō)明
CCRect&rect大小
CAPageViewDirection&
type類型

解釋:創(chuàng)建,并指定其Center,默認(rèn)Center為(0,0,0,0)


void setCurrPage(int var, bool animated, bool listener = false);

返回值:void

參數(shù):

類型參數(shù)名說(shuō)明
intvar頁(yè)面數(shù)
boolanimated動(dòng)畫
boollistener監(jiān)聽(tīng)

解釋:設(shè)置顯示指定頁(yè)

    

unsigned int getPageCount();

返回值:unsigned int

參數(shù):

解釋:獲取頁(yè)面總數(shù)


void setViews(const CADeque<CAView*>& vec);

返回值:void

參數(shù):

類型參數(shù)名說(shuō)明
CADeque<CAView*>&vecCADeque容器

解釋:添加存放View的CADeque容器


void setViews(const CAVector<CAView*>& vec);

返回值:void

參數(shù):

類型參數(shù)名說(shuō)明
CAVector<CAView*>&vecCAVector容器

解釋:添加存放View的CAVector容器


CAView* getSubViewAtIndex(int index);

返回值:CAView*

參數(shù):

類型參數(shù)名說(shuō)明
intindex索引

解釋:通過(guò)索引獲取子視圖


virtual void setShowsScrollIndicators(bool var);

返回值:void

參數(shù):

類型參數(shù)名說(shuō)明
boolvar是否顯示滾動(dòng)條

解釋:設(shè)置顯示滾動(dòng)條


bool init();

返回值:bool

參數(shù):

解釋:初始化







以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)