gpt4 book ai didi

ios - iPhone 应用程序 : avoiding white screen after splash screen. 让闪屏停留,在 UIWebview 加载后隐藏它?闪屏没有正确隐藏

转载 作者:可可西里 更新时间:2023-11-01 05:46:42 26 4
gpt4 key购买 nike

对于 iPhone 应用程序,我们的目标很简单:显示初始页面,然后在 UIWebview 准备好显示其页面时将其隐藏。

我们需要默认启动画面一直停留到 UIWebview 准备好显示为止。否则,会短暂出现白屏。

不幸的是,启动画面在我们让它逗留后无法隐藏。默认启动画面仍然可见,隐藏了 UIWebview。

我们知道这可能违反了 Apple 准则。

这更像是一个原型(prototype),我们想了解我们做错了什么。有什么线索吗?

我们正在使用 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

当前 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 中的所有代码。在您的 Root View Controller 中添加一个名为“splash”的 UIImageView 类的实例变量。

现在在 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 加载完成回调方法/ block 中

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

[splash removeFromSuperView];
});

因此 dispatch_once 确保该方法将在应用程序的生命周期内运行一次且仅运行一次。

编辑:

得到你的回调:

在 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];
});
}

关于ios - iPhone 应用程序 : avoiding white screen after splash screen. 让闪屏停留,在 UIWebview 加载后隐藏它?闪屏没有正确隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11023198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com