无法添加角半径和阴影 [英] Can't add a corner radius and a shadow

查看:92
本文介绍了无法添加角半径和阴影的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在图像上绘制阴影和角半径。我可以单独添加它们,但我无法同时添加这两种效果。我添加了一个阴影:

I'm trying to draw a shadow and a corner radius on an image. I can add them separately, but I've got no way to add both effects at the same time. I'm adding a shadow with:

[layer setShadowOffset:CGSizeMake(0, 3)];
[layer setShadowOpacity:0.4];
[layer setShadowRadius:3.0f];
[layer setShouldRasterize:YES];

这里,layer是UIView子类的CALayer。所以每当我设置

Here, layer is a CALayer of a UIView subclass. So this works whenever I set

[layer setMasksToBounds:NO];

现在添加转角半径我这样做:

Now to add a corner radius I do this:

[layer setCornerRadius:7.0f];

但我需要将MasksToBounds设置为YES才能使其正常工作:

but I need to set MasksToBounds to YES in order for this to work:

[layer setMasksToBounds:YES];

无论如何我可以添加这两种效果吗?

Is there anyway I can get both of these effects to add?

感谢您的时间,

Denis

推荐答案

是的,是的,有... ...

Yes, yes there is...

如果你想要一个圆角半径和一个投影,你就不要打开 -masksToBounds ,而是设置角半径并使用圆角矩形设置阴影的贝塞尔曲线路径。保持两者的半径相同:

If you want both a corner radius and a drop shadow, you don't turn on -masksToBounds, but rather set the corner radius and set the bezier path of the shadow with a rounded rect. Keep the radius of the two the same:

[layer setShadowOffset:CGSizeMake(0, 3)];
[layer setShadowOpacity:0.4];
[layer setShadowRadius:3.0f];
[layer setShouldRasterize:YES];

[layer setCornerRadius:12.0f];
[layer setShadowPath:
                   [[UIBezierPath bezierPathWithRoundedRect:[self bounds]
                                               cornerRadius:12.0f] CGPath]];

您可能希望在没有 -shouldRasterize 参数设置的情况下检查您的性能你正在设置阴影路径。设置阴影路径后,绘图性能会非常好。

You might want to check your performance without the -shouldRasterize parameter set once you're setting the shadow path. Drawing performance tends to be very good once you've set a shadow path.

UPDATE

我在很长一段时间内没有看过这个问题,但似乎你不再需要设置一个 shadowPath 来实现这一点。只需设置 cornerRadius shadowOpacity 即可使用。我认为这是iOS5以来的情况(据我所知)。提供此更新可能是不必要的,因为设置这些参数正常,但我会为后人提供它。总结一下,现在就是你所需要的:

I hadn't looked at this problem in quite awhile, but it appears that you no longer need to set a shadowPath in order to get this to work. Simply setting the cornerRadius and shadowOpacity will work now. I think this has been the case since iOS5 (as far as I can tell). Providing this update is probably unnecessary since setting those parameters 'just works', but I'll provide it for posterity sake. To recap, this is now all you need:

[layer setShadowOpacity:0.4];
[layer setCornerRadius:12.0f];

如果仍需要更好的性能,可以继续设置 shouldRasterize 参数:

If you still need better performance, you can go ahead and set the shouldRasterize parameter as well:

[layer setShouldRasterize:YES];

说到性能,值得注意的是,如果你注意到动画迟缓,你会想要使用毕竟设置阴影路径的技术。此更新实际上只是指出不再需要设置路径来实现同时显示角半径和阴影的效果。但是,如果性能是您的首选,请使用路径。

And speaking of performance, it's worth noting that if you are noticing sluggish animations, you will want to use the technique of setting the shadow path after all. This update was really just to point out that setting the path is no longer required to achieve the effect of displaying both a corner radius and a shadow at the same time. If performance is your priority, though, use a path.

更新2

由于人们似乎无法在某些情况下使其工作,我将在此处创建一个更完整的代码片段:

Since people seem to be having trouble getting this to work in some instances, I'll post a more complete code snippet here from a sample project I created:

- (void)viewDidLoad
{
  [super viewDidLoad];

  CALayer *layer = [CALayer layer];
  [layer setBounds:CGRectMake(0.0f, 0.0f, 100.0f, 200.0f)];
  [layer setPosition:[[self view] center]];
  [layer setBackgroundColor:[[UIColor lightGrayColor] CGColor]];
  [layer setShadowOpacity:0.55f];
  [layer setCornerRadius:8.0f];
  [layer setBorderWidth:1.0f];

  [[[self view] layer] addSublayer:layer];

  [[[self testView] layer] setShadowOpacity:0.55f];
  [[[self testView] layer] setShadowRadius:15.0f];
  [[[self testView] layer] setCornerRadius:8.0f];
  [[[self testView] layer] setBorderWidth:1.0f];
}

testView 是我在Interface Builder中添加了一个UIView并设置了一个插座。这是为了确保它在您明确添加的两个图层以及子视图中的图层上的工作方式相同。

The testView is a UIView I added in Interface Builder and set an outlet on. This is to make sure it's working the same on both layers you add explicitly as well as the layers within subviews.

我在iOS5到iOS6的模拟器上测试了这个0.1。它为我提供了这个结果:

I've tested this on the simulators for iOS5 through iOS6.1. It gives this result for me in each of them:

这篇关于无法添加角半径和阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆