效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController { 4 @private 5 UILabel *lblMessage; 6 CGFloat colorRed; 7 CGFloat colorGreen; 8 CGFloat colorBlue; 9 } 10 11 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)redDidPush; 5 - (void)greenDidPush; 6 - (void)blueDidPush; 7 - (void)changeLabelColor:(CGFloat*)pColor; 8 @end 9 10 @implementation ViewController 11 #pragma mark - Start Implementation For Methods 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 colorRed = 0.0; 15 colorGreen = 0.0; 16 colorBlue = 0.0; 17 CGPoint newPoint = self.view.center; 18 19 //追加Label标签 20 lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 200)]; 21 lblMessage.textAlignment = NSTextAlignmentCenter; 22 lblMessage.center = newPoint; 23 lblMessage.textColor = [UIColor whiteColor]; 24 lblMessage.text = @"染上新的颜色吧..."; 25 lblMessage.backgroundColor = [[UIColor alloc] initWithRed:colorRed green:colorGreen blue:colorBlue alpha:1.0]; 26 //设置Label标签的圆角和边框 27 lblMessage.layer.cornerRadius = 8.0; 28 lblMessage.layer.masksToBounds = YES; 29 lblMessage.layer.borderColor = [UIColor yellowColor].CGColor; 30 lblMessage.layer.borderWidth = 2.0; 31 [self.view addSubview:lblMessage]; 32 33 //追加红色按钮 34 UIButton *btnRed = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 35 btnRed.frame = CGRectMake(0, 0, 50, 40); 36 newPoint.x -= (btnRed.frame.size.width + 10); 37 newPoint.y = self.view.frame.size.height - 70; 38 btnRed.center = newPoint; 39 btnRed.backgroundColor = [UIColor grayColor]; 40 [btnRed setTitle:@"红" forState:UIControlStateNormal]; 41 [btnRed setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 42 [btnRed addTarget:self action:@selector(redDidPush) forControlEvents:UIControlEventTouchUpInside]; 43 [self.view addSubview:btnRed]; 44 45 //追加绿色按钮 46 UIButton *btnGreen = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 47 btnGreen.frame = btnRed.frame; 48 newPoint.x += (btnRed.frame.size.width + 5); 49 btnGreen.center = newPoint; 50 btnGreen.backgroundColor = [UIColor grayColor]; 51 [btnGreen setTitle:@"绿" forState:UIControlStateNormal]; 52 [btnGreen setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; 53 [btnGreen addTarget:self action:@selector(greenDidPush) forControlEvents:UIControlEventTouchUpInside]; 54 [self.view addSubview:btnGreen]; 55 56 57 //追加蓝色按钮 58 UIButton *btnBlue = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 59 btnBlue.frame = btnRed.frame; 60 newPoint.x += (btnRed.frame.size.width + 5); 61 btnBlue.center = newPoint; 62 btnBlue.backgroundColor = [UIColor grayColor]; 63 [btnBlue setTitle:@"蓝" forState:UIControlStateNormal]; 64 [btnBlue setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 65 [btnBlue addTarget:self action:@selector(blueDidPush) forControlEvents:UIControlEventTouchUpInside]; 66 [self.view addSubview:btnBlue]; 67 } 68 69 - (void)didReceiveMemoryWarning { 70 [super didReceiveMemoryWarning]; 71 // Dispose of any resources that can be recreated. 72 } 73 74 #pragma mark - Private Methods 75 - (void)redDidPush { 76 [self changeLabelColor:&colorRed]; 77 } 78 79 - (void)greenDidPush { 80 [self changeLabelColor:&colorGreen]; 81 } 82 83 - (void)blueDidPush { 84 [self changeLabelColor:&colorBlue]; 85 } 86 87 - (void)changeLabelColor:(CGFloat *)pColor { 88 if (pColor) { 89 if (*pColor > 0.99) { 90 *pColor = 0.0; 91 } else { 92 *pColor += 0.1; 93 } 94 lblMessage.backgroundColor = [[UIColor alloc] initWithRed:colorRed green:colorGreen blue:colorBlue alpha:1.0]; 95 } 96 } 97 @end
时间: 2024-11-16 05:13:21