点击"确定"按钮,屏幕颜色随机变化,并显示对应的RBG值

39 篇文章 0 订阅

效果图帅不帅
AppDelegate.h文件

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

AppDelegate.m文件


@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];


    //3.color -> 数值  Apple SD Gothic Neo  Courier New
    UILabel *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];

    UITextField *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];

    UILabel *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];

    UITextField *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];

    UILabel *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];

    UITextField *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];

    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(pressButton) forControlEvents:UIControlEventTouchUpInside];

    return YES;
}

点击按钮的pressButton的方法实现

- (void)pressButton {
    NSInteger Red, Green, Blue;
    Red =arc4random() % 256 ;
    Green = arc4random() % 256 ;
    Blue = arc4random() % 256 ;
    NSString *redString = [NSString stringWithFormat:@"%ld",Red];
    NSString *greenString = [NSString stringWithFormat:@"%ld",Green];
    NSString *blueString = [NSString stringWithFormat:@"%ld",Blue];
    UITextField *redField =  (UITextField *)[self.window viewWithTag:101];//通过tag值找到 redTextField 
    UITextField *greenField = (UITextField *)[self.window viewWithTag:102];//通过tag值找到 greenTextField 
    UITextField *blueField = (UITextField *)[self.window viewWithTag:103];//通过tag值找到 blueTextField 
    redField.text = redString;
    greenField.text = greenString;
    blueField.text = blueString;
    self.window.backgroundColor = [UIColor colorWithRed:Red / 255. green:Green / 255. blue:Blue / 255. alpha:1.0];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值