1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad];
UISlider *slider = [[UISlider alloc]initWithFrame:CGRectMake(100, 100, 200, 30)]; slider.tag = 10;
slider.minimumValue = 0;
slider.maximumValue = 255;
slider.value = 200;
slider.maximumTrackTintColor = [UIColor greenColor];
slider.minimumTrackTintColor = [UIColor orangeColor];
slider.thumbTintColor = [UIColor blackColor];
slider.minimumValueImage = [UIImage imageNamed:@"blackHeart"];
slider.maximumValueImage = [UIImage imageNamed:@"redHeart"];
[slider setThumbImage:[UIImage imageNamed:@"blackHeart"] forState:UIControlStateNormal];
[slider setThumbImage:[UIImage imageNamed:@"redHeart"] forState:UIControlStateHighlighted]; [slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:slider]; UISlider *greenSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 200, 200, 80)]; greenSlider.tag = 11; greenSlider.minimumValue = 0; greenSlider.maximumValue = 255; [greenSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:greenSlider]; UISlider *blueSlider = [[UISlider alloc]initWithFrame:CGRectMake(100, 300, 200, 100)]; blueSlider.tag = 12; blueSlider.minimumValue = 0; blueSlider.maximumValue = 255; [blueSlider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:blueSlider]; } -(void)valueChange:(UISlider*)slider{ NSLog(@"---%lf",slider.value); UISlider *redSlider = (UISlider *)[self.view viewWithTag:10]; UISlider *greenSlider = (UISlider *)[self.view viewWithTag:11]; UISlider *blueSlider = (UISlider *)[self.view viewWithTag:12]; [self.view setBackgroundColor:[UIColor colorWithRed:redSlider.value/255.0 green:greenSlider.value/255.0 blue:blueSlider.value/255.0 alpha:1.0]];
}
@end
|