UISwitch | UIStepper

UISwitch 開關空間blog

- (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];
    }
}

  打開開關     關上開關get

 

UIStepperit

- (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);
}
相關文章
相關標籤/搜索