美文网首页
iOS 给控件设置layer,渐变色,阴影

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

作者: iaiayao | 来源:发表于2018-02-23 15:26 被阅读1058次

按钮 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,渐变色,阴影

      本文链接:https://www.haomeiwen.com/subject/npsxxftx.html