在 iPhone 和 iPhone 上使用 Quartz 绘制阴影速度很慢iPad。另一种方式?

发布于 2024-09-18 14:05:17 字数 598 浏览 6 评论 0原文

当我发现在 iPhone/iPad 上向 UIView 添加阴影是多么容易时,我感到非常兴奋。

只需在 Xcode 中添加框架,将导入添加到文件顶部:

#import <QuartzCore/QuartzCore.h>

然后稍后:

self.contentView.layer.shadowRadius = 3.0;
self.contentView.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
self.contentView.layer.shadowOpacity = 0.5;
self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;

虽然这确实在我的应用程序中创建了美丽的阴影,但现在显示视图时它也会滞后到死亡......即使在外部启动时也是如此调试器的。有什么我忘记了或者这种方法对于更大的视图来说不实用吗?

作为参考,我在此处发布了屏幕截图。

I was pretty excited when I found out just how easy it is to add shadows to my UIViews on the iPhone/iPad.

Just add the framework in Xcode, add the import to the top of the file:

#import <QuartzCore/QuartzCore.h>

Then later:

self.contentView.layer.shadowRadius = 3.0;
self.contentView.layer.shadowOffset = CGSizeMake(-2.0, -3.0);
self.contentView.layer.shadowOpacity = 0.5;
self.contentView.layer.shadowColor = [UIColor blackColor].CGColor;

While this does create a beautiful shadow in my app, it also lags it to death now when the view is shown... even when launched outside of the debugger. Is there something I'm forgetting or is this method just not practical for larger views?

For reference, I posted a screenshot here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

牵强ㄟ 2024-09-25 14:05:17

您应该设置 shadowPath属性。这就是 CoreGraphics 能够优化阴影的方式。

例如,如果您的视图是一个不透明的矩形:

self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.contentView.bounds].CGPath;

You should set the shadowPath property. It is how CoreGraphics is able to optimize shadows.

For example, if your view is an opaque rectangle:

self.contentView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.contentView.bounds].CGPath;
初熏 2024-09-25 14:05:17

我想我应该回答一下,因为我不想让这颗宝石被埋在评论中。

除了上面cobbal的回答很有帮助之外,oculus还提到了以下针对非矩形阴影的优化,例如文本阴影。

self.contentView.layer.shouldRasterize = YES;
// Fix visual degradation when rasterizing on high-density screens:
self.contentView.layer.rasterizationScale = [[UIScreen mainScreen] scale];

Thought I should make an answer because I didn't want this gem to get buried in the comments.

In addition to cobbal's answer above, which helped a lot, occulus also mentioned the following optimization for non-rectangular shadows, such as text shadows.

self.contentView.layer.shouldRasterize = YES;
// Fix visual degradation when rasterizing on high-density screens:
self.contentView.layer.rasterizationScale = [[UIScreen mainScreen] scale];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文