014改变背景颜色(扩展知识:两个圆角的视图)
2015-06-14 23:03
459 查看
效果如下:
![](http://images2015.cnblogs.com/blog/66516/201510/66516-20151031171535529-1883546692.gif)
[b]ViewController.h[/b]
ViewController.m
![](http://images2015.cnblogs.com/blog/66516/201510/66516-20151031171535529-1883546692.gif)
[b]ViewController.h[/b]
#import <UIKit/UIKit.h> @interface ViewController : UIViewController { @private UILabel *lblMessage; CGFloat colorRed; CGFloat colorGreen; CGFloat colorBlue; } @end
ViewController.m
#import "ViewController.h" @interface ViewController () - (void)redDidPush; - (void)greenDidPush; - (void)blueDidPush; - (void)changeLabelColor:(CGFloat*)pColor; @end @implementation ViewController #pragma mark - Start Implementation For Methods - (void)viewDidLoad { [super viewDidLoad]; colorRed = 0.0; colorGreen = 0.0; colorBlue = 0.0; CGPoint newPoint = self.view.center; //追加Label标签 lblMessage = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 200)]; lblMessage.textAlignment = NSTextAlignmentCenter; lblMessage.center = newPoint; lblMessage.textColor = [UIColor whiteColor]; lblMessage.text = @"染上新的颜色吧..."; lblMessage.backgroundColor = [[UIColor alloc] initWithRed:colorRed green:colorGreen blue:colorBlue alpha:1.0]; //设置Label标签的圆角 //默认是四个圆角 // lblMessage.layer.cornerRadius = 20.0; // lblMessage.layer.masksToBounds = YES; /* typedef NS_OPTIONS(NSUInteger, UIRectCorner) { UIRectCornerTopLeft = 1 << 0, UIRectCornerTopRight = 1 << 1, UIRectCornerBottomLeft = 1 << 2, UIRectCornerBottomRight = 1 << 3, UIRectCornerAllCorners = ~0UL }; */ //这里使用Layer实现两个圆角;左上角和右上角 UIRectCorner rectCorner = UIRectCornerTopLeft | UIRectCornerTopRight; UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:lblMessage.bounds byRoundingCorners:rectCorner cornerRadii:CGSizeMake(20.0, 20.0)]; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = path.CGPath; lblMessage.layer.mask = shapeLayer; [self.view addSubview:lblMessage]; //追加红色按钮 UIButton *btnRed = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnRed.frame = CGRectMake(0, 0, 50, 40); newPoint.x -= (btnRed.frame.size.width + 10); newPoint.y = self.view.frame.size.height - 70; btnRed.center = newPoint; btnRed.backgroundColor = [UIColor grayColor]; [btnRed setTitle:@"红" forState:UIControlStateNormal]; [btnRed setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btnRed addTarget:self action:@selector(redDidPush) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnRed]; //追加绿色按钮 UIButton *btnGreen = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnGreen.frame = btnRed.frame; newPoint.x += (btnRed.frame.size.width + 5); btnGreen.center = newPoint; btnGreen.backgroundColor = [UIColor grayColor]; [btnGreen setTitle:@"绿" forState:UIControlStateNormal]; [btnGreen setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; [btnGreen addTarget:self action:@selector(greenDidPush) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnGreen]; //追加蓝色按钮 UIButton *btnBlue = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btnBlue.frame = btnRed.frame; newPoint.x += (btnRed.frame.size.width + 5); btnBlue.center = newPoint; btnBlue.backgroundColor = [UIColor grayColor]; [btnBlue setTitle:@"蓝" forState:UIControlStateNormal]; [btnBlue setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [btnBlue addTarget:self action:@selector(blueDidPush) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnBlue]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Private Methods - (void)redDidPush { [self changeLabelColor:&colorRed]; } - (void)greenDidPush { [self changeLabelColor:&colorGreen]; } - (void)blueDidPush { [self changeLabelColor:&colorBlue]; } - (void)changeLabelColor:(CGFloat*)pColor { if (pColor) { if (*pColor > 0.99) { *pColor = 0.0; } else { *pColor += 0.1; } lblMessage.backgroundColor = [[UIColor alloc] initWithRed:colorRed green:colorGreen blue:colorBlue alpha:1.0]; } } @end
相关文章推荐
- mysql错误代码对照表较完整 mysql_errno()
- SGU137 Funny Strings
- 6.面向对象程序设计(3)
- 第一次冲刺阶段总结
- 第七十三天 how can I 坚持
- 第二次冲刺阶段 站立会议06
- 【开发者沙龙 干货分享】Android App性能评测与调优-内存与流畅度
- 测试
- 《Javascript高级程序设计》--读书笔记(二)
- php zmq 扩展
- HTML tags
- Warning:The project encoding (GBK) does not match the encoding
- 《GPU高性能编程CUDA实战》
- 数据库表设计
- kprobe原理解析(一)
- 第二阶段冲刺 站立会议05
- c# 垮线程调用控件
- HDU 4587 Two Nodes、POJ 2375 Reliable Nets (Tarjan)
- vector和set的区别
- Linux命令行用SpiderMonkey执行javascript脚本