您的位置:首页 > 其它

014改变背景颜色(扩展知识:两个圆角的视图)

2015-06-14 23:03 459 查看
效果如下:



[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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: