UIView 角半径和阴影?
在一本杂志中,我读到了有关 UIView 类的一些不错的扩展。您将能够向任何 UIView 添加带有角弧度的边框或阴影。
@implementation UIView (Extentions)
-(void) enableRoundRectsWithValue:(float)value
{
self.layer.masksToBounds = true;
self.layer.cornerRadius = value;
}
-(void) enableShadow
{
self.layer.masksToBounds = false;
self.layer.shadowOffset = CGSizeMake(0,2);
self.layer.shadowOpacity = 0.5;
}
@end
虽然这些方法本身效果很好,但组合在一起效果不佳。我不能有圆角半径和阴影。至少不像你期望的那样。我猜是因为 maskToBounds 在一种方法中设置为 true,在另一种方法中设置为 false。
如何获得具有角半径和阴影(具有相同角半径)的 UIView?
In a magazin I read about some nice extensions for the UIView class. You will be able to add a border with corner radian or a drop shadow to any UIView.
@implementation UIView (Extentions)
-(void) enableRoundRectsWithValue:(float)value
{
self.layer.masksToBounds = true;
self.layer.cornerRadius = value;
}
-(void) enableShadow
{
self.layer.masksToBounds = false;
self.layer.shadowOffset = CGSizeMake(0,2);
self.layer.shadowOpacity = 0.5;
}
@end
While these methods work fine for themselves they don't play nice together. I can't have a corner radius and a shadow. At least not like you expect them to be. I guess because masksToBounds is set to true in one method and false in the other.
How can I get a UIView with a corner radius and also a shadow (with the same corner radius)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它有点老了,但更多有同样问题的人可以来这里寻找解决方案。
我想这篇文章可以提供帮助。它解释了一些关于 CALayer 和混合效果的信息,包括角半径和阴影。
It's kind of old but more people with the same problem can get here looking for a solution.
I guess this post can help. It explains a little about
CALayer
and about mixing effects, including corner radius and shadows.