iPad横向模式 - 如何添加两个启动画面

时间:2013-05-29 08:59:32

标签: ios ipad xcode4 splash-screen dynamic-splash-screen

经过数月的编码后,我已经成功完成了我的应用。现在我正在尝试将初始的Splash Screen图像添加到我的应用程序中。我该怎么办? 我有两张图片:一张是公司徽标,另一张是 app logo (这些启动画面用于隐藏加载时间)。我已经找到了可能答案的每个地方,但最终没有解决方案。当我命名单个图片 Default-Landscape.png 并运行Ipad应用程序时 - 图像显示直到主视图控制器加载,但我希望第一个图像显示为1秒,然后淡出到第二张图像 1秒后显示主视图控制器(原始应用程序页面)。

我已经检查了各种答案,但似乎都没有 - Xcode 4 and iPad2 splash screen issue Setting splash images for portrait and landscape mode - IPad 等等。

这是我的代码 - AppDelegate.h

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    NSArray *docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docPath=[docDir objectAtIndex:0];
    databasePath=[docPath stringByAppendingPathComponent:@"something.sqlite"];
    //databasePath1=[docPath stringByAppendingPathComponent:@"something.sqlite"];

    NSString *bundlePath=[[NSBundle mainBundle]pathForResource:@"something" ofType:@"sqlite"];
    NSFileManager *mngr=[NSFileManager defaultManager];
    if ([mngr fileExistsAtPath:databasePath]) {
        NSLog(@"File Exists");
    }
    else
    {    
        [mngr copyItemAtPath:bundlePath toPath:databasePath error:NULL];
        NSLog(@"File Created");
    }

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

我现在该怎么办?我应该使用uiImageView创建一个新类(splashScreen 1和splashScreen 2)并更改 didFinishLaunchingWithOptions 方法吗?任何帮助将不胜感激。

更新日期:2013年5月30日

Appdelegate.h

#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "FBInteract.h"
#import "splashScreen1.h"


sqlite3 *dbHandler;
NSString *databasePath;
@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

    UIWindow *window;
    UINavigationController *naviObj;
}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;

@property (strong, nonatomic) UINavigationController *naviObj;

@end

Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    splashScreen1 *splashScreen1PageObj=[[splashScreen1 alloc]init];

    self.naviObj = [[UINavigationController alloc]initWithRootViewController:splashScreen1PageObj];

    self.window.rootViewController = self.naviObj; 

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];

    [self.window makeKeyAndVisible];

    return YES;

}

splashScreen1.h splashScreen2.h

#import <UIKit/UIKit.h>
#import "splashScreen2.h"

@interface splashScreen1 : UIViewController

{
    IBOutlet UIImageView *imgObjSplashImage; //  IBOutlet UIImageView *objSplashImage; - in splashScreen2.h

}

-(void)hideSplash;

-(void)navigationToMain;

-(void)showSplash;

@end

splashScreen1.m splashScreen2.m

- (void)viewDidLoad
{

    // Do any additional setup after loading the view from its nib.

    [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(showSplash) userInfo:nil repeats:NO];

    self.navigationController.navigationBarHidden=YES;

    [imgObjSplashImage setAlpha:0]; //objSplashImage instead of imgObjSplashImage in splashScreen2

    [super viewDidLoad];

}

-(void)showSplash{


    [UIImageView beginAnimations:nil context:NULL];

    [UIImageView setAnimationDuration:1.2];

    [imgObjSplashImage setAlpha:1]; //objSplashImage instead of imgObjSplashImage in splashScreen2

    [UIImageView commitAnimations];

    [NSTimer scheduledTimerWithTimeInterval:2.4 target:self selector:@selector(hideSplash) userInfo:nil repeats:NO];

}

    -(void)hideSplash{


        [UIImageView beginAnimations:nil context:NULL];

        [UIImageView setAnimationDuration:1.4];

        [imgObjSplashImage setAlpha:0]; //objSplashImage instead of imgObjSplashImage in splashScreen2

        [UIImageView commitAnimations];

        [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(navigationToMain) userInfo:nil repeats:NO];

    }

    #pragma mark
#pragma mark - Navigation Coding

-(void)navigationToMain {

    //[self dismissModalViewControllerAnimated:YES];

    [self dismissViewControllerAnimated:NO completion:Nil];

    ViewController *ViewControllerPageObj=[[ViewController alloc]init];

    [self.navigationController pushViewController:ViewControllerPageObj animated:NO];

    [ViewControllerPageObj release];

    //[self presentViewController:ViewControllerPageObj animated:NO completion:Nil];

}

-(void)viewWillAppear:(BOOL)animated {

    self.navigationController.navigationBarHidden=YES;

}

问题是ViewController(主ViewController)没有加载...我在ViewController.m的viewDidLoad部分中收到错误说“消息发送到deallocated instance”

RootViewController - &gt; splashScreen1 - &gt; splashScreen2工作正常(所有动画渐入淡出)但最终的ViewController没有加载..

1 个答案:

答案 0 :(得分:1)

为此你肯定想要另一个VC,让我们称之为StartViewController。添加一个imageView,块旋转和setTimer:

[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(moveOnAndChangeVC:) userInfo:nil repeats:NO];

如果您开发NavigationControl应用程序,您还应该更改rootVC,例如:

+(void)replaceRootVCWithVC:(id)vc {
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:[[sharedAppDelegate navigationController] viewControllers]];
    if(![viewControllers isEmpty])
        [viewControllers replaceObjectAtIndex:0 withObject:vc];
    else
        [viewControllers addObject:vc];

    [[sharedAppDelegate navigationController] setViewControllers:viewControllers];
}

修改

您应该使用初始化程序更改appDelegate行:

self.viewController = [[[StartViewController alloc] initWithNibName:@"StartViewController" bundle:nil] autorelease];

这应该将您的VC称为rootVC。​​

然后在StartViewController.m中你应该识别设备(这是一个仅限iPad的应用程序,对吗?不是通用的)和方向:

+(NSString*)recognizeDeviceAndOrientation {

    NSMutableString * returnString;
    if([StartViewController deviceInterfaceOrientation] == UIInterfaceOrientationLandscapeLeft ||    [StartViewController deviceInterfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
        returnString = @"Default-Landscape";
    } else {
        returnString = @"Default-Portrait";
    }

    if([UIScreen mainScreen].scale == 2.0)
        return [NSString stringWithFormat:@"%@%@",returnString,@"@2x"];;
    else
        return returnString;
}

+(UIInterfaceOrientation)deviceInterfaceOrientation {
    return [[UIApplication sharedApplication] statusBarOrientation];
}

这将返回一个带有DefaultScreen名称并且方向正确的字符串。如果你只处理一个方向而忘记它。

然后在viewDidLoad中添加imageView:

- (void)viewDidLoad {
    [super viewDidLoad];    
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[StartViewController recognizeDeviceAndOrientation]] autorelease];
   [imageView setFrame:self.frame];
   [imageView setContentMode:UIViewContentModeScaleToFill];
   [self.view addSubview:imageView];

   [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(moveOnAndChangeVC:) userInfo:nil repeats:NO];
}

这将调用计时器并设置imageView。

-(IBACtion)moveOnAndChangeVC:(id)sender {
   //if you using navigation app
   ViewController *vc = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];
   [self.navigationController pushViewController:vc animated:NO];
}

这会奏效。然后,如果你想摆脱“后退按钮”,只需改变一个rootView控制器就像我在开始时说的那样。在ViewController类中执行。