CALayer阴影与Spread&尺寸属性?

时间:2011-11-30 00:27:33

标签: ios core-animation core-graphics photoshop shadow

我正在努力在CALayer周围创建一个投影。为了达到我想要实现的效果,我需要访问阴影的“强度”。不知何故像Photoshop的图层样式中的“传播”滑块。我认为CALayer的“ShadowRadius”属性相当于Photoshop的“大小”滑块。

有什么建议吗?可能是径向渐变选项吗?

2 个答案:

答案 0 :(得分:8)

设置图层的shadowOffsetshadowOpacityshadowRadius,您应该拥有所需内容。试试这个阴影项目直接“在”下面,模糊5像素的图层,使用正确的颜色,也可以使它看起来像一个发光:

CALayer *layer = myView.layer;
layer.shadowOpactiy = 0.50;
layer.shadowRadius = 5.0;
layer.shadowOffset = (CGSize){.width=0.0,.height=0.0};

答案 1 :(得分:6)

您可以通过在CALayer上设置自定义shadowPath属性来控制“传播”。

CGFloat radius = CGRectGetWidth(view.bounds)/2.0;
// this is a sample code for a circle, but you can use any shape you want
CGPathRef shadowPathRef = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0 * radius, 2.0 * radius) cornerRadius:radius].CGPath;

layer.shadowPath = shadowPathRef;

如果将半径设置为更大的值,则阴影扩散将更大。