让自己的UIView有shadow

http://blog.csdn.net/favormm/archive/2011/05/08/6404084.aspx

 

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

 

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值