<转>为View绘制阴影

关键代码预览:

 

 1 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 225.0)];  
 2 view.center = self.view.center;  
 3 view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin |   
 4    UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;  
 5 view.layer.contents = (id)[UIImage imageNamed:@"photo.jpeg"].CGImage;  
 6 view.layer.borderColor = [UIColor colorWithWhite:1.0 alpha:1.0].CGColor;  
 7 view.layer.borderWidth = 5.0;  
 8 view.layer.shadowOffset = CGSizeMake(0, 3);  
 9 view.layer.shadowOpacity = 0.7;   
10 view.layer.shouldRasterize = YES;  
11      
12    // shadow  
13    UIBezierPath *path = [UIBezierPath bezierPath];    
14   
15 CGPoint topLeft      = view.bounds.origin;  
16 CGPoint bottomLeft   = CGPointMake(0.0, CGRectGetHeight(view.bounds) + 10);  
17 CGPoint bottomMiddle = CGPointMake(CGRectGetWidth(view.bounds) / 2, CGRectGetHeight(view.bounds) - 5);    
18 CGPoint bottomRight  = CGPointMake(CGRectGetWidth(view.bounds), CGRectGetHeight(view.bounds) + 10);  
19 CGPoint topRight     = CGPointMake(CGRectGetWidth(view.bounds), 0.0);  
20   
21 [path moveToPoint:topLeft];  
22 [path addLineToPoint:bottomLeft];  
23 [path addQuadCurveToPoint:bottomRight  
24              controlPoint:bottomMiddle];  
25 [path addLineToPoint:topRight];  
26 [path addLineToPoint:topLeft];  
27 [path closePath];  
28   
29 view.layer.shadowPath = path.CGPath;      
30   
31 [self.view addSubview:view];  

个人理解 其实也是应用了CAlayer 的阴影属性,并且用了UIBezierPath 来画了一个阴影路径 最后照着这个路径来画阴影

但是这样做阴影貌似效率不高,有人反映会导致变卡, 这一点有待实践

posted @ 2012-08-31 14:30  不曾拥有  阅读(192)  评论(0)    收藏  举报