123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /**
- */
- #import "ADView.h"
- /**并不是要保留ADView内存。
- 而是。在系统进入后台后,要添加ADView。
- 在becomeActive里面。计时1.5秒移除。
- */
- ADView *mainAD = nil;
- UIButton *timeBtn = 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 (!RQ_COMMON_MANAGER.adPaths || RQ_COMMON_MANAGER.adPaths.count <1) {
- }else{
- path = RQ_COMMON_MANAGER.adPaths[arc4random()%RQ_COMMON_MANAGER.adPaths.count];
- }
-
- UIImageView* iv = [[UIImageView alloc] initWithFrame:kFrame];
- //iv.height -= kSize.height*.25;
- [iv setContentMode:UIViewContentModeScaleToFill];
-
- if (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 buttonWithType:UIButtonTypeCustom];
- skipBtn.frame = CGRectMake(kSize.width - 65, 30, 45, 45);
- [skipBtn setTitle:@"5" textColor:[UIColor orangeColor] Font:FontLarger fotState:UIControlStateNormal];
- skipBtn.layer.masksToBounds = YES;
- skipBtn.layer.cornerRadius = 22.5;
- skipBtn.layer.borderWidth = 1.5;
- skipBtn.layer.borderColor = [UIColor orangeColor].CGColor;
- //[skipBtn addTarget:[UIApplication sharedApplication].delegate action:@selector(clickInADView) forControlEvents:UIControlEventTouchUpInside];
- [mainAD addSubview:skipBtn];
- timeBtn = skipBtn;
-
- //主线程中调用 显示广告
- dispatch_after(1, dispatch_get_main_queue(), ^{
- [window addSubview:mainAD];
-
- [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"4" afterDelay:1];
- [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"3" afterDelay:2];
- [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"2" afterDelay:3];
- [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"1" afterDelay:4];
- [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:5];
- });
- }
- - (void)setBtnTitleWithString:(NSString *)str {
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [timeBtn setTitle:str forState:UIControlStateNormal];
- });
- }
- //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
|