现在大部分APP都会有一个倒计时启动页,用来展示广告,分享一种实现方式,原理其实很简单,就是在程序启动时从后台请求图片和广告数据,存入本地,然后在第二次打开APP的时候去展示,点击启动图可以进入一个webView或者其他自定义的页面展示数据,右上角(或者其他任意位置)放一个带倒计时的跳过按钮,可以直接跳过
新建一个类,逻辑放在在load方法中,所以这个类不用被调用,可以做到无注入
类里边包含的属性
@property (nonatomic, strong) UIWindow* window;//用来展示启动图的窗口
//倒计时相关
@property (nonatomic, assign) NSInteger downCount;
@property (nonatomic, weak) UIButton* downCountButton;
@property (nonatomic, strong) NSTimer *countDownTimer;
//在load 方法中,启动监听,可以做到无注入
+ (void)load
{
[self shareInstance];
}
+ (instancetype)shareInstance
{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
在init方法中注册APP启动的通知
- (instancetype)init
{
self = [super init];
if (self) {
//尽量不要在初始化的代码里面做别的事,防止对主线程的卡顿和其他情况
//应用启动, 首次开屏广告
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
///要等DidFinished方法结束后才能初始化UIWindow,不然会检测是否有rootViewController
[self checkAD];//检测本地是否有启动图数据,如果有就展示
[self request];//请求新的启动图数据
}];
//如果需要实现每次APP进入后台后都展示启动图,就要实现下边的逻辑
/*
//进入后台
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[self request];
}];
//后台启动,二次开屏广告
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[self checkAD];
}];
*/
}
return self;
}
检测启动图数据是否存在以及展示启动图的逻辑:
- (void)checkAD
{
//检测本地是否有启动图相关数据
if ([ZCQGStartViewSourceManager isHaveSource]) {
//从本地取出数据来展示
NSDictionary *userInfo = [NSDictionary dictionaryWithContentsOfFile:PLIST_USERINFO];
NSString *token = [userInfo objectForKey:@"token"];
if (token) {
[self show];
}
}
}
//展示启动图
- (void)show
{
//初始化一个Window, 做到对业务视图无干扰。
UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [UIViewController new];
window.rootViewController.view.backgroundColor = [UIColor clearColor];
window.rootViewController.view.userInteractionEnabled = NO;
//广告布局
[self setupSubviews:window];
//设置为最顶层,防止 AlertView 等弹窗的覆盖
window.windowLevel = UIWindowLevelStatusBar + 1;
//windwo的isHidden默认为YES,当你设置为NO时,这个Window就会显示了
window.hidden = NO;
window.alpha = 1;
//防止释放,显示完后 要手动设置为 nil
self.window = window;
}
//初始化显示的视图
- (void)setupSubviews:(UIWindow*)window
{
//用来展示图片内容
UIImageView *imageView = [ZCQGStartViewSourceManager getTheCustomStartView];
//增加点击事件 点击图片的时候可以实现自己想要的逻辑
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(letGo)];
[imageView addGestureRecognizer:tap];
[window addSubview:imageView];
///增加一个倒计时跳过按钮
self.downCount = 3;
UIButton * goout = [[UIButton alloc] initWithFrame:CGRectMake(0,0,1,1)];
[goout addTarget:self action:@selector(goOut) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:goout];
[goout mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(window).offset(30 + STATUS_BAR_ADDHEIGHT);
make.right.equalTo(window).offset(-15);
make.height.equalTo(@40);
}];
self.downCountButton = goout;
[self.downCountButton setTitle:[NSString stringWithFormat:@" 点击跳过 %ld ",(long)self.downCount] forState:UIControlStateNormal];
NSTimeInterval timeInterval = 1.0;
self.countDownTimer = [NSTimer timerWithTimeInterval:timeInterval target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.countDownTimer forMode:NSRunLoopCommonModes];
}
点击启动图的时候
- (void)letGo
{
//取出跳转页面之前的controller 注意: 不直接取KeyWindow 是因为当有AlertView 或者有键盘弹出时, 取到的KeyWindow是错误的。
UIViewController* rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
}
点击跳过按钮的时候,隐藏启动图
- (void)goOut
{
[UIView animateWithDuration:0.2 animations:^{
self.window.alpha = 0;
} completion:^(BOOL finished) {
[self.window.subviews.copy enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj removeFromSuperview];
}];
self.window.hidden = YES;
self.window = nil;//手动释放
}];
}
ZCQGStartViewSourceManager
是用来管理启动图数据的类,提供了几个方法
@interface ZCQGStartViewSourceManager : NSObject
+ (void)requestNewDataSource;//请求最新的启动图数据
+ (BOOL)isHaveSource;//检查本地是否有相关数据
+ (UIImageView *)getTheCustomStartView;//根据本地数据创造一个UIimageView
+ (NSDictionary *)getStartViewInfosDic;//取出本地的启动图数据
@end
网友评论