最近公司需要实现这个功能,给客户展示炫酷的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切换。
初次加载较慢,第二次以后打开速度还行。还有待优化。