控制类

2016-02-23  本文已影响34人  飞翔的鸵鸟

UIControl

addTarget:action:forControlEvents:  添加响应事件(满足什么条件下 让某人调用某方法)

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

//添加响应事件 (通过下标值的变化触发方法)

[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];

// 颜色设置

// 划过距离的颜色(滑块左)

sl.minimumTrackTintColor = [UIColor blackColor];

// 未滑过的颜色(滑块右)

sl.maximumTrackTintColor = [UIColor redColor];

// 滑块颜色

sl.thumbTintColor = [UIColor greenColor];

// 滑动范围

// 最小值

sl.minimumValue = -100;

// 最大值

sl.maximumValue = 1000;

// 更新滑块起始点(value默认从0开始)

sl.value = -100;

// 添加响应事件

[sl addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

- (void)sliderAction:(UISlider *)sl

{

}

3.UISwitch 开关

UISwitch *sw = [[UISwitch alloc] initWithFrame:CGRectMake(250, 150, 100, 50)];

sw.backgroundColor = [UIColor whiteColor];

[self.view addSubview:sw];

[sw release];

// 开关属性

sw.on = YES;

// 开启状态颜色

sw.onTintColor = [UIColor brownColor];

// 按钮的颜色

sw.thumbTintColor = [UIColor redColor];

// 关闭时边框颜色

sw.tintColor = [UIColor cyanColor];

[sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

- (void)switchAction:(UISwitch *)sw

{

if (sw.on) {

NSLog(@"开启");

}else{

NSLog(@"关闭");

}

}

上一篇 下一篇

猜你喜欢

热点阅读