只是用于處理布爾值。
_mainSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
/*
CGRectZero 是高度和寬度為0,位于(0,0)的矩形常量。
需要創建邊框但是不確定邊框大小或位置時,可使用此常量。
*/
//位于正中央
_mainSwitch.center = self.view.center;
[self.view addSubview:_mainSwitch];
//關閉(OFF)顏色為紅,開啟(ON)顏色為藍
_mainSwitch.tintColor = [UIColor redColor];
_mainSwitch.onTintColor = [UIColor blueColor];
//設置滑塊顏色
_mainSwitch.thumbTintColor = [UIColor yellowColor];
//開關按鈕發生變化時調用方法
[_mainSwitch addTarget:self action:@selector(switchState:) forControlEvents:UIControlEventValueChanged];
- (void)switchState:(UISwitch *)sender
{
if (_mainSwitch.isOn) {
NSLog(@"開卡丁車");
} else {
NSLog(@"芝麻關關關門");
}
}
UISwitch開關動態圖