颜色控制
题目:
创建3个UISlider对象。⽤用这3个UISlider对象控制self.view.backgroundColor的变化。 3个UISlider对象分别控制UIColor的red,green,blue。 滑动任何⼀一个UISlider都会引起背景颜⾊色变化。
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
RootViewController *rootVC = [[RootViewController alloc]init];
self.window.rootViewController = rootVC;
[rootVC release];
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@property(nonatomic,retain,readonly)UISlider *sliderRed;
@property(nonatomic,retain,readonly)UISlider *sliderGreen;
@property(nonatomic,retain,readonly)UISlider *sliderBlue;
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// 红色
_sliderRed = [[UISlider alloc]initWithFrame:CGRectMake(40, 100, 300, 10)];
_sliderRed.minimumValue = 0;
_sliderRed.maximumValue = 1.0;
_sliderRed.thumbTintColor = [UIColor redColor];
[_sliderRed addTarget:self action:@selector(SliderRed:) forControlEvents:UIControlEventValueChanged];
_sliderRed.tag = 100;
[self.view addSubview:_sliderRed];
[_sliderRed release];
// 绿色
_sliderGreen = [[UISlider alloc]initWithFrame:CGRectMake(40, 200, 300, 10)];
_sliderGreen.minimumValue = 0;
_sliderGreen.maximumValue = 1.0;
_sliderGreen.thumbTintColor = [UIColor greenColor];
[_sliderGreen addTarget:self action:@selector(SliderGreen:) forControlEvents:UIControlEventValueChanged];
_sliderGreen.tag = 200;
[self.view addSubview:_sliderGreen];
[_sliderGreen release];
// 蓝色
_sliderBlue = [[UISlider alloc]initWithFrame:CGRectMake(40, 300, 300, 10)];
_sliderBlue.minimumValue = 0;
_sliderBlue.maximumValue = 1.0;
_sliderBlue.thumbTintColor = [UIColor blueColor];
[_sliderBlue addTarget:self action:@selector(SliderBlue:) forControlEvents:UIControlEventValueChanged];
_sliderBlue.tag = 300;
[self.view addSubview:_sliderBlue];
[_sliderBlue release];
}
- (void)SliderRed:(UISlider *)sliderred
{
CGFloat a1 = self.sliderGreen.value;
CGFloat b1 = self.sliderBlue.value;
// CGFloat a = ((UISlider *)[self.view viewWithTag:200]).value;
// CGFloat b = ((UISlider *)[self.view viewWithTag:300]).value;
self.view.backgroundColor = [UIColor colorWithRed:sliderred.value green:a1 blue:b1 alpha:1.0];
}
- (void)SliderGreen:(UISlider *)slidergreen
{
CGFloat a1 = self.sliderRed.value;
CGFloat b1 = self.sliderBlue.value;
// CGFloat a = ((UISlider *)[self.view viewWithTag:100]).value;
// CGFloat b = ((UISlider *)[self.view viewWithTag:300]).value;
self.view.backgroundColor = [UIColor colorWithRed:a1 green:slidergreen.value blue:b1 alpha:1.0];
}
- (void)SliderBlue:(UISlider *)sliderblue
{
CGFloat a1 = self.sliderRed.value;
CGFloat b1 = self.sliderGreen.value;
// CGFloat a = ((UISlider *)[self.view viewWithTag:100]).value;
// CGFloat b = ((UISlider *)[self.view viewWithTag:200]).value;
self.view.backgroundColor = [UIColor colorWithRed:a1 green:b1 blue:sliderblue.value alpha:1.0];
}