添加從啓動畫面到運行的程序之間的平滑切換 app
最簡單的「啓動畫面切換」是從默認圖像淡出,而UI同時淡入。爲了讓默認圖像淡出屏幕,首先須要一個視圖,有它顯示這幅圖像,而後把這個視圖淡出。步驟以下: ide
建立一個可用於任何項目的簡單的視圖控制器,由它使用定製的啓動屏幕圖像,並定義一個執行淡出的-hide方法。
代碼以下:
XYSplashScreen.h 動畫
#import <UIKit/UIKit.h> #import "XYSplashScreenDelegate.h" @interface XYSplashScreen : UIViewController @property (nonatomic, retain) UIImage *splashImage; @property (nonatomic, assign) BOOL showsStatusBarOnDismissal; @property (nonatomic, assign) IBOutlet id<XYSplashScreenDelegate> delegate; - (void)hide; @end
將委託單獨寫到一個頭文件裏
代碼以下
XYSplashScreenDelegate.h atom
#import <Foundation/Foundation.h> @class XYSplashScreen; @protocol XYSplashScreenDelegate <NSObject> @optional - (void) splashScreenDidAppear:(XYSplashScreen *)splashScreen; - (void) splashScreenWillDisappear:(XYSplashScreen *)splashScreen; - (void) splashScreenDidDisappear:(XYSplashScreen *)splashScreen; @endXYSplashScreen.m
#import "XYSplashScreen.h" @interface XYSplashScreen () @end @implementation XYSplashScreen - (void)dealloc{ [_splashImage release], _splashImage = nil; [super dealloc]; } - (void)loadView{ UIImageView *iv = [[UIImageView alloc] initWithImage:self.splashImage]; iv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; iv.contentMode = UIViewContentModeCenter; self.view = iv; [iv release]; } - (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; SEL didAppearSelector = @selector(splashScreenDidAppear:); if ([[self delegate] respondsToSelector:didAppearSelector]) { [[self delegate] splashScreenDidAppear:self]; } [self performSelector:@selector(hide) withObject:nil afterDelay:0]; } - (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; if ([[self delegate] respondsToSelector:@selector(splashScreenWillDisappear:)]) { [[self delegate] splashScreenWillDisappear:self]; } } - (void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; if ([[self delegate] respondsToSelector:@selector(splashScreenDidDisappear:)]) { [[self delegate] splashScreenDidDisappear:self]; } self.splashImage = nil; } - (void)hide{ if (self.showsStatusBarOnDismissal) { UIApplication *app = [UIApplication sharedApplication]; [app setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade]; } [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - getter - (UIImage *)splashImage{ if (_splashImage == nil){ self.splashImage = [UIImage imageNamed:@"Default.png"]; } return _splashImage; } @end-(void)loadView,將view屬性設置爲一個以居中的圖像充滿屏幕的圖像視圖。
-(UIImage *)splashImage,splashImage是能夠寫的,因此必要時咱們能夠定製切換圖像,這裏只是用Default.png做爲啓動圖像。 code
在appdelegate裏設置啓動畫面,代碼以下:
XYAppDelegate.h orm
#import <UIKit/UIKit.h> #import "XYSplashScreen.h" @class XYViewController; @interface XYAppDelegate : UIResponder <UIApplicationDelegate,XYSplashScreenDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) XYViewController *viewController; @property (strong, nonatomic) XYSplashScreen *splashScreen; @end
XYAppDelegate.m ip
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = [[[XYViewController alloc] initWithNibName:@"XYViewController" bundle:nil] autorelease]; //self.window.rootViewController = self.viewController; [self.window addSubview:self.viewController.view]; self.splashScreen = [[[XYSplashScreen alloc] init] autorelease]; self.splashScreen.showsStatusBarOnDismissal = YES; self.splashScreen.delegate = self; self.splashScreen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self.viewController presentViewController:self.splashScreen animated:NO completion:nil]; [self.window makeKeyAndVisible]; return YES; }
並在XYAppDelegate.m添加委託方法: get
- (void)splashScreenDidAppear:(XYSplashScreen *)splashScreen{ NSLog(@"%s",__PRETTY_FUNCTION__); } - (void)splashScreenDidDisappear:(XYSplashScreen *)splashScreen{ NSLog(@"%s",__PRETTY_FUNCTION__); } - (void)splashScreenWillDisappear:(XYSplashScreen *)splashScreen{ NSLog(@"%s",__PRETTY_FUNCTION__); self.splashScreen = nil; }
最後將啓動畫面的showStatusBarOnDismissal屬性設置爲YES,好了,這樣就啓動圖像的淡出效果就製做好了, it
關於ipad的設備旋轉爲題,能夠設置一張,1024*1024像素的啓動圖像,這個大小足夠知足這兩個方向的最大屏幕尺寸,由於咱們的代碼可以讓圖像充滿屏幕並始終居中。 io
代碼下載:連接地址