/** */ #import "ADView.h" #import "UIImageView+WebCache.h" /**并不是要保留ADView内存。 而是。在系统进入后台后,要添加ADView。 在becomeActive里面。计时2秒移除。 */ static ADView* mainAD; @interface ADView() @property(nonatomic,strong) NSMutableArray* ivs; @end @implementation ADView +(void)showADView { if (!defUser.adPaths || defUser.adPaths.count <1) { [ADView loadADPaths]; return; } mainAD = [[ADView alloc] initWithFrame:kFrame]; UIWindow* window = [UIApplication sharedApplication].keyWindow; dispatch_after(1, dispatch_get_main_queue(), ^{ [window addSubview:mainAD]; [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:3]; }); } - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.userInteractionEnabled = YES; [self setImage:[UIImage imageNamed:@"LaunchImage.png"]]; [self setContentMode:UIViewContentModeScaleAspectFill]; NSString *path = defUser.adPaths[arc4random()%defUser.adPaths.count]; UIImageView* iv = [[UIImageView alloc] initWithFrame:kFrame]; //iv.height -= kSize.height*.25; if (!path) { path = @""; } [iv sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"firstAD"]]; [self addSubview:iv]; UIButton *skipBtn = [[UIButton alloc] initWithFrame:CGRectMake(kSize.width - 55, kStatusHeight+5, 35, 35)]; skipBtn.backgroundColor = [UIColor darkGrayColor]; UIColor *newcolor = [UIColor whiteColor]; [skipBtn setTitle:@"跳过" textColor:newcolor font:Font16 fotState:UIControlStateNormal]; skipBtn.titleLabel.font = [UIFont systemFontOfSize:14]; skipBtn.layer.masksToBounds = YES; skipBtn.layer.cornerRadius = 35/2; skipBtn.layer.borderWidth = 1.0; skipBtn.layer.borderColor = newcolor.CGColor; [skipBtn addTarget:self action:@selector(removeImidiate) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:skipBtn]; } return self; } +(void)removeADView { if (!mainAD) { return; } [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:2]; } -(void)removeImidiate { if (self.superview) { [UIView animateWithDuration:.5 animations:^{ self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; mainAD = nil; }]; } } void loadADPaths(){ [ADView loadADPaths]; } /**下载工作可以在每次will进入后台时。调用? 但其实这个东西不怎么耗时。真正耗时的是加载图片。 */ +(void)loadADPaths { if (![Util connectedToNetWork]) { return ; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"type" Value:@"4"]; NSString* method = @"getAppAdsNew"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) { RemoveHUD(); if (!dict) { return ; } if ( [dict[@"code"] isEqualToString:@"1"]) { return ; } NSArray* body = dict[@"body"]; [arr removeAllObjects]; for (NSDictionary *dic in body) { if ([dic[@"LOCATION"] isEqualToString:@"11"]) { [arr addObject:[dic objectForKey:@"IMG"]]; } } defUser.adPaths = arr; }]; } @end