码迷,mamicode.com
首页 > 其他好文 > 详细

OC简单小功能的实现-利用三个代表三原色的滑块控制背景颜色变化

时间:2015-08-04 21:09:06      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

@interface MainViewController ()
@property(nonatomic,retain)UISlider *redSlider;
@property(nonatomic,retain)UISlider *blueSlider;
@property(nonatomic,retain)UISlider *greenSlider;
@property(nonatomic,retain)UISlider *alphaSlider;
@end

@implementation MainViewController

- (void)dealloc
{
    [_redSlider release];
    [_blueSlider release];
    [_greenSlider release];
    [_alphaSlider release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // R
    // label
    UILabel *labelRed = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 50, 40)];
    labelRed.text = @"红";
    labelRed.textAlignment = NSTextAlignmentCenter;
    labelRed.backgroundColor = [UIColor whiteColor];
    // slider
    self.redSlider = [[UISlider alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
    //滑块最大最小值
    self.redSlider.maximumValue = 1;
    self.redSlider.minimumValue = 0;
    [self.view addSubview:self.redSlider];
    [self.view addSubview:labelRed];
    // 方法
    [self.redSlider addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventValueChanged];
    [labelRed release];
    [self.redSlider release];

    
    
    // B
    // label
    UILabel *labelBlue = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 50, 40)];
    labelBlue.text = @"蓝";
    labelBlue.textAlignment = NSTextAlignmentCenter;
    labelBlue.backgroundColor = [UIColor whiteColor];
    // slider
    self.blueSlider = [[UISlider alloc] initWithFrame:CGRectMake(100, 150, 200, 40)];
    //滑块最大最小值
    self.blueSlider.maximumValue = 1;
    self.blueSlider.minimumValue = 0;
    [self.view addSubview:self.blueSlider];
    [self.view addSubview:labelBlue];
    // 方法
    [self.blueSlider addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventValueChanged];
    [labelBlue release];
    [self.blueSlider release];

    
    
    // G
    // label
    UILabel *labelGreen = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 50, 40)];
    labelGreen.text = @"绿";
    labelGreen.textAlignment = NSTextAlignmentCenter;
    labelGreen.backgroundColor = [UIColor whiteColor];
    // slider
    self.greenSlider = [[UISlider alloc] initWithFrame:CGRectMake(100, 200, 200, 40)];
    //滑块最大最小值
    self.greenSlider.maximumValue = 1;
    self.greenSlider.minimumValue = 0;
    [self.view addSubview:self.greenSlider];
    [self.view addSubview:labelGreen];
    // 方法
    [self.greenSlider addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventValueChanged];
    [labelGreen release];
    [self.greenSlider release];
    
    // alpha
    // label
    UILabel *labelAlpha = [[UILabel alloc] initWithFrame:CGRectMake(50, 250, 50, 40)];
    labelAlpha.text = @"alpha";
    labelAlpha.textAlignment = NSTextAlignmentCenter;
    labelAlpha.backgroundColor = [UIColor whiteColor];
    // slider
    self.alphaSlider = [[UISlider alloc] initWithFrame:CGRectMake(100, 250, 200, 40)];
    //滑块最大最小值
    self.alphaSlider.maximumValue = 1;
    self.alphaSlider.minimumValue = 0;
    [self.view addSubview:self.alphaSlider];
    [self.view addSubview:labelAlpha];
    // 方法
    [self.alphaSlider addTarget:self action:@selector(colorAction:) forControlEvents:UIControlEventValueChanged];
    [labelAlpha release];
    [self.alphaSlider release];
    
    
    
    
    
    
    
    
    
    
    
}
// 换颜色
- (void)colorAction:(UISlider *)slider
{
    self.view.backgroundColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value blue:self.blueSlider.value alpha:self.alphaSlider.value];
}


版权声明:本文为博主原创文章,转载请注明原文地址

OC简单小功能的实现-利用三个代表三原色的滑块控制背景颜色变化

标签:

原文地址:http://blog.csdn.net/u011752406/article/details/47281417

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!