#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);
}
}