/** */ #import "ADView.h" /**并不是要保留ADView内存。 而是。在系统进入后台后,要添加ADView。 在becomeActive里面。计时1.5秒移除。 */ ADView* mainAD = nil; @interface ADView() @property(nonatomic,strong) NSMutableArray* ivs; @end @implementation ADView //单例类 为了可以在其他页面 直接删除广告 +(id)shareMainAD { @synchronized(self) { if (mainAD == nil) { mainAD = [[ADView alloc] initWithFrame:kFrame]; } } return mainAD; } +(id)allocWithZone:(struct _NSZone *)zone { @synchronized(self) { if (mainAD == nil) { mainAD = [super allocWithZone:zone]; } } return mainAD; } +(void)showADView { mainAD = [ADView shareMainAD]; [mainAD setUserInteractionEnabled:YES]; [mainAD setImage:[UIImage imageNamed:@"LaunchImage.png"]]; [mainAD setContentMode:UIViewContentModeScaleToFill]; UIWindow* window = [UIApplication sharedApplication].keyWindow; NSString* path; if (!defUser.adPaths || defUser.adPaths.count <1) { }else{ path = defUser.adPaths[arc4random()%defUser.adPaths.count]; } UIImageView* iv = [[UIImageView alloc] initWithFrame:kFrame]; //iv.height -= kSize.height*.25; [iv setContentMode:UIViewContentModeScaleToFill]; if (!path || path.length == 0) { //NSLog(@"本地默认广告"); [iv setImage:[UIImage imageNamed:@"firstAD"]]; }else{ [iv sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"firstAD"]]; } // [iv setUserInteractionEnabled:YES]; // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:[UIApplication sharedApplication].keyWindow action:@selector(skipADLink)]; // [iv addGestureRecognizer:tapGesture]; [mainAD 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:@"跳过" forState:UIControlStateNormal]; [skipBtn setTitleColor:newcolor forState: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:[UIApplication sharedApplication].delegate action:@selector(clickInADView) forControlEvents:UIControlEventTouchUpInside]; [mainAD addSubview:skipBtn]; //主线程中调用 显示广告 dispatch_after(1, dispatch_get_main_queue(), ^{ [window addSubview:mainAD]; [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:3]; }); } //dansonmark 先不用 -(void)skipADLink { // NSInteger index = gesture.view.tag; // // NSDictionary *dic = _imgArray[index]; //心塞 这个搞不定呀 // NSLog(@"条"); } +(void)removeADView { if (!mainAD) { return; } [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:3]; } -(void)clickInADView { NSLog(@"此方法永不会进入 代理是APPdelegate"); } -(void)removeImidiate { if (!mainAD) { return; } if (self.superview) { [UIView animateWithDuration:.5 animations:^{ self.alpha = 0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } } /**下载工作可以在每次will进入后台时。调用? 但其实这个东西不怎么耗时。真正耗时的是加载图片。 已将广告下载放到ServiceHelper 所以不用这些操作了 */ @end