不通过Interface Builder创建更改背景色的范例
-
Ios代码 #import <UIKit/UIKit.h> @interface ColorViewController : UIViewController { UILabel *redValue, *greenValue, *blueValue; UISlider *redSlider, *greenSlider, *blueSlider; } @property(nonatomic, retain) UILabel *redValue; @property(nonatomic, retain) UILabel *greenValue; @property(nonatomic, retain) UILabel *blueValue; @property(nonatomic, retain) UISlider *redSlider; @property(nonatomic, retain) UISlider *greenSlider; @property(nonatomic, retain) UISlider *blueSlider; @end 实现文件: Ios代码 #import "ColorViewController.h" @implementation ColorViewController @synthesize redValue, greenValue, blueValue; @synthesize redSlider, greenSlider, blueSlider; - (void)loadView { UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; [contentView setBackgroundColor:[UIColor blackColor]]; self.view = contentView; redValue = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 30, 20)]; redValue.text = @"0"; redValue.textColor = [UIColor redColor]; redValue.backgroundColor = [UIColor blackColor]; greenValue = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 30, 20)]; greenValue.text = @"0"; greenValue.textColor = [UIColor greenColor]; greenValue.backgroundColor = [UIColor blackColor]; blueValue = [[UILabel alloc] initWithFrame:CGRectMake(10, 150, 30, 20)]; blueValue.text = @"0"; blueValue.textColor = [UIColor blueColor]; blueValue.backgroundColor = [UIColor blackColor]; [self.view addSubview:redValue]; [self.view addSubview:greenValue]; [self.view addSubview:blueValue]; [redValue release]; [greenValue release]; [blueValue release]; [self.view addSubview:self.redSlider]; [self.view addSubview:self.greenSlider]; [self.view addSubview:self.blueSlider]; [contentView release]; } - (UISlider *)redSlider{ if(redSlider == nil){ redSlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 30, 255, 20)]; [redSlider addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventValueChanged]; redSlider.maximumValue = 255.0; redSlider.minimumValue = 0.0; redSlider.value = 0.0; redSlider.continuous = YES; } return redSlider; } - (UISlider *)greenSlider{ if(greenSlider == nil){ greenSlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 90, 255, 20)]; [greenSlider addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventValueChanged]; greenSlider.maximumValue = 255.0; greenSlider.minimumValue = 0.0; greenSlider.value = 0.0; greenSlider.continuous = YES; } return greenSlider; } - (UISlider *)blueSlider{ if(blueSlider == nil){ blueSlider = [[UISlider alloc] initWithFrame:CGRectMake(50, 150, 255, 20)]; [blueSlider addTarget:self action:@selector(changeColor) forControlEvents:UIControlEventValueChanged]; blueSlider.maximumValue = 255.0; blueSlider.minimumValue = 0.0; blueSlider.value = 0.0; blueSlider.continuous = YES; } return blueSlider; } - (void)changeColor{ NSString *redStr = [[NSString alloc] initWithFormat:@"%d", (int)redSlider.value]; NSString *greenStr = [[NSString alloc] initWithFormat:@"%d", (int)greenSlider.value]; NSString *blueStr = [[NSString alloc] initWithFormat:@"%d", (int)blueSlider.value]; redValue.text = redStr; greenValue.text = greenStr; blueValue.text = blueStr; self.view.backgroundColor = [UIColor colorWithRed:[redValue.text floatValue]/255.0 green:[greenValue.text floatValue]/255.0 blue:[blueValue.text floatValue]/255.0 alpha:1.0]; [redStr release]; [greenStr release]; [blueStr release]; } - (void)dealloc { [super dealloc]; [redSlider release]; [greenSlider release]; [blueSlider release]; redSlider = nil; greenSlider = nil; blueSlider = nil; [redValue release]; [greenValue release]; [blueValue release]; redValue = nil; greenValue = nil; blueValue = nil; } @end
示例图: