PushViewController 动画很慢/断断续续

发布于 2024-12-26 06:10:31 字数 267 浏览 5 评论 0原文

我推送一个 ViewController ,其中不包含太多视图,UIScrollView 其中包含 10 个视图,我有一个单例 ViewController 并一次又一次地推送它无需再次释放和分配 ViewController,因此我在 viewDidLoad()viewWillAppear() 中执行的所有操作,但动画是又慢又断断续续,这是什么可能是吗?

I push a ViewController which contains not too many views, UIScrollView which contains 10 views inside, I have a singleton ViewController and push it again and again without releasing and allocation again the ViewController, so all the things I do it in viewDidLoad(), and viewWillAppear(), but the animation is slow and choppy, what it could be?

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

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

发布评论

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

评论(8

蓝眸 2025-01-02 06:10:31

我遇到一个问题,当 UIViewController A 执行 PushViewController 来推送 UIViewController B 时,推送动画会停止在大约 25% 处,停止,然后在剩下的时间内滑动 B。

这在 iOS 6 上并没有发生,但当我开始使用 iOS 7 作为 XCode 5 中的基础 SDK 时,这种情况就开始发生。

解决方法是视图控制器 B 没有在其根视图上设置 backgroundColor(根视图是 viewController.view 的值,通常在 loadView 中设置)。在该根视图的初始值设定项中设置背景颜色解决了该问题。

我设法解决这个问题如下:

// CASE 1: The root view for a UIViewController subclass that had a halting animation

- (id)initWithFrame:(CGRect)frame

{

     if ((self = [super initWithFrame:frame])) {

          // Do some initialization ...

          // self.backgroundColor was NOT being set

          // and animation in pushViewController was slow and stopped at 25% and paused

     }

     return self;

}

// CASE 2: HERE IS THE FIX

- (id)initWithFrame:(CGRect)frame

{

     if ((self = [super initWithFrame:frame])) {

          // Do some initialization ...

          // Set self.backgroundColor for the fix!

          // and animation in pushViewController is no longer slow and and no longer stopped at 25% and paused

          self.backgroundColor = [UIColor whiteColor]; // or some other non-clear color

     }

     return self;

}

I had a problem where when UIViewController A did a pushViewController to push UIViewController B, the push animation would stop at about 25%, halt, and then slide B in the rest of the way.

This DID NOT happen on iOS 6, but as soon as I started using iOS 7 as the base SDK in XCode 5, this started happening.

The fix is that view controller B did not have a backgroundColor set on its root view (the root view is the one that is the value of viewController.view, that you typically set in loadView). Setting a backgroundColor in that root view's initializer fixed the problem.

I managed to fix this as follows:

// CASE 1: The root view for a UIViewController subclass that had a halting animation

- (id)initWithFrame:(CGRect)frame

{

     if ((self = [super initWithFrame:frame])) {

          // Do some initialization ...

          // self.backgroundColor was NOT being set

          // and animation in pushViewController was slow and stopped at 25% and paused

     }

     return self;

}

// CASE 2: HERE IS THE FIX

- (id)initWithFrame:(CGRect)frame

{

     if ((self = [super initWithFrame:frame])) {

          // Do some initialization ...

          // Set self.backgroundColor for the fix!

          // and animation in pushViewController is no longer slow and and no longer stopped at 25% and paused

          self.backgroundColor = [UIColor whiteColor]; // or some other non-clear color

     }

     return self;

}
篱下浅笙歌 2025-01-02 06:10:31

解决这个问题的唯一方法是,永远不要将主视图的背景颜色设置为透明颜色。

当您的下一个视图覆盖前一个视图时,如果您将背景设置为透明颜色,则意味着它是透明的,前一个视图在某些时候始终可见,这会破坏动画。

Only solution to this problem, never set background color of main view to clear color.

As your next view coming over the previous view, if you set background to clear color, means it is transparent, the previous view is always visible for sometime which spoil animation.

失眠症患者 2025-01-02 06:10:31

您可以从使用“仪器”>开始Time Profiler 并查看代码中是否有任何部分花费的时间超过必要的时间。

您还可以使用“仪器”>核心动画工具,可用于标记屏幕上未有效绘制/动画的部分。

如果您使用的是旧 iPhone 或原始 iPod - 屏幕复杂,我注意到一些应用程序有点不稳定。

You could start by using Instruments > Time Profiler and see if there is any part of your code that is taking longer than necessary.

You could also use the Instruments > Core Animation tool which can be used to flag parts of your screen that not drawing/animating efficiently.

If you're using an old iPhone or an original iPod - with complex screens, i've noticed some apps a bit choppy.

白衬杉格子梦 2025-01-02 06:10:31

我遇到了这个问题,这是因为推送的视图控制器中的旧代码导致视图“淡入”,在 viewWillAppear 上将 alpha 设置为 0,在 viewDidAppear 上将 alpha 设置为 1。

我删除了执行此操作的旧代码,并且推送工作正常。

-(void)viewWillAppear {
    self.view.alpha = 0; //REMOVE THIS LINE
}

I had this problem, and it was because of old code in the pushed view controller, which caused the view to "fade in", setting the alpha to 0 on viewWillAppear and setting the alpha to 1 on viewDidAppear.

I removed the old code that did this, and the push worked fine.

-(void)viewWillAppear {
    self.view.alpha = 0; //REMOVE THIS LINE
}
假装不在乎 2025-01-02 06:10:31

我的问题是我将大量加载代码放入 viewWillAppear 中。我的情况是

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    tableView.reloadData()
}

我有很多行,所以pushViewController 很滞后。

My problem is I puts mass load codes in viewWillAppear. My case is

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    tableView.reloadData()
}

And I have a lot of rows, so pushViewController is laggy.

晨曦÷微暖 2025-01-02 06:10:31

这可能是下面链接的答案中讨论的已知错误。我遇到了类似的问题,阅读下面的答案为我澄清了这一点。

我今天遇到了同样的问题。我深入研究了这个主题,它似乎与主运行循环休眠有关。

请参阅

This may be the known bug discussed in the linked an answer below. I had a similar issue and reading the answer below clarified it for me.

I've encountered the same issue today. I dug into the topic and it seems that it's related to the main runloop being asleep.

See this.

゛时过境迁 2025-01-02 06:10:31

就我而言,我在 viewWillLayoutSubviews() 方法中设置了 ui 更改代码(例如shadow和cornerRadius和...),当我将这些代码放入其他方法(例如viewWillAppear())时,过渡变得更平滑

In my case I set ui change code ( such as shadow And cornerRadius And ...) in viewWillLayoutSubviews() method and when I put those code in other methods like viewWillAppear() that's transition get smoother

感情废物 2025-01-02 06:10:31

Swift:如果您要在任何视图等上应用阴影,那么您也可以尝试下面的代码。

view.layer.shouldRasterize = true
view.layer.rasterizationScale = UIScreen.main.scale

Swift: If you are applying shadow on any view etc then you can also try below code for this.

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