iOS 给控件设置layer,渐变色,阴影

按钮 Layer

 UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];   

button.frame=CGRectMake(100,250,60,60);// 40px square   

button.layer.cornerRadius=30.0;// half height & width   

button.layer.borderWidth=3.0;   

button.layer.borderColor= [UIColorblackColor].CGColor;   

button.backgroundColor= [UIColorredColor];   

[button setTitle:@"渐变"forState:UIControlStateNormal];   

button.titleLabel.font= [UIFont boldSystemFontOfSize:22];   

[button setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];   

[self.view addSubview:button];

渐变色

CAGradientLayer*layer = [CAGradientLayer layer];   

NSArray*colors = [NSArrayarrayWithObjects: (id)[UIColor whiteColor].CGColor, (id)[UIColorredColor].CGColor, nil];   

[layer setColors:colors];   

[layer setFrame:button.bounds];

//    layer.shadowPath = [UIBezierPath bezierPathWithRect:layer.bounds].CGPath;

//!!!!!!!important   

[button.layer insertSublayer:layeratIndex:0];   

button.clipsToBounds=YES;// Important!

加阴影

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100,400,60,60)];   

[self.view addSubview:myView];   

myView.layer.borderWidth=2.0;   

myView.layer.borderColor = [UIColor redColor].CGColor;   

myView.layer.shadowColor = [UIColor darkGrayColor].CGColor;   

myView.layer.shadowOpacity=1.0;   

myView.layer.shadowOffset = CGSizeMake(2.0,2.0);

//    myView.layer.shadowPath = [UIBezierPath bezierPathWithRect:layer.bounds].CGPath;

//    myView.layer.shadowOffset = CGSizeMake(-2.0, 2.0);

你可能感兴趣的:(iOS 给控件设置layer,渐变色,阴影)