iPhone应用程序:闪屏后避免白屏。 让闪屏逗留,在UIWebview加载后隐藏它? 启animation面不能正确隐藏

我们的目标是简单的iPhone应用程序:显示一个启动页面,然后在UIWebview准备好显示其页面时将其隐藏。

我们需要默认的启animation面,直到UIWebview准备好显示。 否则,会短暂出现白色屏幕。

不幸的是,在我们让它stream连忘返之后,启animation面无法隐藏起来。 默认启animation面保持可见,隐藏下面的UIWebview。

我们了解这可能违反了苹果的指导方针。

这比原型更适合原型,我们想知道我们做错了什么。 任何线索?

我们正在使用Xcode 4.2。

AppDelegate.m:

// // AppDelegate.m // // Created by Macintosh User on 6/4/12. // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @implementation AppDelegate @synthesize window = _window; @synthesize viewController = _viewController; @synthesize imgv; - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; imgv = [[UIImageView alloc] init]; [imgv setImage:[UIImage imageNamed:@"Default.png"]]; [imgv setFrame:CGRectMake(0, 0, 320, 480)]; [self.window addSubview:imgv]; return YES; } @end 

ViewController.m:

 // // ViewController.m // // // Created by Macintosh User on 6/4/12. // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @implementation ViewController #pragma mark - View lifecycle - (void)viewDidLoad { CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0); UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; [webView setBackgroundColor:[UIColor clearColor]]; NSString *urlAddress = @"http://www.cnn.com"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; for (id subview in webView.subviews) if ([[subview class] isSubclassOfClass: [UIScrollView class]]) ((UIScrollView *)subview).bounces = NO; AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIImageView *imageView = appDelegate.imgv; [imageView removeFromSuperview]; [imageView setHidden:YES]; imageView = nil; [self.view addSubview:webView]; [self.view bringSubviewToFront:webView]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"Done loading UIWebView"); } @end 

Curret ViewController.m生成错误:

 // // ViewController.m // Stroll // // Created by Macintosh User on 6/4/12. // Copyright (c) 2012 __MyCompanyName__. All rights reserved. // #import "AppDelegate.h" #import "ViewController.h" @implementation ViewController @synthesize splash; #pragma mark - View lifecycle - (void)viewDidLoad { CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0); UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; webView.delegate = self; [webView setBackgroundColor:[UIColor clearColor]]; NSString *urlAddress = @"http://www.cnn.com"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; for (id subview in webView.subviews) if ([[subview class] isSubclassOfClass: [UIScrollView class]]) ((UIScrollView *)subview).bounces = NO; /* AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIImageView *imageView = appDelegate.imgv; [imageView removeFromSuperview]; [imageView setHidden:YES]; imageView = nil; */ [self.view addSubview:webView]; [self.view bringSubviewToFront:webView]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"Done loading UIWebView"); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ self.view.userInteractionEnabled = NO; splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; splash.image = [UIImage imageNamed:@"Default.png"]; [self.view addSubview:splash]; }); } -(void) webViewDidFinishLoad:(UIWebView *)webView { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [splash removeFromSuperView]; }); } @end 

这里有一个方法来实现它,首先摆脱你的AppDelegate中的所有代码。 在你的根视图控制器中添加一个名为“splash”的类UIImageView的实例variables。

现在在rootViewController.m中:

 -(void) viewWillAppear:(BOOL) animated { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ self.view.userInteractionEnabled = NO; splash = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds]; splash.image = [UIImage imageNamed:@"Default.png"]; [self.view addSubview:splash]; }); } 

现在在你的webView加载完成callback方法/块

 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [splash removeFromSuperView]; }); 

所以dispatch_once确保方法在应用程序的生命周期中只运行一次。

编辑:

为了得到你的callback:

在viewController.h – > viewC:UIViewController <UIWebViewDelegate>

在viewController.m中

 -(void) viewDidLoad{ CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0); UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame]; webView.delegate = self; [webView setBackgroundColor:[UIColor clearColor]]; NSString *urlAddress = @"http://www.cnn.com"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; } 

然后

 -(void) webViewDidFinishLoad:(UIWebView *)webView { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [splash removeFromSuperView]; }); } 

Default.png默认作为启animation面使用,然后自动删除。 但是你又创build了另一个实例,并将它自己放到视图中。 删除这部分代码

 imgv = [[UIImageView alloc] init]; [imgv setImage:[UIImage imageNamed:@"Default.png"]]; [imgv setFrame:CGRectMake(0, 0, 320, 480)]; [self.window addSubview:imgv]; 

从您的application:didFinishLaunchingWithOptions:方法

[First ViewController.m]

启animation面没有消失的原因是因为ViewController中的ViewDidLoad在启animation面被添加到屏幕窗口之前被调用。

具体来说,ViewDidLoad方法是在调用splash之前调用[self.window makeKeyAndVisible]。

[Second ViewController.m]

你可能不想在这里添加启animation面,原因有两个:1)在ViewWillAppear,视图可能还没有被创build; 和2)ViewWillAppear:可以被调用几次,你可能不是真的想要。

[可行的方法]

一个可行的方法(我正在使用的)是第一次和第二次试验的结合。 我的意思是

  1. 将启animation面添加到AppDelegate的窗口中;
  2. 做一些后台任务来获取数据(例如,或加载webview);
  3. 当任务完成后,发布通知。 注册到TaskDoneNotification的启animation面将处理必要的清理,并将其本身从其超级视图中移除。

完成