iOS 自定義APP啟動頁

現(xiàn)在大部分APP都會有一個倒計時啟動頁,用來展示廣告,分享一種實現(xiàn)方式,原理其實很簡單,就是在程序啟動時從后臺請求圖片和廣告數(shù)據(jù),存入本地,然后在第二次打開APP的時候去展示,點擊啟動圖可以進(jìn)入一個webView或者其他自定義的頁面展示數(shù)據(jù),右上角(或者其他任意位置)放一個帶倒計時的跳過按鈕,可以直接跳過

新建一個類,邏輯放在在load方法中,所以這個類不用被調(diào)用,可以做到無注入

類里邊包含的屬性

@property (nonatomic, strong) UIWindow* window;//用來展示啟動圖的窗口
//倒計時相關(guān)
@property (nonatomic, assign) NSInteger downCount;
@property (nonatomic, weak) UIButton* downCountButton;
@property (nonatomic, strong) NSTimer *countDownTimer;
//在load 方法中,啟動監(jiān)聽,可以做到無注入
+ (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) {
        
        //盡量不要在初始化的代碼里面做別的事,防止對主線程的卡頓和其他情況
        //應(yīng)用啟動, 首次開屏廣告
        [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
            ///要等DidFinished方法結(jié)束后才能初始化UIWindow,不然會檢測是否有rootViewController
            [self checkAD];//檢測本地是否有啟動圖數(shù)據(jù),如果有就展示
            [self request];//請求新的啟動圖數(shù)據(jù)

        }];
        
        //如果需要實現(xiàn)每次APP進(jìn)入后臺后都展示啟動圖,就要實現(xiàn)下邊的邏輯
        /*
        //進(jìn)入后臺
        [[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;
}

檢測啟動圖數(shù)據(jù)是否存在以及展示啟動圖的邏輯:

- (void)checkAD
{
    //檢測本地是否有啟動圖相關(guān)數(shù)據(jù)
    if ([ZCQGStartViewSourceManager isHaveSource]) {
        //從本地取出數(shù)據(jù)來展示
        NSDictionary *userInfo = [NSDictionary dictionaryWithContentsOfFile:PLIST_USERINFO];
        NSString *token = [userInfo objectForKey:@"token"];
        if (token) {
           [self show];
        }
    }
    
}

//展示啟動圖
- (void)show
{
    //初始化一個Window, 做到對業(yè)務(wù)視圖無干擾。
    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];
    
    //設(shè)置為最頂層,防止 AlertView 等彈窗的覆蓋
    window.windowLevel = UIWindowLevelStatusBar + 1;
    
    //windwo的isHidden默認(rèn)為YES,當(dāng)你設(shè)置為NO時,這個Window就會顯示了
    window.hidden = NO;
    window.alpha = 1;
    //防止釋放,顯示完后  要手動設(shè)置為 nil
    self.window = window;
}

//初始化顯示的視圖
- (void)setupSubviews:(UIWindow*)window
{
    //用來展示圖片內(nèi)容
    UIImageView *imageView = [ZCQGStartViewSourceManager getTheCustomStartView];
    
    //增加點擊事件 點擊圖片的時候可以實現(xiàn)自己想要的邏輯
    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
{
    //取出跳轉(zhuǎn)頁面之前的controller 注意: 不直接取KeyWindow 是因為當(dāng)有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 是用來管理啟動圖數(shù)據(jù)的類,提供了幾個方法

@interface ZCQGStartViewSourceManager : NSObject

+ (void)requestNewDataSource;//請求最新的啟動圖數(shù)據(jù)

+ (BOOL)isHaveSource;//檢查本地是否有相關(guān)數(shù)據(jù)

+ (UIImageView *)getTheCustomStartView;//根據(jù)本地數(shù)據(jù)創(chuàng)造一個UIimageView

+ (NSDictionary *)getStartViewInfosDic;//取出本地的啟動圖數(shù)據(jù)

@end
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容