启动页首屏显示广告 方法实现的不是太好

#import <UIKit/UIKit.h>

#import "MainADModel.h"

typedef void (^MBStartPageAdsTap)(MainADModel *adModel);


@interface MBStartPageAds : UIView


- (instancetype)initWithframe:(CGRect)frame TapBlock:(MBStartPageAdsTap)tapBlock;

@end

#import "MBStartPageAds.h"

#import "MBAppViewModel.h"

#import "UIButton+Countdown.h"

#import "UIButton+WebCache.h"

#import <UIImageView+WebCache.h>


@implementation MBStartPageAds

{

    MBStartPageAdsTap _tapBlock;

    CGRect _frame;

    MBAppViewModel *_mbappViewModel;

    UIImageView*_splashView;

    NSInteger p;

    NSData* _data;

    MainADModel* _adModel;

}


- (instancetype)initWithframe:(CGRect)frame TapBlock:(MBStartPageAdsTap)tapBlock

{

    if (self = [super init]) {

        _tapBlock = tapBlock;

        _frame=frame;

        

        [self initUI];

    }

    return self;

}

- (void)initUI{

    self.frame=_frame;

    self.userInteractionEnabled=YES;

    self.backgroundColor=[UIColor whiteColor];

    _mbappViewModel =[[MBAppViewModel alloc]init];

    _splashView=[[UIImageView alloc]initWithFrame:_frame];

    _splashView.backgroundColor=[UIColor whiteColor];

    _splashView.image=[UIImage imageNamed:@"launchImage"];

    _splashView.userInteractionEnabled=YES;

    [self addSubview:_splashView];


    [self getadvertisement];

}

//请求数据 判断时间

-(void)getadvertisement

{

    double delayInSeconds = 1.5;

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        

        if(!_data)

        {

            if (_tapBlock) {

                _tapBlock(nil);

            }

            return ;

        }

        else

        {

            NSLog(@"1.5秒过去了,请求回来了");

        }

        //执行事件

    });

    

    [_mbappViewModel getadVertisementSuccess:^(id responseObject) {

        NSArray* adArr = (NSArray*)responseObject;

        if (adArr.count > 0) {

            _adModel = adArr[0];

            //         下载并显示广告

            [self qidong:_adModel];

        }

        

    } Faild:^(NSInteger faildCode, NSString *faildMessage) {

        if (_tapBlock) {

            _tapBlock(nil);

        }

    }];

}

//

-(void)qidong:(MainADModel *)adModel{

    [[SDImageCache sharedImageCache]clearMemory];

    

    UIImageView*defaultView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    defaultView.alpha=0;

    defaultView.backgroundColor=[UIColor whiteColor];

    defaultView.userInteractionEnabled=YES;

    defaultView.contentMode=UIViewContentModeScaleAspectFill;

    

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

    btn.frame=CGRectMake(0, 85, SCREEN_WIDTH, SCREEN_HEIGHT-85);

    btn.backgroundColor=[UIColor clearColor];

    [btn addTarget:self action:@selector(btndown) forControlEvents:UIControlEventTouchUpInside];

    

    UIButton *tiaoguoBtn=[UIButton buttonWithType:UIButtonTypeCustom];

    tiaoguoBtn.frame=CGRectMake(SCREEN_WIDTH-50, 50, 40, 40);

    [tiaoguoBtn advertisementTimer];

    [tiaoguoBtn addTarget:self action:@selector(tiaoguoBtnDown) forControlEvents:UIControlEventTouchUpInside];

    

    

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

        

        _data = [NSData dataWithContentsOfURL:[NSURL URLWithString:adModel.adimage]];

        

        dispatch_async(dispatch_get_main_queue(), ^{

            

            defaultView.image = [UIImage imageWithData:_data];

            [UIView animateWithDuration:0.5 animations:^{

                defaultView.alpha=1;

            } completion:^(BOOL finished)

             {

                 

             }];

        });

    });

    [self addSubview:tiaoguoBtn];

    [self addSubview:btn];

    [_splashView addSubview:defaultView];

    [self performSelector:@selector(defaultViewRemoveFromWindow:) withObject:_splashView afterDelay:3];

}

//任意地方点击跳转

-(void)btndown

{

    p++;

    if (_tapBlock) {

        _tapBlock(_adModel);

    }

}

//跳过按钮的跳转

-(void)tiaoguoBtnDown

{

    [self defaultViewRemoveFromWindow:_splashView];

}

-(void)defaultViewRemoveFromWindow:(id)sennder

{

    p++;//不要重复走方法

    if(p==2) return;

    

        if (_tapBlock) {

            _tapBlock(nil);

        }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值