利用3个UISlider来改变一个对象的颜色

首先在 ColorView这个自定义视图的.h文件定义三个属性(作用:分别记录ColorView的red,green,blue)

@property (nonatomic,assign) CGFloat r;

@property (nonatomic,assign) CGFloat g;

@property (nonatomic,assign) CGFloat b;

#import "RootViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];   

    

    //  创建一个ColorView对象

    ColorView *colorView = [[ColorView alloc]initWithFrame:CGRectMake(50, 320, 300, 50)];

    colorView.backgroundColor = [UIColor whiteColor];

    colorView.tag = 200;

    [self.view addSubview:colorView];

    [colorView release];

    

//  创建三个UISlider来控制来控制ColorView对象的颜色

    //  创建第一个UISlider对象

    UISlider *slider1 = [[UISlider alloc]initWithFrame:CGRectMake(50, 380, 300, 30)];

    slider1.minimumValue = 0;

    slider1.maximumValue = 256;

    slider1.value = 128;

    slider1.minimumTrackTintColor = [UIColor redColor];

    slider1.maximumTrackTintColor = [UIColor grayColor];

    slider1.thumbTintColor = [UIColor greenColor];

    [slider1 addTarget:self action:@selector(redColor:) forControlEvents:(UIControlEventValueChanged)];

    [self.view addSubview:slider1];

    [slider1 release];

    

    //  创建第二个UISlider对象

    UISlider *slider2 = [[UISlider alloc]initWithFrame:CGRectMake(50, 440, 300, 30)];

    slider2.minimumValue = 0;

    slider2.maximumValue = 256;

    slider2.value = 128;

    slider2.minimumTrackTintColor = [UIColor blueColor];

    slider2.maximumTrackTintColor = [UIColor grayColor];

    slider2.thumbTintColor = [UIColor greenColor];

    [slider2 addTarget:self action:@selector(blueColor:) forControlEvents:(UIControlEventValueChanged)];

    [self.view addSubview:slider2];

    [slider2 release];

    

    //  创建第三个UISlider对象

    UISlider *slider3 = [[UISlider alloc]initWithFrame:CGRectMake(50, 500, 300, 30)];

    slider3.minimumValue = 0;

    slider3.maximumValue = 256;

    slider3.value = 128;

    slider3.minimumTrackTintColor = [UIColor greenColor];

    slider3.maximumTrackTintColor = [UIColor grayColor];

    slider3.thumbTintColor = [UIColor greenColor];

    [slider3 addTarget:self action:@selector(greenColor:) forControlEvents:(UIControlEventValueChanged)];

    [self.view addSubview:slider3];

    [slider3 release];

    

}

- (void)blueColor:(UISlider *)slider

{

    ColorView *blueColor = (ColorView *)[self.view viewWithTag:200];

    blueColor.b = slider.value;

    blueColor.backgroundColor = [UIColor colorWithRed:blueColor.r/255.0 green:blueColor.g/255.0 blue:blueColor.b/255.0 alpha:1];

}

- (void)greenColor:(UISlider *)slider

{

    ColorView *greenColor = (ColorView *)[self.view viewWithTag:200];

    greenColor.g = slider.value;

    greenColor.backgroundColor = [UIColor colorWithRed:greenColor.r/255.0 green:greenColor.g/255.0 blue:greenColor.b/255.0 alpha:1];

}

- (void)redColor:(UISlider *)slider

{

    ColorView *redColor = (ColorView *)[self.view viewWithTag:200];

    redColor.r = slider.value;

    redColor.backgroundColor = [UIColor colorWithRed:redColor.r/255.0 green:redColor.g/255.0 blue:redColor.b/255.0 alpha:1];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

你可能感兴趣的:(控件)