博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

转http://blog.csdn.net/toddmi/article/details/8133967

给一个对象简单设置阴影效果:

 

[html] view plaincopy
 
  1. Lable.shadowColor = color;  
  2. Lable.shadowOffset = CGSizeMake(0, -1.0);  


 

 

[html] view plaincopy
 
  1. UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 130, 130)];  
  2. [testView setBackgroundColor:[UIColor grayColor]];  
  3. [[testView layer] setShadowOffset:CGSizeMake(15, 15)];  //设置阴影位置相对于Object位置的偏移值  
  4. [[testView layer] setShadowRadius:0];  
  5. [[testView layer] setShadowOpacity:0.6];  
  6. [[testView layer] setShadowColor:[UIColor grayColor].CGColor];  
  7. [self.view addSubview:testView];  
  8. [testView release];  


根据path设置阴影效果:

 

 

[html] view plaincopy
 
  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  

 

 

UIView以外的区域设置阴影效果:

 

[html] view plaincopy
 
  1. [[UIColor colorWithWhite:0.0f alpha:0.5f] setFill];//阴影效果  根据透明度来设计  
  2.     UIRectFill( rect );  
  3.     CGRect holeRect = CGRectMake(holeX, holeY, holeWidth, holeHeight);//设置透明范围  
  4.     CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );      
  5.     [[UIColor clearColor] setFill];  
  6.     UIRectFill( holeRectIntersection );   

 

UIView设置边框:

 

[html] view plaincopy
 
  1. //UIView设置边框  
  2. [[testView layer] setCornerRadius:5];  
  3. [[testView layer] setBorderWidth:2];  
  4. [[testView layer] setBorderColor:[UIColor redColor].CGColor];  



拉伸一张图片:

 

[html] view plaincopy
 
    1. UIImage *smallImage = [UIImage imageNamed:@"hc_03.png"];      
    2. UIImageView *imageView = [[UIImageView alloc] initWithImage:smallImage];    
    3. [imageView setFrame:CGRectMake(0, 20, 320, 44)];    
    4. imageView.contentMode = UIViewContentModeScaleToFill;    
    5. [self.view addSubview:imageView];    
    6.   
    7. [imageView release];    
posted on 2013-05-08 18:19  Likwo  阅读(715)  评论(0编辑  收藏  举报