4

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

在 UIWebview 准备好显示之前,我们需要默认的闪屏。否则,会短暂出现白屏。

不幸的是,启动画面在我们让它徘徊后无法隐藏。默认的初始屏幕仍然可见,隐藏了下面的 UIWebview。

我们理解这可能违反 Apple 准则。

对于原型来说,这比什么都重要,我们想了解我们做错了什么。有什么线索吗?

我们使用的是 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

视图控制器.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
4

3 回答 3

3

这是实现它的一种方法,首先摆脱 AppDelegate 中的所有代码。在你的根视图控制器中添加一个名为“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 加载完成回调方法/块

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];
        });
}
于 2012-06-13T22:19:48.370 回答
0

Default.png 默认用作启动画面,然后自动删除。但是您创建另一个实例并自己将其放置到视图中。删除这部分代码

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

从你的application:didFinishLaunchingWithOptions:方法

于 2012-06-13T21:53:18.507 回答
0

[第一个 ViewController.m]

闪屏没有消失的原因是因为在将闪屏添加到屏幕窗口之前调用了ViewController中的ViewDidLoad。

具体来说,ViewDidLoad 方法是在 [self.window makeKeyAndVisible] 周围调用的,就在调用启动画面之前。

[第二个ViewController.m]

由于两个原因,您可能不想在此处添加启动屏幕:1)在 ViewWillAppear 处,可能尚未创建视图;和 2) ViewWillAppear: 可以被多次调用,你可能并不真正想要。

【可行的方法】

一种可行的方法(我正在使用的方法)是您的第一次和第二次试验的组合。我是说

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

完毕

于 2012-07-18T02:31:42.743 回答