Iphone 在默认图像上添加activityindicator-给定代码
我有一个图像名称Default.png。这将在应用程序启动时加载。现在我需要添加一个活动指示器。因此,它将旋转,使应用程序看起来很漂亮 我们知道在加载应用程序时无法添加任何UI组件。因此我想在Iphone 在默认图像上添加activityindicator-给定代码,iphone,objective-c,cocoa-touch,Iphone,Objective C,Cocoa Touch,我有一个图像名称Default.png。这将在应用程序启动时加载。现在我需要添加一个活动指示器。因此,它将旋转,使应用程序看起来很漂亮 我们知道在加载应用程序时无法添加任何UI组件。因此我想在App delegate中的didfishlaunchingwithoptions方法中添加UIActivityIndicator 以下是我遵循的步骤。 我添加了一个视图 添加了default.png 新增活动指标 然后 但没有什么是偶然的 帮助如何编写代码?我想你想做一个“自定义启动屏幕”不是吗 您不能以
App delegate
中的didfishlaunchingwithoptions
方法中添加UIActivityIndicator
以下是我遵循的步骤。
我添加了一个视图
添加了default.png
新增活动指标
然后
但没有什么是偶然的
帮助如何编写代码?我想你想做一个“自定义启动屏幕”不是吗
您不能以这种方式添加活动指示器
您需要添加一个视图控制器,并在此视图控制器上使用“活动指示器”执行您需要的操作 您的思路是正确的,但您确实需要暂时向窗口中添加另一个
UIView
(不一定是另一个UIViewContoller
)。我经常做这种事。以下是一些适用于您的应用程序的代码dFinishLaunching:withOptions:
方法:
[window makeKeyAndVisible]
...
// Create and show an overlay view with a spinner
UIImage *defaultImage = [UIImage imageNamed:@"Default.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:defaultImage];
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
// Position the spinner appropriately for our splash image so it's not obscured
CGRect frame = spinner.frame;
frame.origin.x = imageView.frame.size.width / 2 - frame.size.width / 2;
frame.origin.y = imageView.frame.size.height / 5 * 4 - frame.size.height / 2;
spinner.frame = frame;
[spinner startAnimating];
[spinner setHidesWhenStopped:YES];
startupView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[startupView addSubview:imageView];
[imageView release];
[startupView addSubview:spinner];
[spinner release];
[window addSubview:startupView];
ivarstartupView
属于应用程序委托,稍后在启动序列中,另一种方法通过将其淡出视图来优雅地将其删除:
UIActivityIndicatorView *spinner = [[startupView subviews] lastObject];
[spinner stopAnimating];
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^(void) {
startupView.alpha = 0.0;
} completion:^(BOOL finished) {
[startupView removeFromSuperview];
[startupView release];
}];
值得一提的是,在苹果看来,像这样的启动屏幕并不是“推荐”的启动屏幕。但他们似乎并不拒绝拥有它们的应用。我从哪里调用此ViewCOntroller?从哪个方法-(BOOL)application:(UIApplication*)application使用options:(NSDictionary*)launchOptions完成启动。使用此方法,您可以将SplashScreenViewController设置为self.window.rootViewController;当然,您需要创建一个新的视图控制器调用SplashScreenViewControlle如何删除它?它是ApplicationIDBecomeActive吗?我尝试添加了您提到的
SplashScreenViewController*SplashScreenViewController=[SplashScreenViewController alloc]]initWithNibName:@“SplashScreenViewController”捆绑包:nil;self.window.rootViewController=SplashScreenViewController
。但我看不到活动指标。不知道它是不是很快就出现了。如果是这样的话,我怎样才能在那里停留2秒钟。谢谢,我应该在哪里添加删除活动指示器代码(代码的第二部分)。我应该在哪个方法中包含它?以及[window makeKeyAndVisible]做什么?删除启动屏幕的代码应该放在您完成启动和初始化的任何位置。例如,您可以从applicationdFinishLaunching
调用另一个方法(或在异步块中调用),该方法对UI进行初始化或加载一些数据。完成后,您可以移除飞溅物makeKeyAndVisible
告诉窗口使其自身可见并位于任何其他窗口的顶部。
UIActivityIndicatorView *spinner = [[startupView subviews] lastObject];
[spinner stopAnimating];
[UIView animateWithDuration:1.0
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^(void) {
startupView.alpha = 0.0;
} completion:^(BOOL finished) {
[startupView removeFromSuperview];
[startupView release];
}];