iphone - 当视图的阴影打开时,动画的性能非常差

发布于 2024-12-09 06:57:44 字数 294 浏览 0 评论 0原文

我有一个 UILabel ,上面有 CALayer 阴影。我只是通过 UIView 动画移动它。

性能很差,我可以看到动画一点也不流畅。

我认为是 UILabel 的阴影导致了动画问题,因为如果我关闭阴影,动画就会变得像正常一样平滑。

我尝试过使用 view.layer.shouldRasterize = YES;

但动画性能仍然存在。

任何人都可以给我一些提示吗?

谢谢

I have a UILabel with CALayer shadow on.And I just move it around via UIView animation.

The performance is poor and I can see the animation is not smooth at all.

I think it is the shadow of the UILabel which causes the animation problem because if I turn the shadow off, the animation becomes as smooth as normal.

I have tried using view.layer.shouldRasterize = YES;

But still the animation performance is there.

Anyone can give me some hints?

Thanks

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

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

发布评论

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

评论(2

殊姿 2024-12-16 06:57:44

您可以通过使用 CALayer 的 shadowPath 属性来极大地提高 CALayer 阴影的性能,这允许它绘制阴影而无需重新计算图层的 alpha 遮罩。对于矩形视图,您可以像这样使用它:

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

或者,如果它的角是圆角的,

theView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:theView.bounds cornerRadius:theView.layer.cornerRadius].CGPath;

请注意,这是视图边框周围的阴影 - 如果您希望文本本身的阴影有更好的性能,您需要使用标签的文本阴影属性(牺牲了 CALayer 阴影的优点,例如模糊,以获得更好的渲染速度)或者 - 一个更复杂的选项 - 创建一个 CGPathRef 以用作来自文本的图层的 shadowPath字形本身。

You can greatly improve the performance of a CALayer’s shadow by using its shadowPath property—this allows it to draw the shadow without having to recalculate the alpha mask of the layer. For a rectangular view, you’d use it like this:

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

or, if its corners are rounded,

theView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:theView.bounds cornerRadius:theView.layer.cornerRadius].CGPath;

Note that this is a shadow around the view’s borders—if you want better performance on the shadow on the text itself, you either need to use the label’s text-shadow properties (which sacrifice the niceties of CALayer shadows, like blur, for better rendering speed) or—a much more complicated option—create a CGPathRef to use as the layer’s shadowPath from the text glyphs themselves.

凉城 2024-12-16 06:57:44

不确定这是否是您正在寻找的答案,但我发现了这个: Drop UITextField 文本上的阴影

这可能会带来更好的性能,我还没有尝试过,但看起来会是这样。

Not sure if this is the answer you're looking for but I found this: Drop Shadow on UITextField text

It may be better performance, I haven't tried it but it seems it would be.

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