UISwitch | UIStepper
UISwitch 开关空间
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UISwitch *switchC = [[UISwitch alloc] initWithFrame:CGRectMake(150, 100, 50, 50)]; switchC.onTintColor = [UIColor redColor]; // 打开后的颜色 switchC.tintColor = [UIColor greenColor]; // 周围圈的颜色 switchC.thumbTintColor = [UIColor yellowColor]; // 开关颜色 switchC.on = YES; // 初始默认是关上的NO [switchC setOn:NO animated:YES]; [switchC addTarget:self action:@selector(doTap:) forControlEvents:(UIControlEventValueChanged)]; [self.view addSubview:switchC]; [switchC release]; } - (void)doTap:(UISwitch *)swithC { if (swithC.on) { self.view.backgroundColor = [UIColor grayColor]; } else { self.view.backgroundColor = [UIColor magentaColor]; } }
打开开关 关上开关
UIStepper
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIStepper *step = [[UIStepper alloc] initWithFrame:CGRectMake(150, 200, 50, 50)]; step.minimumValue = 0; step.maximumValue = 9; step.stepValue = 1; // 每次变化的数值 step.value = 5; // 当前的数值 [step addTarget:self action:@selector(doStep:) forControlEvents:(UIControlEventValueChanged)]; [self.view addSubview:step]; } - (void)doStep:(UIStepper *)step { NSLog(@"%f, %f", step.stepValue, step.value); }