过程如下:
1.先加载系统Launch Images Sourc文件中对应的静态图片
2.再执行didFinishLaunchingWithOptions:代码,运行项目程序。
3.最后在didFinishLaunchingWithOptions方法中加载动态图片。
注意:加载静态的启动页之后,再执行程序,然后再加载动态图。有的app执行程序加载的数据很多,所以静态页和动态页的衔接点会空出很多时间,给人一种卡的现象。
步骤一:
- 不同屏幕尺寸下静态图片(A)放到系统Launch Images Sourc对应的 自己创建的文件
LaunchImage-3中。每张静态图片(A)放到对应的位置就行。
静态图片(A)
步骤二:
-
动态图片(B)放到didFinishLaunchingWithOptions:方法中。需要用代码(C)判断当前机型,然后将每张动态图片(B)放到对应的位置上。
代码(C)
代码如下
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
...
...
...
AS_ZBEmptyViewController *emptyVC = [[AS_ZBEmptyViewController alloc]init];
_window.rootViewController = emptyVC;
[_window makeKeyAndVisible];
// 加载动态图片
[self setUpLaunchScreen];
...
...
...
}
//添加启动图
- (void)setUpLaunchScreen{
NSURL *fileUrl;
if (APP_SCREEN_HEIGHT==480) {// 屏幕的高度为480
fileUrl = [[NSBundle mainBundle] URLForResource:@"startup1" withExtension:@"gif"]; // 加载GIF图片
}else if(APP_SCREEN_HEIGHT==568) {// 屏幕的高度为568
//5 5s 0.853333 0.851574
fileUrl = [[NSBundle mainBundle] URLForResource:@"startup2" withExtension:@"gif"]; // 加载GIF图片
}else if(APP_SCREEN_HEIGHT==667){// 屏幕的高度为667
//6 6s 7 1.000000 1.000000
fileUrl = [[NSBundle mainBundle] URLForResource:@"startup3" withExtension:@"gif"]; // 加载GIF图片
}else if(APP_SCREEN_HEIGHT==736){// 屏幕的高度为736
//6p 6sp 7p
fileUrl = [[NSBundle mainBundle] URLForResource:@"startup4" withExtension:@"gif"]; // 加载GIF图片
}else if(APP_SCREEN_HEIGHT==812){// 屏幕的高度为812 375.000000 812.000000
// x
fileUrl = [[NSBundle mainBundle] URLForResource:@"startup5" withExtension:@"gif"]; // 加载GIF图片
}else{
}
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef) fileUrl, NULL); //将GIF图片转换成对应的图片源
size_t frameCout = CGImageSourceGetCount(gifSource); // 获取其中图片源个数,即由多少帧图片组成
NSMutableArray *frames = [[NSMutableArray alloc] init]; // 定义数组存储拆分出来的图片
for (size_t i = 0; i < frameCout; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); // 从GIF图片中取出源图片
UIImage *imageName = [UIImage imageWithCGImage:imageRef]; // 将图片源转换成UIimageView能使用的图片源
[frames addObject:imageName]; // 将图片加入数组中
CGImageRelease(imageRef);
}
self.customLaunchImageView = [[UIImageView alloc]initWithFrame:self.window.bounds];
NSLog(@"宽高为%lf %lf",self.window.bounds.size.width,self.window.bounds.size.height);
self.customLaunchImageView.userInteractionEnabled = YES;
self.customLaunchImageView.animationImages = frames; // 将图片数组加入UIImageView动画数组中
self.customLaunchImageView.animationDuration = 0.7; // 每次动画时长
[self.customLaunchImageView startAnimating]; // 开启动画,此处没有调用播放次数接口,UIImageView默认播放次数为无限次,故这里不做处理
[[UIApplication sharedApplication].keyWindow addSubview:self.customLaunchImageView];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self.customLaunchImageView];
//5秒后自动关闭
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self yourButtonClick];
});
}
- (void)yourButtonClick {
// 移动自定义启动图
if (self.customLaunchImageView) {
[UIView animateWithDuration:0.3 animations:^{
self.customLaunchImageView.alpha = 0;
} completion:^(BOOL finished) {
[self.customLaunchImageView removeFromSuperview];
self.customLaunchImageView = nil;
}];
}
}