在启动页后添加本地gif图

本文介绍了一种以FLAnimatedImageView为基础,封装的启动页GIF动图框架。通过监听UIApplicationDidFinishLaunchingNotification,实现在不干扰业务层的情况下初始化开屏广告。利用通知RBGifLaunchFinish进行后续页面切换。虽然初次加载可能较慢,但二次打开有良好表现,仍有优化空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近公司需要实现这个功能,给客户展示炫酷的GIF图以留下第一印象。

故此,以FLAnimatedImageView为基础框架,封装了一个启动页GIF动图框架。

+(void)load{
    [self shareLaunch];
}

+(RBGifLaunch *)shareLaunch{
    static RBGifLaunch *instance = nil;
    static dispatch_once_t oneToken;
    dispatch_once(&oneToken,^{
        instance = [[RBGifLaunch alloc] init];
    });
    return instance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        //在UIApplicationDidFinishLaunching时初始化开屏广告,做到对业务层无干扰,当然你也可以直接在AppDelegate didFinishLaunchingWithOptions方法中初始化
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            //初始化开屏广告
            [self setupRBGifLaunch];
        }];
    }
    return self;
}

-(void)setupRBGifLaunch {
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window.rootViewController = [RBGifLaunchViewController new];
    window.rootViewController.view.backgroundColor = [UIColor clearColor];
    window.rootViewController.view.userInteractionEnabled = NO;
    window.windowLevel = UIWindowLevelStatusBar + 1000;
    window.hidden = NO;
    window.alpha = 1;
    _window = window;
    self.bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    UIImageView *bgImageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    bgImageView.image = [self imageFromLaunchScreen];
    [self.bgView addSubview:bgImageView];
    
    /** 添加launchImageView */
    NSURL *imgUrl = [[NSBundle mainBundle] URLForResource:@"LaunchGif" withExtension:@"gif"];
    FLAnimatedImage *animatedImg = [FLAnimatedImage animatedImageWithGIFData:[NSData dataWithContentsOfURL:imgUrl]];
    FLAnimatedImageView *animatedImgView = [[FLAnimatedImageView alloc] init];
    self.animatedImgView = animatedImgView;
    animatedImgView.animatedImage = animatedImg;
    animatedImgView.frame = [UIScreen mainScreen].bounds;
    __weak typeof (self) weakSelf = self;
    animatedImgView.loopCompletionBlock = ^(NSUInteger loopCountRemaining){
        [weakSelf.animatedImgView removeFromSuperview];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"RBGifLaunchFinish" object:nil];
        [weakSelf removeOnly];
    };
    [self.bgView addSubview:self.animatedImgView];
    [_window addSubview:self.bgView];
}

-(UIImage *)imageFromLaunchScreen{
    NSString *UILaunchStoryboardName = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchStoryboardName"];
    if(UILaunchStoryboardName == nil){
        CLog(@"从 LaunchScreen 中获取启动图失败!");
        return nil;
    }
    UIViewController *LaunchScreenSb = [[UIStoryboard storyboardWithName:UILaunchStoryboardName bundle:nil] instantiateInitialViewController];
    if(LaunchScreenSb){
        UIView * view = LaunchScreenSb.view;
        view.frame = [UIScreen mainScreen].bounds;
        UIImage *image = [self imageFromView:view];
        return image;
    }
    CLog(@"从 LaunchScreen 中获取启动图失败!");
    return nil;
}

-(UIImage*)imageFromView:(UIView*)view{
    CGSize size = view.bounds.size;
    //参数1:表示区域大小 参数2:如果需要显示半透明效果,需要传NO,否则传YES 参数3:屏幕密度
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


-(void)removeOnly {
    if(_window){
        [_window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if(obj){
                [obj removeFromSuperview];
                obj = nil;
            }
        }];
        _window.hidden = YES;
        _window = nil;
    }
}

把LaunchScreen的图片设置为空白或者动图的首帧,这样子更不显得突兀。

后续操作通过监听notification的key"RBGifLaunchFinish",进行rootViewController切换。

初次加载较慢,第二次以后打开速度还行。还有待优化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值