定制属于你自己的阴影效果

来源:互联网 发布:uda sql 0432 编辑:程序博客网 时间:2024/04/28 14:09
[cpp] view plaincopyprint?
  1. #import <QuartzCore/QuartzCore.h>  
  2.   
  3. @implementation UIView (NKShadow)  
  4.   
  5.   
  6. // add the shadow effect to the view  
  7. -(void)addShadow{  
  8.       
  9.     self.layer.shadowOpacity = 0.4;  
  10.     self.layer.shadowRadius = 0.9;  
  11.     self.layer.shadowOffset = CGSizeMake(0, 0);  
  12.       
  13.     UIBezierPath *path = [UIBezierPath bezierPath];  
  14.       
  15.     CGPoint p1 = CGPointMake(self.frame.origin.x, self.frame.origin.y+self.frame.size.height);  
  16.     CGPoint p2 = CGPointMake(self.frame.origin.x+self.frame.size.width, p1.y);  
  17.     CGPoint c1 = CGPointMake((p1.x+p2.x)/4 , p1.y+6.0);  
  18.     CGPoint c2 = CGPointMake(c1.x*3, c1.y);          
  19.       
  20.     [path moveToPoint:p1];  
  21.     [path addCurveToPoint:p2 controlPoint1:c1 controlPoint2:c2];  
  22.       
  23.     self.layer.shadowPath = path.CGPath;  
  24. }  
  25.   
  26. -(void)addGrayGradientShadow{  
  27.     // 0.8 is a good feeling shadowOpacity  
  28.     self.layer.shadowOpacity = 0.4;  
  29.       
  30.     // The Width and the Height of the shadow rect  
  31.     CGFloat rectWidth = 10.0;  
  32.     CGFloat rectHeight = self.frame.size.height;  
  33.       
  34.     // Creat the path of the shadow  
  35.     CGMutablePathRef shadowPath = CGPathCreateMutable();  
  36.     // Move to the (0, 0) point  
  37.     CGPathMoveToPoint(shadowPath, NULL, 0.0, 0.0);  
  38.     // Add the Left and right rect  
  39.     CGPathAddRect(shadowPath, NULL, CGRectMake(0.0-rectWidth, 0.0, rectWidth, rectHeight));  
  40.     CGPathAddRect(shadowPath, NULL, CGRectMake(self.frame.size.width, 0.0, rectWidth, rectHeight));  
  41.       
  42.     self.layer.shadowPath = shadowPath;  
  43.     CGPathRelease(shadowPath);  
  44.     // Since the default color of the shadow is black, we do not need to set it now  
  45.     //self.layer.shadowColor = [UIColor blackColor].CGColor;  
  46.       
  47.     self.layer.shadowOffset = CGSizeMake(0, 0);  
  48.     // This is very important, the shadowRadius decides the feel of the shadow  
  49.     self.layer.shadowRadius = 10.0;  
  50. }  
  51.   
  52.   
  53. @end  
0 0
原创粉丝点击