xcode 在应用程序启动期间显示活动指示器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6849072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show activity indicator during application launch
提问by Tanoro
I am trying to add an activity indicator during startup. I did have a launch image, but I'd rather have just an indicator alone. I added the following to my app delegate, but the indicator doesn't appear.
我正在尝试在启动期间添加活动指示器。我确实有一个启动图像,但我宁愿只有一个指示器。我将以下内容添加到我的应用程序委托中,但没有出现指示器。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// *******Create activity indicator****
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(225, 115, 30, 30)];
[activity setBackgroundColor:[UIColor clearColor]];
[activity setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleGray];
[window addSubview: activity];
[activity release];
[activity startAnimating];
activity.hidden = FALSE;
// *******End activity indicator****
MainViewController *viewController = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
self.mainViewController = viewController;
[window addSubview:[mainViewController view]];
mainViewController.view.frame = CGRectMake(0, 20, 320, 411);
[window addSubview:[rootController view]];
[window makeKeyAndVisible];
#if !TARGET_IPHONE_SIMULATOR
[application registerForRemoteNotificationTypes:
UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
#endif
application.applicationIconBadgeNumber = 0;
// Hide indicator
[viewController release];
activity.hidden = TRUE;
[activity stopAnimating];
return YES;
}
I visited this question from back in 2009, but the solution didn't work for me. iPhone-SDK:Activity Indicator during Startup?
我从 2009 年开始访问了这个问题,但该解决方案对我不起作用。 iPhone-SDK:启动期间的活动指示器?
采纳答案by Paul.s
I don't think this is possible from looking at the docs for UIApplicationDelegate
我认为查看UIApplicationDelegate的文档是不可能的
The earliest your app is called is in
最早调用您的应用程序是在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Which as the name suggests is when your app hasFinishedLaunching
顾名思义就是当您的应用程序 hasFinishedLaunching
The reason nothing happens when you show and hide the indicator is to do with threading. The user interface will only update at the end of your method where the net result of your changes will be that the indicator is hidden.
显示和隐藏指示器时什么也没有发生的原因与线程有关。用户界面只会在您的方法结束时更新,您更改的最终结果将是隐藏指标。
回答by Maor Zohar
For all those who needed this, and i know there were many...
(Made on Xcode version 4.2.1)
对于所有需要这个的人,我知道有很多......
(在 Xcode 4.2.1 版上制作)
So...
所以...
In the AppDelegate.h add this:
在 AppDelegate.h 中添加:
@property (nonatomic, strong) UIImageView *splashView;
In the AppDelegate.m add this:
在 AppDelegate.m 中添加:
On the top of the page of cours @synthesize splashView;
And then:
在 cours @synthesize splashView 的页面顶部;
进而:
- (void) splashFade
{
splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:@"Default.png"];
[_window addSubview:splashView];
[_window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelay:2.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:_window cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
[UIView commitAnimations];
//Create and add the Activity Indicator to splashView
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.alpha = 1.0;
activityIndicator.center = CGPointMake(160, 360);
activityIndicator.hidesWhenStopped = NO;
[splashView addSubview:activityIndicator];
[activityIndicator startAnimating];
}
- (void)startupAnimationDone:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
[splashView removeFromSuperview];
}
The [UIView setAnimationDelay:2.5]is responsible for how long the splashView will be in front by the delay time you choose.
在[UIView的setAnimationDelay:2.5]负责splashView有多长在前面由您选择的延迟时间。
You can change the position of the indicator by changing the nubmers of x/y in:
activityIndicator.center = CGPointMake(160, 360);
您可以通过更改 x/y 中的数字来更改指标的位置:
activityIndicator.center = CGPointMake(160, 360);
At last, under the methode:
最后,在methode下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Just add this:
只需添加这个:
[self performSelector:@selector(splashFade) withObject:nil];
And there you go :)
Hope it helped.
你去:)
希望它有所帮助。
Have a nice programming....
有一个很好的编程....
回答by OhadM
Another solution is to let the launchimageload and when your ViewController loads at the viewDidLoad, use overlay + indicator on top of your view with alpha below 1:
另一个解决方案是让启动图像加载,当您的 ViewController 在 viewDidLoad 加载时,在您的视图顶部使用叠加 + 指示器,alpha 低于 1:
UIView *baseView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = NO;
baseView.alpha = 0.4;
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
indicator.center = self.view.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:self.view];
[indicator startAnimating];
Now comes the fun part, use delegate to remove the indicator + overlay when all your background threads finish to load up and simple call a method via your delegate:
现在是有趣的部分,当您的所有后台线程完成加载并通过您的委托简单调用一个方法时,使用委托删除指示器 + 覆盖:
- (void) onFinishLoading{
[indicator stopAnimating];
[baseView removeFromSuperview];
}
- In order to stopAnimation and removeFromSuperView you might want to make baseViewand indicatorproperty + @synthesize
- 为了 stopAnimation 和 removeFromSuperView,您可能需要创建baseView和指标属性 + @synthesize
回答by elslooo
This isn't possible (as @Paul.S is saying). However, you can simulate this by adding a (static, so it's not animating) Default.png-splash-screen to your project.
这是不可能的(正如@Paul.S 所说)。但是,您可以通过向项目添加(静态,因此它不是动画)Default.png-splash-screen 来模拟这一点。
In my opinion, though, you should rather invest in a better performance. For example: do you link your app with unused frameworks (eg. QuartzCore, MapKit) or something.
Starting an app should take 2 seconds max. At this time, you don't really need to show your user an activity-indicator. If it does take longer, you should take a look at what makes it so slow, rather than trying to hide it.
不过,在我看来,你应该投资于更好的表现。例如:您是否将您的应用与未使用的框架(例如 QuartzCore、MapKit)或其他东西联系起来。
启动应用最多需要 2 秒。此时,您实际上不需要向用户显示活动指示器。如果确实需要更长的时间,您应该看看是什么让它变得如此缓慢,而不是试图隐藏它。

