美文网首页UI
iOS设置动态启动页LaunchScreen

iOS设置动态启动页LaunchScreen

作者: T情不知所起一往而深 | 来源:发表于2019-03-20 16:41 被阅读0次

一.确定LaunchScreen File(这里用的是系统默认的LaunchScreen.storybord) 也可用自定义xib(需要第四部修改方法,通过NSBundle获取)

WeChat46651f66c179601a3cfa01d02e4f11a8.png

二.设置LaunchScreen.storybord 的storybord ID

WechatIMG5.jpeg

三.导入加载GIF需要的头文件

pod 'SDWebImage/GIF'
#import "FLAnimatedImageView+WebCache.h"

四.找到相对应的view

 @property (strong, nonatomic) UIView *lunchV;

 UIViewController *vc=[[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil]instantiateViewControllerWithIdentifier:@"LaunchScreen"];
 self.lunchView=vc.view;
 self.lunchV.frame =[UIScreen mainScreen].bounds;
 [self.window addSubview:self.lunchV];
 [self.window bringSubviewToFront:self.lunchV];
 [self loadNetGif];
 [self start];

五.加载本地/网络GIF

//加载网络GIF
-(void)loadNetGif{
    FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] initWithFrame:self.lunchV.bounds];
    NSURL *url = [NSURL URLWithString:@"http://xxxxxx1.gif"];
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"image"]];
    [self.lunchV addSubview:imageView];
}
//加载本地GIF
-(void)loadNativeGif{
    FLAnimatedImageView *imgView = [[FLAnimatedImageView alloc] initWithFrame:self.lunchV.bounds];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]]pathForResource:@"loading" ofType:@"gif"];
    NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
    imgView.backgroundColor = [UIColor clearColor];
    imgView.animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData];
    [self.lunchV addSubview:imgView];
}

六.设置GIF加载时长

-(void)start{
    __block NSInteger second = 6;//加载时长
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"%ld",second);
            if (second == 0) {
                [self.lunchV removeFromSuperview];
                dispatch_cancel(timer);
            } else {
                second--;
            }
        });
    });
    dispatch_resume(timer);
}

下面是一篇关于luanchImage的用法:
使用launchImage做启动页

相关文章

网友评论

    本文标题:iOS设置动态启动页LaunchScreen

    本文链接:https://www.haomeiwen.com/subject/nmftvqtx.html