1> 添加一个控制器,让窗口刚开始展示出来的是一个控制器
1.1
广告界面是固定的,所以可以用Xib描述(为Xib添加imageView,添加按钮)
注:imageview默认是不可以和用户交互的,所以要设置其userInteractionEnabled属性
self.advertisementView.userInteractionEnabled=YES;
1.2 控制器的View在添加广告图片和跳过按钮的时候不要使用addSubView添加
使用insertSubview:belowSubview:方法添加,它的作用是:让跳过按钮保持在广告图片的上面,而不会被广告图片盖住
[self.view insertSubview: _advertisementView belowSubview:_overLook];
1.3 为imageview添加点按钮手势
// 添加点按手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jump)];
[_adView addGestureRecognizer:tap];
// 一旦用户点击了广告图片我们就跳转到广告界面
- (void)jump
{
// 广告的URL
NSURL * url = [NSURL URLWithString:self.adItem.ori_curl];
// 跳转到广告的地址
if ([[UIApplication sharedApplication]canOpenURL:url])
{
[[UIApplication sharedApplication]openURL:url];
}
2> 在控制器的ViewDidLoad方法中加载数据
2.1 向服务器请求广告数据(这里有个主意点:有时候服务器返会给我们的图片可能比较大,这时我们得确定广告图片的大小) (
adScreen是屏幕大小的宏)
// 确定广告界面尺寸,广告界面图片,点击广告界面跳转到的界面
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为广告界面添加个3秒的定时器
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
2.3 实现定时器方法
-
- (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>用户点击跳过按钮的需要处理的业务逻辑
- (IBAction)jumpBtn:(UIButton *)sender { // 销毁定时器 [self.timer invalidate]; // 创建根控制器 HYTabBarController * tabBatCon = [[HYTabBarController alloc] init]; // 跳转到根控制器 [UIApplication sharedApplication].keyWindow.rootViewController = tabBatCon; }
4>严谨一点当我们的广告界面销毁时我们要取消网络请求管理者
-(void)dealloc { // 取消请求 [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)]; // 销毁管理者 [self.manager invalidateSessionCancelingTasks:NO]; }