只是用于處理布爾值。
_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)圖