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

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

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

创建一个可用于任何项目的简单的视图控制器,由它使用定制的启动屏幕图像,并定义一个执行淡出的-hide方法。

代码如下:

XYSplashScreen.h

#import

#import "XYSplashScreenDelegate.h"

@interface XYSplashScreen : UIViewController

@property (nonatomic, retain) UIImage *splashImage;

@property (nonatomic, assign) BOOL showsStatusBarOnDismissal;

@property (nonatomic, assign) IBOutlet id delegate;

- (void)hide;

@end

将委托单独写到一个头文件里

代码如下

XYSplashScreenDelegate.h

#import

@class XYSplashScreen;

@protocol XYSplashScreenDelegate

@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

#import "XYSplashScreen.h"

@class XYViewController;

@interface XYAppDelegate : UIResponder

@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像素的启动图像,这个大小足够满足这两个方向的最大屏幕尺寸,因为我们的代码能够让图像充满屏幕并始终居中。

代码下载:链接地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值