iPhone开发之启动画面及动画
一、静态图片的设置
iOS设备现在有三种不同的分辨率:iPhone 320x480、iPhone 4 640x960、iPad 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]; }
上述代码实现了一个简单的进度启动动画,供大家参考。