W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
類說明
CASwitch控件,它是開關(guān)控件,可以實(shí)現(xiàn)類型開關(guān)。
CASwitch 屬性 (點(diǎn)擊屬性名可查看屬性介紹)
屬性 | 說明 |
On | switch是否處于開狀態(tài) |
TouchClick | switch是否處于觸摸點(diǎn)擊事件 |
OnImage | 獲取開狀態(tài)的圖像 |
OffImage | 獲取關(guān)狀態(tài)的圖像 |
ThumbTintImage | 獲取Switch的圖像 |
CASwitch 方法 (點(diǎn)擊方法名可查看方法介紹)
方法 | 說明 |
setOnImage | switch開狀態(tài)時(shí)的圖像 |
setOffImage | switch關(guān)狀態(tài)時(shí)的圖像 |
setThumbTintImage | switch的圖像 |
setIsOn | 設(shè)置switch處于開狀態(tài) |
createWithFrame | 創(chuàng)建,并指定其Frame |
createWithCenter | 創(chuàng)建,并指定其Center |
addTarget | 添加回調(diào)事件 |
removeTarget | 刪除回調(diào)事件 |
initWithFrame | 初始化,并指定其Frame |
initWithCenter | 初始化,并指定其Center |
CASwitch使用起來也非常簡單,我們看一下實(shí)例代碼:
首先在.h文件添加CASwitch的監(jiān)聽函數(shù)
//CASwitch狀態(tài)回調(diào)
void callback(CAControl* control, CCPoint point);
然后在cpp文件中添加一下代碼:
void FirstViewController::viewDidLoad()
{
// Do any additional setup after loading the view from its nib.
DSize size = this->getView()->getBounds().size;
//創(chuàng)建
CASwitch* defaultSwitch = CASwitch::createWithCenter(DRect(size.width*0.5, size.height*0.2, size.width*0.3, 20));
//設(shè)置tag
defaultSwitch->setTag(100);
//設(shè)置監(jiān)聽函數(shù)
defaultSwitch->addTarget(this, CAControl_selector(FirstViewController::callback));
//添加繪制
this->getView()->addSubview(defaultSwitch);
//創(chuàng)建
CASwitch* customSwitch = CASwitch::createWithCenter(DRect(size.width*0.5, size.height*0.4, size.width*0.3, 20));
//設(shè)置tag
customSwitch->setTag(101);
//設(shè)置開啟時(shí)圖片
customSwitch->setOnImage(CAImage::create("source_material/btn_rounded_highlighted.png"));
//設(shè)置關(guān)閉時(shí)圖片
customSwitch->setOffImage(CAImage::create("source_material/btn_rounded_normal.png"));
//設(shè)置中間圖片
customSwitch->setThumbTintImage(CAImage::create("source_material/btn_rounded3D_selected.png"));
//設(shè)置監(jiān)聽函數(shù)
customSwitch->addTarget(this, CAControl_selector(FirstViewController::callback));
//添加繪制
this->getView()->addSubview(customSwitch);
}
監(jiān)聽函數(shù)內(nèi)容如下:
void FirstViewController::callback(CAControl* control, DPoint point)
{
CCLog("callback");
//強(qiáng)轉(zhuǎn)類型
CASwitch* caSwtich = (CASwitch*)control;
//獲得tag
CCLog("Tag:%d", caSwtich->getTag());
//獲得狀態(tài)
if (!caSwtich->isOn())
{
CCLog("OFF");
}
else
{
CCLog("ON");
}
}
CASwitch 屬性介紹
類型:bool
解釋:查看是否處于開狀態(tài) 。is{}。
類型:bool
解釋:查看是否處于觸摸點(diǎn)擊事件。is{}。
類型:CAImage*
解釋:獲取開狀態(tài)的圖像。get{}。
類型:CAImage*
解釋:獲取關(guān)狀態(tài)的圖像。get{}。
類型:CAImage*
解釋:獲取Switch的圖像。get{}。
CASwitch 方法介紹
void setOnImage(CAImage* onImage)
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAImage* | onImage | 開狀態(tài)時(shí)的圖像 |
解釋:設(shè)置開狀態(tài)時(shí)的圖像
void setOffImage(CAImge* offImage)
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAImge* | offImage | 關(guān)狀態(tài)時(shí)的圖像 |
解釋:設(shè)置關(guān)狀態(tài)時(shí)的圖像
void setThumTintImage(CAImage* thumbTintImage)
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAImage* | thumbTintImage | Switch的背景圖像 |
解釋:設(shè)置Switch的背景圖像
void setIsOn(bool on, bool animated);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
bool | on | 是否處于開狀態(tài) |
bool | animated | 是否添加動(dòng)畫 |
解釋:設(shè)置switch處于開狀態(tài)
static CASwitch* createWithFrame(const DRect& rect);
返回值:static CASwitch*
參數(shù):
類型 | 參數(shù)名 | 說明 |
const DRect& | rect | 區(qū)域大小 |
解釋:創(chuàng)建,并指定其Frame
static CASwitch* createWithCenter(const DRect& rect);
返回值:static CASwitch*
參數(shù):
類型 | 參數(shù)名 | 說明 |
const DRect& | rect | 中心點(diǎn)的位置及大小 |
解釋:創(chuàng)建,并指定其Center
void addTarget(CAObject* target, SEL_CAControl selector);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAObject* | target | 當(dāng)前對(duì)象 |
SEL_CAControl | selector | 函數(shù)回調(diào)器 |
解釋:添加回調(diào)事件
void removeTarget(CAObject* target, SEL_CAControl selector);
返回值:void
參數(shù):
類型 | 參數(shù)名 | 說明 |
CAObject* | target | 當(dāng)前對(duì)象 |
SEL_CAControl | selector | 函數(shù)回調(diào)器 |
解釋:刪除回調(diào)事件
bool initWithFrame(const DRect& rect);
返回值:bool
參數(shù):
類型 | 參數(shù)名 | 說明 |
const DRect& | rect | 區(qū)域大小 |
解釋:初始化,并指定其Frame
bool initWithCenter(const DRect& rect);
返回值:
參數(shù):
類型 | 參數(shù)名 | 說明 |
const DRect& | rect | 中心點(diǎn)的位置及大小 |
解釋:初始化,并指定其Center
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: