开发者

iOS: CALayer shadow only rectangular?

开发者 https://www.devze.com 2023-02-19 12:51 出处:网络
For开发者_开发技巧 the shadow properties on the CALayer, is that only rectangular and particular to the layer only? That is, with quartz I can set shadow while doing a fill path on some arbitrary path

For开发者_开发技巧 the shadow properties on the CALayer, is that only rectangular and particular to the layer only? That is, with quartz I can set shadow while doing a fill path on some arbitrary path. Can I do that with CALayer's shadow properties? I do not think so but I want a confirmation.


Yes, you can can use the shadowPath property on your CALayer to set a custom shadow. Here is an example:

[shadowDepth.layer setMasksToBounds:NO ];
[shadowDepth.layer setShadowColor:[[UIColor blackColor ] CGColor ] ];
[shadowDepth.layer setShadowOpacity:0.5 ];
[shadowDepth.layer setShadowRadius:6.0 ];
[shadowDepth.layer setShadowOffset:CGSizeMake( 0 , 0 ) ];
[shadowDepth.layer setShouldRasterize:YES ];
UIBezierPath * depthShadowPath = [UIBezierPath bezierPath ];
[depthShadowPath moveToPoint:CGPointMake( 40 , 140 ) ];
[depthShadowPath addLineToPoint:CGPointMake( shadowDepth.frame.size.width - 40 , 140 ) ];
[depthShadowPath addLineToPoint:CGPointMake( shadowDepth.frame.size.width + 20 , shadowDepth.frame.size.height + 40 ) ];
[depthShadowPath addLineToPoint:CGPointMake( -10 , shadowDepth.frame.size.height + 40 ) ];
[depthShadowPath addLineToPoint:CGPointMake( 40 , 140 ) ];
[shadowDepth.layer setShadowPath:[depthShadowPath CGPath ] ];

I did a post on this along with some tips to optimize the performance hit to you application. Check it out here: CALayer Shadows


If you're targeting iOS 3.2 or later then you can use any path you want, but people often miss it because it's in iOS but not in OS X. Check out the shadowPath property, which lets you set a specific path for each individual layer.

Also, if you're going to use shadows, it's definitely a good idea to enable shouldRasterize if possible; it makes a huge performance difference.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号