设定三原色的值, 生成对应的屏幕背景

来源:互联网 发布:淘宝网2017女装羽绒服 编辑:程序博客网 时间:2024/05/15 23:42

作业效果图
AppDelegate.h文件

#import <UIKit/UIKit.h>@interface AppDelegate : UIResponder <UIApplicationDelegate,UITextFieldDelegate> //添加协议//编辑环境为MRC, 改用 retain 修饰实例变量@property (retain, nonatomic) UIWindow *window;@end

AppDelegate.m文件

#import "AppDelegate.h"@interface AppDelegate (){//用Extension方法扩展实例变量的作用域    UILabel *redLabel;    UILabel *greenLabel;    UILabel *blueLabel;    UITextField *redTextField;    UITextField *greenTextField;    UITextField *blueTextField;}@end
@implementation AppDelegate//编辑环境选用的是MRC模式, 需重写dealloc方法- (void)dealloc{    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    //创建redLabel    redLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 80, 30)];    redLabel.backgroundColor = [UIColor redColor];    NSLog(@"%@",[UIFont familyNames]);    redLabel.font = [UIFont fontWithName:@"Courier New" size:25];    redLabel.textAlignment = NSTextAlignmentCenter;    redLabel.text = @"Red";    redLabel.textColor = [UIColor whiteColor];    [self.window addSubview:redLabel];    [redLabel release];    //创建redTextField    redTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 100, 165, 30)];    redTextField.tag = 101;    redTextField.backgroundColor = [UIColor whiteColor];    redTextField.placeholder = @"请输入0-255的数";    redTextField.borderStyle = UITextBorderStyleRoundedRect;    [self.window addSubview:redTextField];    [redTextField release];    //创建greenLabel    greenLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 80, 30)];    greenLabel.backgroundColor = [UIColor greenColor];    greenLabel.text= @"Green";    greenLabel.textColor = [UIColor whiteColor];    greenLabel.font = [UIFont fontWithName:@"Courier New" size:25];    greenLabel.textAlignment = NSTextAlignmentCenter;    [self.window addSubview:greenLabel];    [greenLabel release];    //创建greenTextField    greenTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 150, 165, 30)];    greenTextField.tag = 102;    greenTextField.backgroundColor = [UIColor whiteColor];    greenTextField.placeholder = @"请输入0-255的数";    greenTextField.borderStyle = UITextBorderStyleRoundedRect;    [self.window addSubview:greenTextField];    [greenTextField release];    //创建blueLabel    blueLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 80, 30)];    blueLabel.backgroundColor = [UIColor blueColor];    blueLabel.text = @"Blue";    blueLabel.textColor = [UIColor whiteColor];    blueLabel.textAlignment = NSTextAlignmentCenter;    blueLabel.font = [UIFont fontWithName:@"Courier New" size:25];    [self.window addSubview:blueLabel];    [blueLabel release];    //创建blueTextField    blueTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 200, 165, 30)];    blueTextField.tag = 103;    blueTextField.backgroundColor = [UIColor whiteColor];    blueTextField.placeholder = @"请输入0-255的数";    blueTextField.borderStyle = UITextBorderStyleRoundedRect;    [self.window addSubview:blueTextField];    [blueTextField release];    //创建okButton    UIButton *okButton = [UIButton buttonWithType:UIButtonTypeSystem];    okButton.frame = CGRectMake(150, 280, 75, 30);    okButton.backgroundColor = [UIColor whiteColor];    [okButton setTitle:@"确定" forState:UIControlStateNormal];    [self.window addSubview:okButton];    [okButton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];

点击按钮的submit的方法实现

- (void)submit {    CGFloat Red, Green, Blue;    Red = [redTextField.text integerValue] / 255.;    Green = [greenTextField.text integerValue] / 255.;    Blue = [blueTextField.text integerValue] / 255.;    self.window.backgroundColor = [UIColor colorWithRed: Red green:Green  blue:Blue alpha:1.0];}

下一篇用 tag值方法 实现相反操作

0 0
原创粉丝点击