iPhone開發之啟動畫面及動畫


一、靜態圖片的設置

iOS設備現在有三種不同的分辨率:iPhone 320x480iPhone 4 640x960iPad 768x1024。以前程序的啟動畫面(圖片)只要准備一個 Default.png 就可以了,但是現在變得復雜多了。下面就是 CocoaChina 會員做得總結

  如果一個程序,既支持iPhone又支持iPad,那么它需要包含下面幾個圖片:

Default-Portrait.png iPad專用豎向啟動畫面 768x1024或者768x1004

Default-Landscape.png iPad專用橫向啟動畫面 1024x768或者1024x748

Default-PortraitUpsideDown.png iPad專用豎向啟動畫面(Home按鈕在屏幕上面),可省略 768x1024或者768x1004

Default-LandscapeLeft.png iPad專用橫向啟動畫面,可省略 1024x768或者1024x748

Default-LandscapeRight.png iPad專用橫向啟動畫面,可省略 1024x768或者1024x748

Default.png iPhone默認啟動圖片,如果沒有提供上面幾個iPad專用啟動圖片,則在iPad上運行時也使用Default.png(不推薦) 320x480或者320x460

Default@2x.png iPhone4啟動圖片640x960或者640x920

 

  為了在iPad上使用上述的啟動畫面,你還需要在info.plist中加入key: UISupportedInterfaceOrientations。同時,加入值UIInterfaceOrientationPortrait, UIInterfacOrientationPortraitUpsideDown, UIInterfaceOrientationLandscapeLeft, UIInterfaceOrientationLandscapeRight

 

二、設置靜態圖片停留的時間

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中使用下面集中方法都可以:

1、sleep(3);

2、[NSThread sleepForTimeInterval:5.0];

3、[selfperformSelector:@selector(startupview) withObject:nilafterDelay:3];

 

三、設置啟動動畫

在appDelegate中加載啟動動畫的controller,在開啟動畫的controller中載入首頁controller,通過透明度來設置淡入和淡出。

appDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate> {
     Startupscreen *startupscreen;
}

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) Startupscreen *startupscreen;

@end

appDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSLog(@"didFinishLaunchingWithOptions");
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    startupscreen = [[Startupscreen alloc] initWithNibName:@"Startupscreen" bundle:nil];
    [self.window addSubview:startupscreen.view];
    [self.window makeKeyAndVisible];
    return YES;
}

Startupscreen.h

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

@interface Startupscreen : UIViewController{

    NSTimer *timer;
    UIImageView *splashImageView;
    UINavigationController *nav;
    ViewController *myviewcontroller;
}

@property (nonatomic,retain) NSTimer *timer;
@property (nonatomic,retain) UIImageView *splashImageView;
@property (nonatomic,retain) UINavigationController *nav;
@property (nonatomic,retain) ViewController *myviewcontroller;

@end

Startupscreen.m

#import "Startupscreen.h"
#import "ViewController.h"

@interface Startupscreen ()

@end

@implementation Startupscreen
@synthesize timer;
@synthesize splashImageView;
@synthesize nav;
@synthesize myviewcontroller;



int flag;
NSTimer *timer;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
        CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
        UIView *view = [[UIView alloc] initWithFrame:appFrame];
        view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        self.view = view;
        [view release];
    
    
    
    flag = 0;
    
    splashImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"c618Default1.png"]];
    splashImageView.frame = CGRectMake(0, 0, 768, 1004);
    [self.view addSubview:splashImageView];

    timer = [NSTimer scheduledTimerWithTimeInterval:0.6 
                                             target:self 
                                           selector:@selector(addLabel) 
                                           userInfo:nil 
                                            repeats:YES];
    
    myviewcontroller = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    myviewcontroller.view.alpha = 0.0;
    //[self.view addSubview:myviewcontroller.view];
    nav = [[UINavigationController alloc] init];
    [nav pushViewController:myviewcontroller animated:NO];
    [self.view addSubview:nav.view];
    
    
    timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
   
    
    
}

- (void)addLabel{
    
    flag++;
    if (flag <=5) {
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(200+50*flag,600,40, 40)];
        label1.text = @"123";
        label1.font = [UIFont systemFontOfSize:23];
        label1.textColor = [UIColor whiteColor];
        [self.view addSubview:label1];
        [label1 release];
    }
    
}

- (void)fadeScreen
{
    [UIView beginAnimations:nil context:nil]; // begins animation block
    [UIView setAnimationDuration:0.75];        // sets animation duration
    [UIView setAnimationDelegate:self];        // sets delegate for this block
    [UIView setAnimationDidStopSelector:@selector(finishedFading)];   // calls the finishedFading method when the animation is done (or done fading out)    
    self.view.alpha = 0.0;       // Fades the alpha channel of this view to "0.0" over the animationDuration of "0.75" seconds
    [UIView commitAnimations];   // commits the animation block.  This Block is done.
}


- (void) finishedFading
{
    
    [UIView beginAnimations:nil context:nil]; // begins animation block
    [UIView setAnimationDuration:0.75];        // sets animation duration
    self.view.alpha = 1.0;   // fades the view to 1.0 alpha over 0.75 seconds
    myviewcontroller.view.alpha = 1.0;
    [UIView commitAnimations];   // commits the animation block.  This Block is done.
    
    for(UIView *mylabelview in [self.view subviews])
    {
        if ([mylabelview isKindOfClass:[UILabel class]]) {
            [mylabelview removeFromSuperview];
        }
    }
    
    [splashImageView removeFromSuperview];
}

上述代碼實現了一個簡單的進度啟動動畫,供大家參考。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM