在iOS上最快的阴影方式? [英] Fastest way to do shadows on iOS?

查看:89
本文介绍了在iOS上最快的阴影方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

QuartzCore .layer.shadow吸收性能。它们似乎需要在每次更改时重新渲染,导致一切都滞后。

QuartzCore .layer.shadow's suck up performance. They appear to need to be re-rendered every time something changes, causing everything to lag.

Coregraphics渐变(对于单向阴影) - 看起来不正确。如果你的渐变从0.3 alpha变为0,它会有一些奇怪的效果,你可以看到它停止。它看起来不漂亮或自然。也许它没有抖动,但我确信我听说过核心图形渐变。这很奇怪,我不知道。

Coregraphics gradient (for 1 way shadows) - doesn't look right. if your gradient goes from 0.3 alpha to 0, it has some odd effect where you can 'see' it stop. It just doesn't look nice, or natural. Maybe it isn't dithered, but I'm sure I heard core graphics gradients are. It's odd, I don't know.

Coregraphics阴影 - 在设置它们时需要一段时间来渲染,但其他方面性能很好。只是第二次你正在等待一个视图出现,因为它必须先渲染它的阴影,这就是问题。

Coregraphics shadow - take a while to render as you set them, but otherwise great performance. It's just that second you're waiting for a view to appear because it has to render it's shadow first, that's the problem.

所以我必须遗漏一些东西。是否有另一种看起来正确的方法,并且在渲染时间和性能方面都很快?

So I must be missing something. Is there another method which looks right, and is speedy both in rendering time and in performance?

推荐答案

添加shadowPath应该会给你巨大的性能提升。以下示例假设您只希望视图两侧的阴影

Adding a shadowPath should give you a huge performance boost. The following example assumes you only want the shadow on the sides of your view

CGPathRef path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
[view.layer setShadowPath:path];

编辑:
默认情况下,CALayer会在动画,以下代码允许您将阴影缓存为位图并重复使用它而不是重绘它:

On default a CALayer draws a shadow during animations, the following code allows you to cache the shadow as a bitmap and reuse it instead of redrawing it:

self.view.layer.shouldRasterize = YES;
// Don't forget the rasterization scale
// I spent days trying to figure out why retina display assets weren't working as expected
self.view.layer.rasterizationScale = [UIScreen mainScreen].scale;

这篇关于在iOS上最快的阴影方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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