//
// ViewController.m
// UISwtichAll
#import "ViewController.h"
@interface ViewController ()
{
UILabel *valueLabel;
UILabel *minLabel;
UILabel *maxLabel;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
#pragma mark UISwitch使用
UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(50, 50, 100, 50)];
//设置初始为on
mySwitch.on = YES;
[mySwitch setOnTintColor:[UIColor redColor]];//on时的颜色
[mySwitch setTintColor:[UIColor brownColor]];//off时的边框颜色
[mySwitch setThumbTintColor:[UIColor grayColor]];//按钮的颜色
[mySwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mySwitch];
#pragma mark UISlider
UISlider *mySlider = [[UISlider alloc]initWithFrame:CGRectMake(50, 200, [UIScreen mainScreen].bounds.size.width - 100, 20)];
mySlider.minimumValue = 0;//设置最小值
mySlider.maximumValue = 100;//设置最大值
mySlider.value = 0;//设置初始值
mySlider.continuous = YES;//设置可持续变化
mySlider.minimumTrackTintColor = [UIColor redColor]; //滑轮左边颜色
mySlider.maximumTrackTintColor = [UIColor greenColor]; //滑轮右边颜色
mySlider.thumbTintColor = [UIColor brownColor];//设置了滑轮的颜色
//设置图片后,颜色会失效
//设置最小值一端图片,会挤压滑动条宽度
// mySlider.minimumValueImage=[UIImage imageNamed:@"1.png"];
//设置最大值一端图片,会挤压滑动条宽度
// mySlider.maximumValueImage=[UIImage imageNamed:@"2.png"];
//设置已经滑过一端滑动条背景图片,会覆盖之前之前的颜色,以下相同
// [mySlider setMinimumTrackImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];
//设置未滑过一端滑动条背景图片
// [mySlider setMaximumTrackImage:[UIImage imageNamed:@"logo.png"] forState:UIControlStateNormal];
//设置滑块图片背景
// [mySlider setThumbImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[mySlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];// 针对值变化添加响应方法
[mySlider addTarget:self action:@selector(switchEndAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mySlider];
// 当前值label
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(25, 100, 50, 50)];
valueLabel.textAlignment = NSTextAlignmentCenter;
valueLabel.font = [UIFont systemFontOfSize:25];
// valueLabel.text = [NSString stringWithFormat:@"%.1f", slider.value];
[self.view addSubview:valueLabel];
// 最小值label
minLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 40, 20)];
minLabel.textAlignment = NSTextAlignmentRight;
minLabel.text = [NSString stringWithFormat:@"%.0f", mySlider.minimumValue];
[self.view addSubview:minLabel];
// 最大值label
maxLabel = [[UILabel alloc] initWithFrame:CGRectMake(mySlider.frame.origin.x + mySlider.frame.size.width + 10, 150, 40, 20)];
maxLabel.textAlignment = NSTextAlignmentLeft;
maxLabel.text = [NSString stringWithFormat:@"%.0f", mySlider.maximumValue];
[self.view addSubview:maxLabel];
}
//switch点击方法
- (void)switchAction:(UISwitch *)mySwitch
{
UISwitch *Switch = (UISwitch *)mySwitch;
if (Switch.on == YES) {
NSLog(@"on");
}else{
NSLog(@"off");
}
}
- (void)switchEndAction:(UISwitch *)mySwitch
{
valueLabel.hidden = YES;
NSLog(@"结束 手指离开");
}
- (void)sliderValueChanged:(id)sender {
UISlider *slider = (UISlider *)sender;
float num = (float)slider.value;
valueLabel.text = [NSString stringWithFormat:@"%.0f", slider.value];
valueLabel.hidden = NO;
valueLabel.frame = CGRectMake(25 + ([UIScreen mainScreen].bounds.size.width - 100)/100 * num , 100, 50, 50);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end