UIView 角半径和阴影?

发布于 2024-12-08 10:50:22 字数 553 浏览 1 评论 0原文

在一本杂志中,我读到了有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-12-15 10:50:22

它有点老了,但更多有同样问题的人可以来这里寻找解决方案。

我想这篇文章可以提供帮助。它解释了一些关于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文