(Iphone Sdk) 如何在加载视图控制器时显示动画活动指示器?

发布于 2024-10-18 05:17:26 字数 84 浏览 5 评论 0原文

我的视图非常重,需要两秒或更长时间才能加载。我想在 viewDidLoad 上携带所有内容时显示活动指示器,然后隐藏它。有人可以指导我这样做吗?提前致谢。

I have a so heavy view that takes two seconds or more seconds to load. I would like to show activity indicator while be carrying all things at the viewDidLoad and then hide it. Can someone guide me to do this? Thanks in advance.

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

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

发布评论

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

评论(5

缺⑴份安定 2024-10-25 05:17:26

尝试 Matej Bukovinski 的 MBProgressHUD。

https://github.com/matej/MBProgressHUD

在后台获取数据

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD showWhileExecuting:@selector(getStartupData:)
               onTarget:self
             withObject:nil
               animated:YES]

如果你想在 main 中获取数据线程,只需显示 HUD,然后在收到通知时将其关闭。

HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
[self.tableView addSubview:HUD];
HUD.labelText = @"Loading";
HUD.tag = 998;
[HUD show:YES];
[HUD release];

Try MBProgressHUD by Matej Bukovinski.

https://github.com/matej/MBProgressHUD

Fetching data in background

HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD showWhileExecuting:@selector(getStartupData:)
               onTarget:self
             withObject:nil
               animated:YES]

If you want to get data in main thread, just show the HUD and then dismiss it when notified.

HUD = [[MBProgressHUD alloc] initWithView:self.tableView];
[self.tableView addSubview:HUD];
HUD.labelText = @"Loading";
HUD.tag = 998;
[HUD show:YES];
[HUD release];
诗笺 2024-10-25 05:17:26

我不确定,但我怀疑是否可以在后台线程上运行 viewDidLoad 。如果这是真的,那么在运行 viewDidLoad 时,您将无法为活动指示器设置动画。

所以如果我是你,我会弄清楚是什么花了 viewDidLoad 这么长时间,并将其放入可以在后台运行的方法中。

然后在 viewWillAppear 方法中添加一个活动指示器视图,并在后台任务完成时将其删除。

根据您在 viewDidLoad 中执行的操作,这可能需要大量重构和添加委托方法。
我猜你正在下载一些应该显示的东西。

I don't know for sure but I doubt that it's possible to run viewDidLoad on a background thread. If this is true you have no way to animate an activity indicator when running viewDidLoad.

So If I were you I would figure out what takes viewDidLoad so long, and put this into a method that can run in the background.

Then add a activity indicator view in your viewWillAppear method and remove it when your background task is complete.

Depending on the stuff you do in viewDidLoad this might require a lot of refactoring and adding of delegate methods.
I guess you are downloading something that should be displayed.

紫南 2024-10-25 05:17:26

尝试将花费很长时间的内容移至 viewDidAppear 中。然后,您可以使用活动指示器创建一个新视图并使其可见(在界面生成器中最简单),一旦您完成所有工作,只需调用:

[loadingView setHidden:YES];

祝你好运

Try moving the stuff that's taking so long into viewDidAppear. Then you can create a new view with the activity indicator and have it visible (easiest in interface builder) and once all your stuff is done just call:

[loadingView setHidden:YES];

good luck

七堇年 2024-10-25 05:17:26

[[UIApplication sharedApplication] setNetworkActivityIndi​​catorVisible:YES]; 放入调用慢速加载 viewController 的视图中,如果慢速启动器是初始视图,则放入 appDidFinishLoading 中。

此外,请考虑将需要很长时间的活动从主线程移至后台线程,以保持 GUI 始终响应。

Put [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; in the view from where you call the slow loading viewController, or into appDidFinishLoading if the slow starter is the initial view.

Additionally, consider moving the activity that takes a long time from the main thread into a background thread, keeping the GUI always responsive.

此生挚爱伱 2024-10-25 05:17:26

你可以非常简单地做到这一点
在 viewDidLoad

UIAactivityIndicatorView *  date  = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 150, 30, 30)];
[date setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addsubview:date];
[date startAnimating];

加载页面视图后,您可以通过以下方式停止对活动指示器进行动画处理
[停止动画的日期];

这会起作用

you can do this very simply
in viewDidLoad

UIAactivityIndicatorView *  date  = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150, 150, 30, 30)];
[date setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
[self.view addsubview:date];
[date startAnimating];

After loading the page view, there u stop animate the Acitivity indicator by
[date stopAnimating];

this will work

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