CADrawerController 控件名字为 抽屉视图控制器 。
说白了就是实现下面这个功能(以手机QQ事例):
就是实现左边的功能设置。
实现步骤:
1.创建app , 使用project-creator.exe创建一个项目
2.修改根视图控制器 ,修改RootWindow.cpp文件的Init函数,修改结果如下:
bool RootWindow::init() { if (!CAWindow::init()) { return false; } // FirstViewController* _viewController = new FirstViewController(); // _viewController->init(); // this->setRootViewController(_viewController); // _viewController->release(); CADrawerController *pdraw = new CADrawerController(); pdraw->init(); FirstViewController *pfirst = new FirstViewController; pfirst->init(); SecondViewController *psecond = new SecondViewController; psecond->init(); psecond->setDrawViewController(pdraw); pdraw->initWithController(pfirst, psecond, 300); pdraw->setEffect3D(true); //切换显示3d效果 pdraw->setBackgroundView(CAImageView::createWithColor(CAColor_orange)); //设置切换背景 this->setRootViewController(pdraw); pfirst->release(); psecond->release(); return true; }
3. 添加朱视图控制器类 SecondViewController , 代码如下:
SecondViewController.h
#pragma once #include <iostream> #include "CrossApp.h" USING_NS_CC; class SecondViewController : public CAViewController { public: SecondViewController(); virtual ~SecondViewController(); CC_SYNTHESIZE(CADrawerController*, m_draw, DrawViewController); void ShowLeftView(CAControl* btn, CCPoint point); protected: void viewDidLoad(); void viewDidUnload(); };
SecondViewController.cpp文件
#include "SecondViewController.h" SecondViewController::SecondViewController() { } SecondViewController::~SecondViewController() { } void SecondViewController::viewDidLoad() { // Do any additional setup after loading the view from its nib. CCRect winRect = this->getView()->getBounds(); CAImageView* imageView = CAImageView::createWithImage(CAImage::create("r/HelloWorld.png")); imageView->setImageViewScaleType(CAImageViewScaleTypeFitImageCrop); imageView->setFrame(winRect); this->getView()->addSubview(imageView); CALabel* label = CALabel::createWithFrame(CCRect(0, winRect.size.height*0.5, winRect.size.width, 200)); label->setTextAlignment(CATextAlignmentCenter); label->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter); label->setFontSize(_px(72)); label->setText("Hello World2!"); label->setColor(CAColor_white); this->getView()->insertSubview(label, 1); CAImage *pimage = CAImage::create("r/number.png"); CAButton *btn = CAButton::createWithFrame(CCRect(0, 50, winRect.size.width, 200), CAButtonTypeRoundedRect); btn->setImageForState(CAControlStateAll , pimage ); btn->addTarget(this, CAControl_selector(SecondViewController::ShowLeftView), CAControlEventTouchDown); this->getView()->insertSubview(btn, 1); CCLog("%f", CAApplication::getApplication()->getWinSize().width); } void SecondViewController::viewDidUnload() { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } void SecondViewController::ShowLeftView(CAControl* btn, CCPoint point) { m_draw->showLeftViewController(true); }
4.效果如下(忽略界面的丑) :