1> 添加一個(gè)控制器;讓窗口剛開始展示出來的是一個(gè)控制器.
- 廣告界面是固定的;所以可以用Xib描述.(為Xib添加imageView,添加按鈕)
注意:imageview默認(rèn)是不可以和用戶交互的
imageV.userInteractionEnabled = YES;
- 控制器的View在添加廣告圖片和跳過按鈕的時(shí)候不要使用
addSubView添加. - 使用
insertSubview:belowSubview方法添加;它的作用:是讓跳過按鈕保持在廣告圖片的上面,不會(huì)讓廣告圖片蓋住.
[self.view insertSubview:imageV belowSubview:self.adButton];
- 為imageview添加點(diǎn)按鈕手勢(shì).
```objc
// 添加點(diǎn)按手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump)];
[_adView addGestureRecognizer:tap];
// 一旦用戶點(diǎn)擊了廣告圖片我們就跳轉(zhuǎn)到廣告界面
- (void)jump
{
// 廣告的URL
NSURL * url = [NSURL URLWithString:self.adItem.ori_curl];
// 跳轉(zhuǎn)到廣告的地址
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication]openURL:url];
}
2> 在控制器的ViewDidLoad方法中加載數(shù)據(jù)
- 2.1 向服務(wù)器請(qǐng)求廣告數(shù)據(jù)(這里有個(gè)主意點(diǎn):有時(shí)候服務(wù)器返會(huì)給我們的圖片可能比較大,這時(shí)我們的確定廣告圖片) (
BSScreenW是屏幕的尺寸的宏)
// 確定廣告界面尺寸,廣告界面圖片,點(diǎn)擊廣告界面跳轉(zhuǎn)到的界面
CGFloat w = BSScreenW;
CGFloat h = BSScreenH;
if (_adItem.h) {
h = BSScreenW / _adItem.w * _adItem.h;
if (h > BSScreenH) {
h = BSScreenH;
}
}
self.adView.frame = CGRectMake(0, 0, w, h);
// 加載圖片
[self.adView sd_setImageWithURL:[NSURL URLWithString:_adItem.w_picurl]];
-
- 2.2為廣告界面添加個(gè)3秒的定時(shí)器
// 添加定時(shí)器
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
- 2.3 實(shí)現(xiàn)定時(shí)器方法
```objc
- (void)timeChange
{
static NSInteger time = 3;
if (time == -1) {
[self jumpBtn:nil];
return;
}
NSString * timer = [NSString stringWithFormat:@"跳過(%ld)", time];
[_jumpBtn setTitle:timer forState:UIControlStateNormal];
time--;
}
3>用戶點(diǎn)擊跳過按鈕的需要處理的業(yè)務(wù)邏輯
- (IBAction)jumpBtn:(UIButton *)sender {
// 銷毀定時(shí)器
[self.timer invalidate];
// 創(chuàng)建根控制器
HYTabBarController * tabBatCon = [[HYTabBarController alloc] init];
// 跳轉(zhuǎn)到根控制器
[UIApplication sharedApplication].keyWindow.rootViewController = tabBatCon;
}
4>嚴(yán)謹(jǐn)一點(diǎn)當(dāng)我們的廣告界面銷毀時(shí)我們要取消網(wǎng)絡(luò)請(qǐng)求管理者
-(void)dealloc
{
// 取消請(qǐng)求
[self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
// 銷毀管理者
[self.manager invalidateSessionCancelingTasks:NO];
}
最終的效果:

Snip20151224_167.png