对于 iPhone 应用,我们的目标很简单:显示启动页面,然后在 UIWebview 准备好显示其页面时将其隐藏.
Our goal is simple for an iPhone app: show a splash page then hide it when a UIWebview is ready to show its page.
在 UIWebview 准备好显示之前,我们需要默认的闪屏.否则,会短暂出现白屏.
We need the default splash screen to linger until the UIWebview is ready to display. Otherwise, a white screen appears briefly.
不幸的是,启动画面在我们让它徘徊后无法隐藏.默认启动画面仍然可见,隐藏了下面的 UIWebview.
Unfortunately, the splash screen fails to hide after we make it linger. The default splash screen remains visible, concealing the UIWebview underneath.
我们了解这可能违反 Apple 准则.
We understand this may violate Apple guidelines.
这对于原型来说比什么都重要,我们想了解我们做错了什么.有什么线索吗?
This is more for a prototype than anything, and we would like to understand what we're doing wrong. Any clues?
我们使用的是 Xcode 4.2.
We're using Xcode 4.2.
//
// 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
//
//
// 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
当前 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 的实例变量.
Here's a way to achieve it, get rid of all the code in your AppDelegate first of all. In your root view controller add an instance variable of class UIImageView called "splash".
现在在 rootViewController.m 中:
Now in the 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 加载完成回调方法/块中
Now in your webView load completion callback method/block
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[splash removeFromSuperView];
});
因此 dispatch_once 确保该方法在应用程序的生命周期内只运行一次.
so the dispatch_once makes sure the method will run once and only once in the life time of the application.
获取您的回电:
在 viewController.h -> viewC : UIViewController <UIWebViewDelegate >
in viewController.h -> viewC : UIViewController < UIWebViewDelegate >
在 viewController.m 中
in 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];
});
}
这篇关于iPhone 应用程序:避免闪屏后出现白屏.让闪屏徘徊,在 UIWebview 加载后隐藏它?启动画面没有正确隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!