本文介绍了iPhone应用程序:启动画面后避免白屏。让Flash屏幕流延,在UIWebview加载后隐藏它?飞溅屏幕没有正确隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的目标对于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 :



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的实例变量。

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< viewController.m中的UIWebViewDelegate>

in viewController.h -> viewC : UIViewController < UIWebViewDelegate >

-(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应用程序:启动画面后避免白屏。让Flash屏幕流延,在UIWebview加载后隐藏它?飞溅屏幕没有正确隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:18