前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 启动页后广告Demo

iOS 启动页后广告Demo

作者头像
Mr.RisingSun
发布2018-01-09 17:12:19
1.8K0
发布2018-01-09 17:12:19
举报
文章被收录于专栏:移动端开发移动端开发

重点!  

    对于启动页后的广告,相信大家也都看到过很多很多的,比如我自己常看到的有 QQ音乐,爱奇艺了。你点击了APP,它会启动就会随之启动。。其实这些APP的启动页是没有消失的,你去认真的观察一下!所以它们的顺序就变成了  点击 —> 启动页 —> 广告  下面是我截的QQ音乐的顺序图。不知道怎么弄GIF图。?

为什么说这是重点呢,可能有些小伙伴会误以为使用广告替代了启动页,但启动页怎样做成一个广告呢,还要加一些点击时间之类的,很是不懂!现在就清楚了,至少知道它的一个流程才会有思路的。

     还有一点,这个广告是缓存了的,你试着启动几次,它会给你不同的启动广告的,完事了,你把你的4G和Wifi都关掉,然后再去启动相应的APP,广告依旧是会出现的。。

一个思路:

 这里我说我的一个思路:

     1:把广告先封装到一个View当中去,然后把它加载到一个控制器当中显示,因为涉及到缓存和隐藏导航和标签栏的状况。

     2:在 APPDelegate 的   didFinishLaunchingWithOptions  中进行一个根视图的切换(同志们看了下面代码分析要觉得有问题,欢迎指正)。

这里是广告View的.m文件代码:

代码语言:javascript
复制
#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文件:

代码语言:javascript
复制
#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.
}
*/

 最重要的下面!!

代码语言:javascript
复制
- (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可以联系到我

最后的效果图:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-07-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云代码分析
腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档