ios开发整理

应用添加广告启动页

2017-07-21  本文已影响0人  冲浪小子

通常一个app启动的时候会加载一个广告启动页。如下图:


ADStartPage.gif

不啰嗦直接上代码

xib.png

launch Image view 以后放启动图片,防止广告页加载失败。
Ad Contain View 是占位用的,放广告页用。
Jump Btn 是跳过按钮。
为什么这么设计呢?
1.广告业务逻辑
2.占位视图思想:有个控件不确定尺寸,但是层次结构已经确定,就可以使用占位视图思想
3.屏幕适配.通过屏幕高度判断

其中设置启动图片为防止图片广告图片加载不出来。

```objc```
/***********屏幕适配*************/
#define QBYScreenW [UIScreen mainScreen].bounds.size.width
#define QBYScreenH [UIScreen mainScreen].bounds.size.height
#define iphone6P (QBYScreenH == 736)
#define iphone6 (QBYScreenH == 667)
#define iphone5 (QBYScreenH == 568)
#define iphone4 (QBYScreenH == 480)
/***********屏幕适配*************/

// 设置启动图片
- (void)setupLaunchImage
{
    // 6p:LaunchImage-800-Portrait-736h@3x.png
    // 6:LaunchImage-800-667h@2x.png
    // 5:LaunchImage-568h@2x.png
    // 4s:LaunchImage@2x.png
    if (iphone6P) { // 6p
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    } else if (iphone6) { // 6
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iphone5) { // 5
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-568h"];
        
    } else if (iphone4) { // 4
        
        self.launchImageView.image = [UIImage imageNamed:@"LaunchImage-700"];
    }
    
}

objc

// [responseObject writeToFile:@"/Users/gujunqi/Desktop/ad.plist" atomically:YES];
// 请求数据 -> 解析数据(写成plist文件) -> 设计模型 -> 字典转模型 -> 展示数据
// 获取字典
NSDictionary *adDict = [responseObject[@"ad"] lastObject];

    // 字典转模型
    _item = [QBYADItem mj_objectWithKeyValues:adDict];
    
    // 创建UIImageView展示图片 =>
    CGFloat h = QBYScreenW / _item.w * _item.h;
    
    self.adView.frame = CGRectMake(0, 0, QBYScreenW, h);
    // 加载广告网页
    [self.adView sd_setImageWithURL:[NSURL URLWithString:_item.w_picurl]];
    
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    NSLog(@"%@",error);
    
}];

}

- 其余代码,很好理解

```objc```

- (void)timeChange
{
    // 倒计时
    static int i = 10;
    
    if (i == 0) {
        [self clickJump:nil];
    }
    
    i--;
    
    // 设置跳转按钮文字
    [_jumpBtn setTitle:[NSString stringWithFormat:@"跳转 (%d)",i] forState:UIControlStateNormal];
}

// 点击跳转做的事情
- (IBAction)clickJump:(id)sender {
    // 销毁广告界面,进入主框架界面
    QBYTabBarController *tabBarVc = [[QBYTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController = tabBarVc;
    
    // 干掉定时器
    [_timer invalidate];
}

- (UIImageView *)adView
{
    if (_adView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        [self.adContainView addSubview:imageView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [imageView addGestureRecognizer:tap];
        
        imageView.userInteractionEnabled = YES;
        
        
        _adView = imageView;
    }
    
    return _adView;
}

// 点击广告界面调用
- (void)tap
{
    // 跳转到界面 => safari
    NSURL *url = [NSURL URLWithString:_item.ori_curl];
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:url]) {
        [app openURL:url];
    }
}

代码和资料具体下载地址:
https://github.com/qibingying/iosDevelopInsight/tree/master/%E5%B9%BF%E5%91%8A%E5%90%AF%E5%8A%A8%E9%A1%B5

上一篇 下一篇

猜你喜欢

热点阅读