重點!
對於啟動頁后的廣告,相信大家也都看到過很多很多的,比如我自己常看到的有 QQ音樂,愛奇藝了。你點擊了APP,它會啟動就會隨之啟動。。其實這些APP的啟動頁是沒有消失的,你去認真的觀察一下!所以它們的順序就變成了 點擊 —> 啟動頁 —> 廣告 下面是我截的QQ音樂的順序圖。不知道怎么弄GIF圖。😊

為什么說這是重點呢,可能有些小伙伴會誤以為使用廣告替代了啟動頁,但啟動頁怎樣做成一個廣告呢,還要加一些點擊時間之類的,很是不懂!現在就清楚了,至少知道它的一個流程才會有思路的。
還有一點,這個廣告是緩存了的,你試着啟動幾次,它會給你不同的啟動廣告的,完事了,你把你的4G和Wifi都關掉,然后再去啟動相應的APP,廣告依舊是會出現的。。
一個思路:
這里我說我的一個思路:
1:把廣告先封裝到一個View當中去,然后把它加載到一個控制器當中顯示,因為涉及到緩存和隱藏導航和標簽欄的狀況。
2:在 APPDelegate 的 didFinishLaunchingWithOptions 中進行一個根視圖的切換(同志們看了下面代碼分析要覺得有問題,歡迎指正)。
這里是廣告View的.m文件代碼:
#import "AdvertisingView.h"
#import "UIImageView+WebCache.h"
@interface AdvertisingView()
@property(nonatomic,strong) UIImageView * advertisingView;
@property(nonatomic,strong) UIButton * advertisingJumpButton;
@property(nonatomic,strong) UILabel * timeLabel;
@property(nonatomic,assign) int secondsCountDown;
@property(nonatomic,strong) NSTimer * countDownTimer;
/** 定時器(這里不用帶*,因為dispatch_source_t就是個類,內部已經包含了*) */
@property (nonatomic, strong) dispatch_source_t timer;
@end
@implementation AdvertisingView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
-(instancetype) initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.advertisingView];
[self addSubview:self.advertisingJumpButton];
[self addSubview:self.timeLabel];
}
return self;
}
-(void)startplayAdvertisingView:(void (^)(AdvertisingView *))advertisingview
{
[[UIApplication sharedApplication].keyWindow addSubview:self];
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
// 這里的block advertisingview ;
// advertisingview(); 你要調用就要傳參數過去,調用的具體代碼在 APPdelegate 里面調用的時候添加這個 block具體的代碼、、、
__block int count = 3;
// 獲得隊列
dispatch_queue_t queue = dispatch_get_main_queue();
// 創建一個定時器(dispatch_source_t本質還是個OC對象)
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
// 設置定時器的各種屬性(幾時開始任務,每隔多長時間執行一次)
// GCD的時間參數,一般是納秒(1秒 == 10的9次方納秒)
// 何時開始執行第一個任務
// dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC) 比當前時間晚3秒
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
uint64_t interval = (uint64_t)(1.0 * NSEC_PER_SEC);
dispatch_source_set_timer(self.timer, start, interval, 0);
// 設置回調
dispatch_source_set_event_handler(self.timer, ^{
/**
* 回主線程更新UI
*/
count--;
dispatch_async(dispatch_get_main_queue(), ^{
if (count == 0) {
// 取消定時器
dispatch_cancel(self.timer);
self.timer = nil;
[self removeFromSuperview];
/**
* 廣告顯示完,就調用 block 更換 rootcontroller
*/
advertisingview(self);
}
else{
_timeLabel.text = [NSString stringWithFormat:@"%d",count];}
});
});
// 啟動定時器
dispatch_resume(self.timer);
}
-(UIImageView * )advertisingView
{
if (_advertisingView == nil) {
_advertisingView =[[UIImageView alloc]initWithFrame:self.bounds];
[_advertisingView sd_setImageWithURL:[NSURL URLWithString:@"http://g.hiphotos.baidu.com/zhidao/pic/item/f2deb48f8c5494ee665ec00f29f5e0fe99257eac.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
}
return _advertisingView;
}
-(UILabel * )timeLabel
{
if (_timeLabel == nil) {
_timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.bounds.size.width - 50, 50, 50, 40)];
_timeLabel.backgroundColor =[UIColor blackColor];
_timeLabel.alpha = 0.5;
_timeLabel.textColor = [UIColor whiteColor];
}
return _timeLabel;
}
-(UIButton * )advertisingJumpButton
{
if (_advertisingJumpButton == nil) {
_advertisingJumpButton = [UIButton buttonWithType:UIButtonTypeCustom];
_advertisingJumpButton.frame = CGRectMake(0, self.bounds.size.height - 80, self.bounds.size.width, 60);
[_advertisingJumpButton setTitle:@"了解詳情" forState:UIControlStateNormal];
[_advertisingJumpButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_advertisingJumpButton addTarget:self action:@selector(buttonclick) forControlEvents:UIControlEventTouchUpInside];
_advertisingJumpButton.titleLabel.font = [UIFont systemFontOfSize:18];
_advertisingJumpButton.backgroundColor = [UIColor blackColor];
_advertisingJumpButton.alpha = 0.5;
_advertisingJumpButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
_advertisingJumpButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
}
return _advertisingJumpButton;
}
-(void)buttonclick
{
}
@end
下面是添加這個View的控制器的.m文件:
#import "AdvertisingViewController.h"
@interface AdvertisingViewController ()
@end
@implementation AdvertisingViewController
/**
* 可以在這個控制器里面做緩存?愛奇藝,QQ音樂的廣告是緩存了的!
*/
-(void)viewWillAppear:(BOOL)animated
{
[self setNeedsStatusBarAppearanceUpdate];
}
- (BOOL)prefersStatusBarHidden {
return YES;
}
-(void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.adverView];
}
-(AdvertisingView *)adverView
{
if (_adverView == nil) {
_adverView = [[ AdvertisingView alloc]initWithFrame:self.view.bounds];
}
return _adverView;
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
最重要的下面!!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
/**
* 進來先讓啟動頁沉睡 2 秒鍾
*/
[NSThread sleepForTimeInterval:2.0];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
AdvertisingViewController * adviewcontroller = [[AdvertisingViewController alloc]init];
ZXTabBarController * tabbarcontroller = [[ZXTabBarController alloc]init];
// 這里要添加根控制器,不添加等下面添加是不行的。
self.window.rootViewController = adviewcontroller ;
[self.window makeKeyAndVisible];
[adviewcontroller.adverView startplayAdvertisingView:^(AdvertisingView * adverview) {
// 更換根控制器
self.window.rootViewController = tabbarcontroller;
}];
// Override point for customization after application launch.
return YES;
}
總結一下:
這樣做,效果是實現了,但我心里一直的疑問就是在上面的更換根控制器這里,這樣寫效果是沒問題的,下面我也會把效果圖給大家看一下,本來這里我感覺就是這整個效果的一個核心的地方,大家要覺得有問題。。歡迎來撩!!要是你喜歡的話。。哈哈哈 這里有我的QQ可以聯系到我
最后的效果圖:

