UIControl
addTarget:action:forControlEvents:? 添加響應(yīng)事件(滿足什么條件下 讓某人調(diào)用某方法)
1.UISegmentedControl分段控制器
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"消息", @"電話", @"微信"]];
seg.frame = CGRectMake(100, 40, 200, 40);
[self.view addSubview:seg];
[seg release];
//選中分段下標
seg.selectedSegmentIndex = 2;
//背景顏色
seg.backgroundColor = [UIColor blackColor];
//渲染顏色
seg.tintColor = [UIColor lightGrayColor];
//插入新的分段
[seg insertSegmentWithTitle:@"陌陌" atIndex:2 animated:YES];
//添加響應(yīng)事件 (通過下標值的變化觸發(fā)方法)
[seg addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
- (void)segAction:(UISegmentedControl *)seg
{
}
2.UISlider滑塊控制器
UISlider *sl = [[UISlider alloc] initWithFrame:CGRectMake(100, 550, 200, 50)];
sl.backgroundColor = [UIColor yellowColor];
[self.view addSubview:sl];
[sl release];
// 顏色設(shè)置
// 劃過距離的顏色(滑塊左)
sl.minimumTrackTintColor = [UIColor blackColor];
// 未滑過的顏色(滑塊右)
sl.maximumTrackTintColor = [UIColor redColor];
// 滑塊顏色
sl.thumbTintColor = [UIColor greenColor];
// 滑動范圍
// 最小值
sl.minimumValue = -100;
// 最大值
sl.maximumValue = 1000;
// 更新滑塊起始點(value默認從0開始)
sl.value = -100;
// 添加響應(yīng)事件
[sl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
- (void)sliderAction:(UISlider *)sl
{
}
3.UISwitch 開關(guān)
UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(250, 150, 100, 50)];
sw.backgroundColor = [UIColor whiteColor];
[self.view addSubview:sw];
[sw release];
// 開關(guān)屬性
sw.on = YES;
// 開啟狀態(tài)顏色
sw.onTintColor = [UIColor brownColor];
// 按鈕的顏色
sw.thumbTintColor = [UIColor redColor];
// 關(guān)閉時邊框顏色
sw.tintColor = [UIColor cyanColor];
[sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
- (void)switchAction:(UISwitch *)sw
{
if (sw.on) {
NSLog(@"開啟");
}else{
NSLog(@"關(guān)閉");
}
}