iOS啟動(dòng)加載添加廣告頁(yè)面

前言:iOSAPP啟動(dòng)加載時(shí)添加廣告頁(yè)面的功能很久之前就存在了,因?yàn)楝F(xiàn)在手上的項(xiàng)目和之前一個(gè)項(xiàng)目都沒有這一需求,也就沒去做。最近發(fā)現(xiàn)手機(jī)上面的APP幾乎八成都用到了啟動(dòng)加載時(shí)跳出廣告頁(yè)面,今天上午沒什么事就自己寫了一個(gè)demo出來(lái),代碼很簡(jiǎn)單,在這和大家一起分享參考。


Github:看這里


思路整理:

1、向后臺(tái)請(qǐng)求數(shù)據(jù)并進(jìn)行UI展示(忽略)

2、根據(jù)后臺(tái)返回?cái)?shù)據(jù)判斷加載頁(yè)面

3、進(jìn)入廣告頁(yè)面

4、返回到主頁(yè)面

代碼實(shí)現(xiàn):

1、在程序加載完成didFinishLaunch中向后臺(tái)請(qǐng)求數(shù)據(jù),如果有廣告數(shù)據(jù)就呈現(xiàn)廣告數(shù)據(jù),如果沒有廣告數(shù)據(jù)直接進(jìn)入主頁(yè)面;這一步思路和做法比較簡(jiǎn)單,我們用imageView直接加載圖片進(jìn)行代替,實(shí)際操作時(shí)按上面思路做即可。

2、在后臺(tái)返回廣告數(shù)據(jù)之后,設(shè)置rootViewController為廣告頁(yè)面控制器,這里我們?cè)俸雎跃W(wǎng)絡(luò)請(qǐng)求步驟的情況下直接賦值:

LanuchViewController *vc = [[LanuchViewController alloc]init];

vc.time = 3;

vc.imgUrl = nil;

vc.url = @"http://www.baidu.com";

self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:vc];

3、在廣告頁(yè)面添加背景視圖和跳過(guò)按鈕,并添加相應(yīng)的監(jiān)聽事件:

/**廣告視圖

*/

@property (nonatomic, strong) UIImageView *backImageView;

/**定時(shí)器

*/

@property (nonatomic, strong) NSTimer *timer;

/**倒計(jì)時(shí)

*/

@property (nonatomic, assign) NSInteger time;

/**跳過(guò)按鈕

*/

@property (nonatomic, strong) UIButton *passBtn;

實(shí)例化控件,并添加點(diǎn)擊事件:

_backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];

_backImageView.userInteractionEnabled = YES;

_backImageView.backgroundColor = [UIColor grayColor];

[self.view addSubview:_backImageView];

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpToWebView)];

[_backImageView addGestureRecognizer:tap];

_passBtn = [MyControl buttonWithFram:CGRectMake(KScreenWidth-80, 30, 60, 30) title:[NSString stringWithFormat:@"跳過(guò)%lds",_time] imageName:nil];

_passBtn.backgroundColor = [UIColor whiteColor];

[_passBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[_passBtn addTarget:self action:@selector(jumpToRootViewCtrl) forControlEvents:UIControlEventTouchUpInside];

_passBtn.layer.cornerRadius = 5;

_passBtn.layer.masksToBounds = YES;

[self.view addSubview:_passBtn];

添加定時(shí)器

_timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeCutdown) userInfo:nil repeats:YES];

NSRunLoop *loop = [NSRunLoop currentRunLoop];

[loop addTimer:_timer forMode:NSRunLoopCommonModes];


設(shè)置倒計(jì)時(shí),計(jì)時(shí)結(jié)束是進(jìn)入主頁(yè)面

- (void)timeCutdown{

if (self.time > 0) {

[_passBtn setTitle:[NSString stringWithFormat:@"跳過(guò)%lds",self.time] forState:UIControlStateNormal];

self.time--;

}else{

[_timer invalidate];

[UIView animateWithDuration:1.5 animations:^{

self.view.alpha = 0;

} completion:^(BOOL finished) {

[UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[MainViewController alloc]init]];

}];

}

}


跳轉(zhuǎn)到網(wǎng)頁(yè)時(shí),先將根試圖控制器改變?yōu)橹黜?yè)面,之后push到網(wǎng)頁(yè),不加動(dòng)畫效果,制造視覺假象。

- (void)jumpToWebView{

[_timer invalidate];

LanuchWebViewController *vc = [[LanuchWebViewController alloc]init];

vc.url = self.url;

MainViewController *main = [[MainViewController alloc]init];

[UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc]initWithRootViewController:main];

[main.navigationController pushViewController:vc animated:NO];

}

點(diǎn)擊跳過(guò)按鈕,直接進(jìn)入到主頁(yè)面。點(diǎn)擊跳轉(zhuǎn)或倒計(jì)時(shí)結(jié)束時(shí),給當(dāng)前視圖添加漸變透明的動(dòng)畫效果。

- (void)jumpToRootViewCtrl{

[_timer invalidate];

[UIView animateWithDuration:0.5 animations:^{

self.view.alpha = 0;

} completion:^(BOOL finished) {

[UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[MainViewController alloc]init]];

}];

}

給廣告網(wǎng)頁(yè)頁(yè)面添加webView和進(jìn)度條:

@interface LanuchWebViewController ()@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, strong) UIProgressView *progressView;

@end

WKWebView有一個(gè)estimatedProgress屬性,可以獲取到當(dāng)前頁(yè)面加載進(jìn)度,我們通過(guò)KVO方式來(lái)動(dòng)態(tài)獲取當(dāng)前加載進(jìn)度

[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context{

if ([keyPath isEqualToString:@"estimatedProgress"]) {

if ([change[@"new"] floatValue] < [change[@"old"] floatValue]) {

return;

}

[_progressView setProgress:[change[@"new"] floatValue] animated:YES];

if (_progressView.progress == 1.0) {

_progressView.hidden = YES;

[_progressView removeFromSuperview];

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容