利用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.

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值