iOS开发实用技术iOS高级文章

LaunchScreen启动图和自定义启动图

2017-08-16  本文已影响104人  鞋底没纹易摔跤

LaunchScreen启动图

1.在LaunchScreen.storyboard中添加全屏的UIImageView并添加对应图片(可自己添加控件)
2.下方红框的Launch Screen File选择LaunchScreen
3.此时运行app应该能正常显示了
4.如果启动图显示过快,可在application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加代码[NSThread sleepForTimeInterval:1.0](比如放在第一行)。


自定义启动图

首先在Assets.xcassets中右键新建Launch Image,如下图

新建之后默认名字是LaunchImage(如果有多个的话会LaunchImage-1之类这样显示)。默认界面如下:


有些图片适配是不需要的,比如iOS 5、6 和 Landscape(横屏)iOS 8、9,可以在上图右边红框内去掉相应的勾选,如下图


下面是参考尺寸
iPhone Portrait iOS 8-Retina HD 5.5 @3x -> 1242×2208
iPhone Portrait iOS 8-Retina HD 4.7 @2x -> 750×1334
iPhone Portrait iOS 7,8-2x @2x -> 640×960
iPhone Portrait iOS 7,8-Retina 4 @2x -> 640×1136
iPhone Portrait iOS 5,6-1x @1x -> 320×480
iPhone Portrait iOS 5,6-2x @2x -> 640×960
iPhone Portrait iOS 5,6-Retina4 @2x -> 640×1136
上面蓝色字体的是对应上图中四个位置的图片尺寸,然后把图片改成对应的像素尺寸,再拖到上图中对应位置即可(文件中的Contents.json会自动配置,不需要手动配置)。

注意:图片需要是png格式。假如是其他格式直接把后缀名改成png格式的话虽然表面没问题,但实际程序运行中是不能识别的(亲测)。所以如果是其他格式的图片最好通过格式转换器转换成png格式,比如用ps导出png格式。

配置完毕如图:


然后Targets -> General中设置启动图的数据源



然后此时运行程序应该能看到效果了(必须注意图片不能手动改后缀名)

如果想要在启动图中添加一些动态效果,比如



主要思路:
在上面设置了LaunchImage基础之上,app启动时就会自动显示启动图,然后在AppDelegate中的window中添加一个控件,比如UIImageView,再对该控件设置成启动动画的图片并添加动画。(即启动图启动完毕之后就会显示该UIImageView,使得整体看起来还是在启动过程中)。那UIImageView怎么知道应该显示的是那种尺寸的图片?参照getLaunchImage函数,主要通过获取LaunchImage中所有图片,然后通过循环根据屏幕大小条件和横屏竖屏条件筛选出合适的图片。

示例代码:

Application:didFinishLaunchingWithOptions函数中

self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];self.window.rootViewController=[[JMMainViewController alloc]init];
[self.window makeKeyAndVisible];
[self showLaunchImageView];//调用启动图动画


//启动动画
-(void)showLaunchImageView{
    UIImageView* launchImageView = [[UIImageView alloc]initWithFrame:self.window.bounds];
    launchImageView.image = [self getLaunchImage];
    [self.window addSubview:launchImageView];
    [self.window bringSubviewToFront:launchImageView];
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:1.2 animations:^{
            launchImageView.alpha = 0.0;
            launchImageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
         }completion:^(BOOL finished) {
            [launchImageView removeFromSuperview];
        }];
    });
}


//根据屏幕尺寸自动选择合适尺寸的启动图片
-(UIImage*)getLaunchImage{
   
    UIImage *launchImage = nil;
    NSString* viewOrientation = nil;
    CGSize viewSize = [UIScreen mainScreen].bounds.size;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
   
    //是否左横屏或者右横屏
    if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {  
    //home键所在方向为left或者right
    viewOrientation = @"Landscape";  //横屏
    }else{
        viewOrientation =@"Portrait";  //竖屏
    }
   
    //取得所有启动图片
    NSArray *imagesDictionary = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
   
    for (NSDictionary *dict in imagesDictionary) {
       CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
       //判断图片的尺寸和横竖屏是否和当前屏幕符合
       if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]) {
           launchImage = [UIImage imageNamed:dict[@"UILaunchImageName"]];
       }
    }
   
    return launchImage;
}
上一篇下一篇

猜你喜欢

热点阅读