简体   繁体   English

为什么简单的模态视图控制器在显示和关闭时会滞后?

[英]Why would a simple modal view controller lag when presented and dismissed?

The main view of my app is a UIImagePickerController camera view. 我的应用程序的主视图是UIImagePickerController相机视图。 When the app becomes active (in didBecomeActive), I present a modal view controller that shows some settings generated from a network request. 当应用程序变为活动状态(在didBecomeActive中)时,我将提供一个模式视图控制器,该控制器显示从网络请求生成的一些设置。 (Note that for debugging purposes, I took the network request out and am currently just showing a dummy view) (请注意,出于调试目的,我取出了网络请求,目前仅显示一个虚拟视图)

The modal view animates in smoothly, but after loading it freezes for 3 seconds then responds normally. 模态视图动画流畅,但加载后冻结3秒钟,然后正常响应。 After dismissing the view (also animates smoothly), my image picker controller pauses for 2 seconds then resumes normally. 消除视图(动画效果也很流畅)后,我的图像选择器控制器暂停2秒钟,然后正常恢复。

I have removed all functionality from the modal view controller to make sure there was no operations clogging the main thread. 我已从模态视图控制器中删除了所有功能,以确保没有任何操作阻塞主线程。 I am presenting the most basic of controllers, and still get the choppy ui. 我介绍了最基本的控制器,但仍然得到了不稳定的ui。 I would suspect that this is from my presenting view controller calling viewDidLoad/Unload or something similar, but my search did not give me any information on what delegate methods are called in the presenting view controller when a modal view is shown. 我会怀疑这是由于我的呈现视图控制器调用了viewDidLoad / Unload或类似的东西,但是当显示模式视图时,我的搜索没有提供有关呈现视图控制器中调用了哪些委托方法的任何信息。

My problem can be solved by answering: 我的问题可以通过回答来解决:

  • What delegate methods are called in the presenter when a modal view is shown? 显示模式视图时,在演示者中调用了哪些委托方法?
  • (If any ^) How can I not call those methods, or make them run smoother? (如果有^)我如何不能调用这些方法,或者使它们运行得更流畅?
  • What common pitfalls are associated with modal view controllers? 模态视图控制器有哪些常见的陷阱?

There are multiple methods invoked. 有多种调用方法。 ViewDidLoad ViewWillAppear ViewDidAppear ViewWillDisappear ViewDidDisappear. ViewDidLoad ViewWillAppear ViewDidAppear ViewWillDisappear ViewDidDisappear。 Check all of those methods. 检查所有这些方法。 Also, check any subviews you have created and see if they are doing any thing on their thread involving image loading in the methods i stated. 另外,检查您创建的所有子视图,并查看它们是否在我声明的方法中的线程上进行了涉及图像加载的任何操作。 Also does this occur in the simulator as well as a test device? 模拟器和测试设备中也会发生这种情况吗?

ModalViewControllers do not have too many pitfalls but understanding how many views are allocated on things like navigation stacks and how many views you have on top of each other. ModalViewControllers没有太多陷阱,但可以了解在诸如导航堆栈之类的东西上分配了多少个视图,以及彼此之间有多少个视图。 When you get rid of the modal viewcontroller do you call dismissviewcontroller? 当您摆脱模态视图控制器时,您会调用dismissviewcontroller吗?

This is probably because you are making a lot of processing in the main thread (usually when UI stops, it's because main thread processing). 这可能是因为您在主线程中进行了大量处理(通常是在UI停止时,这是因为主线程处理了)。 Try to provide us some code, specifically the one you think is the most heavy processing code! 尝试为我们提供一些代码,特别是您认为最重的处理代码! Sorry about my poor english :P! 对不起我的英语不好:P! Try dispatching most heavy code to another thread with 尝试使用以下命令将最繁重的代码分发到另一个线程

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //your heavy code here =)
});

Regards, Lucas 问候,卢卡斯

One thing that might be a contributor to some slight lag is reloading the same viewController from scratch each time. 可能造成一些轻微延迟的一件事是每次都从头开始重新加载相同的viewController。

BProfileTableViewController * _profileViewController = [[UIStoryboard storyboardWithName:@"Profile" bundle:[NSBundle chatUIBundle]] instantiateInitialViewController];
UINavigationController * profileNavigationController = [[UINavigationController alloc] initWithRootViewController:profileViewController];
[self.navigationController presentViewController:profileNavigationController animated:YES completion:nil];

You can see here that if this is on a tableView click then each time the app needs to create the viewController again. 您可以在此处看到,如果这是在tableView上单击的,则每次应用程序需要再次创建viewController时,请单击。 If instead we just reuse the view then it gets rid of some of that lag. 如果相反,我们只是重用视图,那么它可以消除某些滞后。

Add this in the header file 将此添加到头文件中

BProfileTableViewController * _profileView;

Then the modal view load code changes to: 然后,模态视图加载代码更改为:

// Open the users profile
if (!_profileView) {
    _profileView = [[UIStoryboard storyboardWithName:@"Profile" bundle:[NSBundle chatUIBundle]] instantiateInitialViewController];
}

UINavigationController * profileNavigationController = [[UINavigationController alloc] initWithRootViewController:_profileView];
[self.navigationController presentViewController:profileNavigationController animated:YES completion:nil];

This means we are just reloading the view instead of recreating the view 这意味着我们只是重新加载视图,而不是重新创建视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从主视图控制器知道何时解散了所提供的模态视图控制器? - Knowing from the main view controller when the presented modal view controller is dismissed? 检测何时关闭呈现的视图控制器 - Detect when a presented view controller is dismissed 模态显示/关闭视图控制器时通知? - Notification when view controller is presented modally/dismissed? 关闭呈现的视图控制器时如何恢复焦点? - How to restore focus when presented view controller is dismissed? 当通过手势关闭呈现的视图 controller 时如何获得通知? - How to get notified when a presented view controller is dismissed with a gesture? 调用viewWillAppear时显示视图控制器的问题被解除 - Issue with calling viewWillAppear of presenting view controller when presented one is dismissed TabBarController viewDidAppear 在呈现的视图控制器关闭时未调用 - TabBarController viewDidAppear not called when presented view controller dismissed 为什么在applicationDidFinishLaunchingWithOptions中未显示模态视图控制器? - Why is the modal view controller not being presented in applicationDidFinishLaunchingWithOptions? 出现模态视图 controller 时暂停 animation - Pause animation when modal view controller is presented 为什么在取消模态视图控制器后从视图中删除CAGradientLayer - Why is the CAGradientLayer removed from view after modal view controller is dismissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM