iOS——添加基本的启动画面切换

添加从启动画面到运行的程序之间的平滑切换

最简单的“启动画面切换”是从默认图像淡出,而UI同时淡入。为了让默认图像淡出屏幕,首先需要一个视图,有它显示这幅图像,然后把这个视图淡出。步骤如下:

创建一个可用于任何项目的简单的视图控制器,由它使用定制的启动屏幕图像,并定义一个执行淡出的-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

#import <Foundation/Foundation.h>

@class XYSplashScreen;

@protocol XYSplashScreenDelegate <NSObject>
@optional
- (void) splashScreenDidAppear:(XYSplashScreen *)splashScreen;
- (void) splashScreenWillDisappear:(XYSplashScreen *)splashScreen;
- (void) splashScreenDidDisappear:(XYSplashScreen *)splashScreen;

@end
XYSplashScreen.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作为启动图像。

在appdelegate里设置启动画面,代码如下:
XYAppDelegate.h

#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

- (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添加委托方法:

- (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,好了,这样就启动图像的淡出效果就制作好了,

关于ipad的设备旋转为题,可以设置一张,1024*1024像素的启动图像,这个大小足够满足这两个方向的最大屏幕尺寸,因为我们的代码能够让图像充满屏幕并始终居中。

代码下载:链接地址

你可能感兴趣的:(ios,Objective-C)