UISwitch 開(kāi)關(guān)控件

只是用于處理布爾值。

_mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
/*
 CGRectZero 是高度和寬度為0,位于(0,0)的矩形常量。
 需要?jiǎng)?chuàng)建邊框但是不確定邊框大小或位置時(shí),可使用此常量。
 */

//位于正中央
_mainSwitch.center = self.view.center;

[self.view addSubview:_mainSwitch];

//關(guān)閉(OFF)顏色為紅,開(kāi)啟(ON)顏色為藍(lán)
_mainSwitch.tintColor = [UIColor redColor];
_mainSwitch.onTintColor = [UIColor blueColor];

//設(shè)置滑塊顏色
_mainSwitch.thumbTintColor = [UIColor yellowColor];

//開(kāi)關(guān)按鈕發(fā)生變化時(shí)調(diào)用方法
[_mainSwitch addTarget:self action:@selector(switchState:) forControlEvents:UIControlEventValueChanged];


- (void)switchState:(UISwitch *)sender
{
    if (_mainSwitch.isOn) {
        NSLog(@"開(kāi)卡丁車(chē)");
    } else {
        NSLog(@"芝麻關(guān)關(guān)關(guān)門(mén)");
    }
}
UISwitch開(kāi)關(guān)動(dòng)態(tài)圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容