原理:默认启动页面执行完成后,给Window添加一个UIView视图。UIView添加一个UIImageView(用于展示启动背景图)和UIlabel(用于展示版本信息)。
代码:
自定义一个UIView 仿照启动背景效果
LauntchScreenView.h文件
#import<UIKit/UIkit.h>
@interfaceLaunchScreenView :UIView
/**
*初始化对象
*
*@return返回当前对象*/
+ (instancetype)initView;
/**
*实例方法
*
*@param Image UIImage对象
*
*@return返回当前对象
*/
- (instancetype)initWithImage:(UIImage*)Image;
/**
*执行启动动画效果
*/
- (void)startAnimation;
@end
LauntchScreenView.m文件
#import"LaunchScreenView.h"
@interface LaunchScreenView()
@property(nonatomic,strong)UIImageView *launchImageView;
@property(nonatomic,strong)UILabel *lab;
@end
@implementation LaunchScreenView
+ (instancetype)initView {
UIImage*bgImage = [UIImage imageNamed:@"750-1334.png"];
return[[self alloc]initWithImage:bgImage];
}
- (instancetype)initWithImage:(UIImage*)Image {
if(self= [superinitWithFrame:[UIApplication sharedApplication].keyWindow.bounds]) {
_launchImageView= [[UIImageView alloc]initWithFrame:[UIApplication sharedApplication].keyWindow.bounds];
_launchImageView.image= Image;
[self addSubview:_launchImageView];
_lab= [[UILabel alloc]initWithFrame:CGRectMake(100,100,170,29)];
_lab.font= [UIFont systemFontOfSize:15];
_lab.textColor= [UIColor blackColor];
_lab.textAlignment=NSTextAlignmentCenter;
_lab.text=@"1.1.2";//此处动态获取版本号
[self addSubview:_lab];
}
return self;
}
- (void)startAnimation {
[[UIApplication sharedApplication].keyWindowaddSubview:self];
[[UIApplication sharedApplication].keyWindowbringSubviewToFront:self];
_launchImageView.alpha=0.99f;
_lab.alpha=0.99f;
[UIView animateWithDuration:1.0fanimations:^{
_launchImageView.alpha=1;
_lab.alpha=1;
}completion:^(BOOLfinished) {
[UIView animateWithDuration:1.0animations:^{
_launchImageView.alpha=0;
_lab.alpha=0;
}completion:^(BOOLfinished) {
[self removeFromSuperview];
}];
}];
}
@end
AppDelegate.m didFinishLaunchingWithOptions方法中 添加
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
[self.window makeKeyAndVisible];
LaunchScreenView*com = [LaunchScreenView initView];
[comstart Animation];
returnYES;
}