UIView动画性能问题

发布于 2024-09-27 13:01:23 字数 396 浏览 0 评论 0原文

我正在使用 UIView 动画和 PresentModalViewController 制作一些动画。通过模拟器看起来不错,但在设备上却相当不稳定。即使是相当基本的视图,例如带有 UISearchBar 的 viewController、带有自定义颜色的 UITableView(空)以及导航栏上的按钮,在通过 PresentModalViewController 进行动画处理时也会出现抖动。

我尝试在应用程序加载时将视图加载到内存中,然后在按下按钮时将其呈现,以查看是否存在差异,但结果相同。请注意,我正在代码中创建对象并将它们添加到 viewControllers 视图中,我没有使用笔尖。

有几个视图,我所动画的只是任务栏或 alpha 属性,这些动画效果很好。

有什么技巧可能有帮助吗?实际发布应用程序时会修复此问题吗?

I'm doing some animations using the UIView animation and presentModalViewController. Through the simulator it looks fine but on device its rather choppy. Even fairly basic views for example a viewController with a UISearchBar, UITableView (empty) with a a custom color and a button on the nav bar is jerky when animated through presentModalViewController.

I've tried loading the view into memory on App load and then present it when a button is pressed to see if there is a difference but its the same outcome. As a note I'm creating the objects in code and adding them to the viewControllers view, I'm not using nibs.

There are a couple of views that all I'm animating is a task bar or an alpha property and these animate fine.

Any tricks that might help? Is this something that would be fixed when actually releasing the App?

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

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

发布评论

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

评论(1

挖鼻大婶 2024-10-04 13:01:23

许多 UIKit 对象渲染速度相当慢(例如 UITextViews 和 UISegmentedControllers),因此如果您尝试同时为多个对象设置动画,它们可能看起来不稳定。动画的每一帧都需要重新绘制视图。

您可以通过将这些缓慢的视图转换为可以更流畅地进行动画处理的 UIImageViews 来加快该过程。尝试在制作动画之前调用以下方法:

    [self flattenView:yourView forDuration:0.5];


-(void)flattenView:(UIView *)view forDuration:(CGFloat)duration
{
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView * imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    imgV.image = img;
    [view addSubview:imgV];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [imgV removeFromSuperview];
    });
}

A lot of UIKit objects render quite slowly (like UITextViews and UISegmentedControllers) so they can look choppy if you try to animate a lot of them at once. Every frame of the animation requires the view to be redrawn.

You can speed up the process by converting these slow views into UIImageViews which can be animated much more smoothly. Try calling the following method right before you do animations:

    [self flattenView:yourView forDuration:0.5];


-(void)flattenView:(UIView *)view forDuration:(CGFloat)duration
{
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, [[UIScreen mainScreen] scale]);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView * imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    imgV.image = img;
    [view addSubview:imgV];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, duration * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [imgV removeFromSuperview];
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文