014改变背景颜色

效果如下:

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

014改变背景颜色的相关文章

jquery改变背景颜色

jquery改变背景颜色<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").toggle(function(){    $

jquery动态改变背景颜色插件

GETHUB下载地址 背景颜色用animate方法时时无法改变颜色的 所以要使用插件进行补充. 用法: <!DOCTYPE html> <html> <head> <style> div { background-color: #bada55; width: 100px; border: 1px solid green; } </style> <script src="http://code.jquery.com/jquery-1

CSS实例:鼠标滑过超级链接文字时改变背景颜色

先讲简单的: 通过CSS可以设置超链接在不同时刻的颜色: <style> a:link {color: #FF0000} /* 未访问的链接 */ a:visited {color: #00FFFF} /* 已访问的链接 */ a:hover {color: #0000FF} /* 鼠标移动到链接上 */ a:active {color: #00FF00} /* 选定的链接 */ </style> <ul id="content"> <li&g

改变背景颜色

//改变背景颜色 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e) { //如果是绑定数据行 if (e.Row.RowType == DataControlRowType.DataRow) { //鼠标经过时,行背景色变 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ffa501'&qu

锐浪 报表, 交叉报表中 对交叉字段,做条件改变背景颜色 .

var fieldCount = Report.RunningDetailGrid.ColumnContent.ContentCells.Count; // 总字段列数 var lockFieldCount = Report.DetailGrid.CrossTab.ListCols; // 锁定字段列数 var crossFieldCount = fieldCount - lockFieldCount; for(var colIndex = 1; colIndex <=crossFieldCou

Android实现连续并排的若干个TextView单击改变背景颜色达到选项卡Tab栏切换效果

<Android实现连续并排的若干个TextView单击改变背景颜色达到选项卡Tab栏切换效果> 实现的组件交互目的很简单,就是要达到类似tab选项卡的导航栏一样,当用户点击了连续并排的若干个TextView其中之一时候,要产生一定的交互效果(背景颜色改变等).这种实现的方式有不少可选方案,现在通过背景的selector.xml实现. 效果如下: A.初始状态: B.当用户单击了TextView 2,但此时用户的手指一直在按下并未松开的状态,(注意交互设计效果!TextView 2的背景颜色变

iOS 创建多个button实现点击改变背景颜色

工程中需要实现与UISegmentedControl效果相似的一排一共十个button,如下图.但是SegmentedControl修改不太方便,就用button替代, 循环创建十个button,点击改变背景色.其他的没被点的button的背景恢复默认.之前也写过,因为button太多,需要各种判断.感觉之前方法的方法比较麻烦. . 第一种方法 在创建button的时候,把所有的button存储到可变数组里,在button点击事件里面,拿到之前的可变数组,对里面的每一个button遍历,遍历的时

UITableviewCell点击改变背景颜色, 标题高亮.

 改变UITableViewCell选中时背景色: cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cellart.png"]] autorelease];   // 点击背景图片 cell.selectedBackgroundView = [[[UIView alloc] initWithFrame:cell.frame] autorelease];    

eclipse改变背景颜色

eclipse eclipse默认的背景颜色好丑,肿么办!看的时间太长,眼镜太干 把eclipse改成保护色 在这个网站下载主题:eclipsecolorthemes.org 进入这个官网来找自己喜欢的背景色 选择一个自己喜欢的主题,然后进入下载.这里我选择Sublime Text2的主题进入.进入之后大概是这个样子,选择EPF文件下载. 然后再项目区->import选择General->Preferences选择之前下载的epf文件->Finish.即可 配置完成之后,大家会看到只有代