让自己的UIView有shadow

加一个有shadow的sub view, 只需要在你自定义UIView里加几行代码。

//turning off bounds clipping allows the shadow to extend beyond the rect of the view [self setClipsToBounds:NO]; //the colors for the gradient. highColor is at the top, lowColor as at the bottom UIColor * highColor = [UIColor colorWithWhite:1.000 alpha:1.000]; UIColor * lowColor = [UIColor colorWithRed:0.851 green:0.859 blue:0.867 alpha:1.000]; //The gradient, simply enough. It is a rectangle CAGradientLayer * gradient = [CAGradientLayer layer]; [gradient setFrame:[self bounds]]; [gradient setColors:[NSArray arrayWithObjects:(id)[highColor CGColor], (id)[lowColor CGColor], nil]]; //the rounded rect, with a corner radius of 6 points. //this *does* maskToBounds so that any sublayers are masked //this allows the gradient to appear to have rounded corners CALayer * roundRect = [CALayer layer]; [roundRect setFrame:[self bounds]]; [roundRect setCornerRadius:6.0f]; [roundRect setMasksToBounds:YES]; [roundRect addSublayer:gradient]; //add the rounded rect layer underneath all other layers of the view [[self layer] insertSublayer:roundRect atIndex:0]; //set the shadow on the view's layer [[self layer] setShadowColor:[[UIColor blackColor] CGColor]]; [[self layer] setShadowOffset:CGSizeMake(0, 6)]; [[self layer] setShadowOpacity:1.0]; [[self layer] setShadowRadius:10.0];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值