ADView.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. */
  3. #import "ADView.h"
  4. /**并不是要保留ADView内存。
  5. 而是。在系统进入后台后,要添加ADView。
  6. 在becomeActive里面。计时1.5秒移除。
  7. */
  8. ADView *mainAD = nil;
  9. UIButton *timeBtn = nil;
  10. @interface ADView()
  11. @property(nonatomic,strong) NSMutableArray* ivs;
  12. @end
  13. @implementation ADView
  14. //单例类 为了可以在其他页面 直接删除广告
  15. +(id)shareMainAD
  16. {
  17. @synchronized(self)
  18. {
  19. if (mainAD == nil)
  20. {
  21. mainAD = [[ADView alloc] initWithFrame:kFrame];
  22. }
  23. }
  24. return mainAD;
  25. }
  26. +(id)allocWithZone:(struct _NSZone *)zone
  27. {
  28. @synchronized(self)
  29. {
  30. if (mainAD == nil)
  31. {
  32. mainAD = [super allocWithZone:zone];
  33. }
  34. }
  35. return mainAD;
  36. }
  37. +(void)showADView
  38. {
  39. mainAD = [ADView shareMainAD];
  40. [mainAD setUserInteractionEnabled:YES];
  41. [mainAD setImage:[UIImage imageNamed:@"LaunchImage.png"]];
  42. [mainAD setContentMode:UIViewContentModeScaleToFill];
  43. UIWindow* window = [UIApplication sharedApplication].keyWindow;
  44. NSString* path;
  45. if (!RQ_COMMON_MANAGER.adPaths || RQ_COMMON_MANAGER.adPaths.count <1) {
  46. }else{
  47. path = RQ_COMMON_MANAGER.adPaths[arc4random()%RQ_COMMON_MANAGER.adPaths.count];
  48. }
  49. UIImageView* iv = [[UIImageView alloc] initWithFrame:kFrame];
  50. //iv.height -= kSize.height*.25;
  51. [iv setContentMode:UIViewContentModeScaleToFill];
  52. if (path.length == 0) {
  53. //NSLog(@"本地默认广告");
  54. [iv setImage:[UIImage imageNamed:@"firstAD"]];
  55. }else{
  56. [iv sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"firstAD"]];
  57. }
  58. // [iv setUserInteractionEnabled:YES];
  59. // UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:[UIApplication sharedApplication].keyWindow action:@selector(skipADLink)];
  60. // [iv addGestureRecognizer:tapGesture];
  61. [mainAD addSubview:iv];
  62. UIButton *skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  63. skipBtn.frame = CGRectMake(kSize.width - 65, 30, 45, 45);
  64. [skipBtn setTitle:@"5" textColor:[UIColor orangeColor] Font:FontLarger fotState:UIControlStateNormal];
  65. skipBtn.layer.masksToBounds = YES;
  66. skipBtn.layer.cornerRadius = 22.5;
  67. skipBtn.layer.borderWidth = 1.5;
  68. skipBtn.layer.borderColor = [UIColor orangeColor].CGColor;
  69. //[skipBtn addTarget:[UIApplication sharedApplication].delegate action:@selector(clickInADView) forControlEvents:UIControlEventTouchUpInside];
  70. [mainAD addSubview:skipBtn];
  71. timeBtn = skipBtn;
  72. //主线程中调用 显示广告
  73. dispatch_after(1, dispatch_get_main_queue(), ^{
  74. [window addSubview:mainAD];
  75. [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"4" afterDelay:1];
  76. [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"3" afterDelay:2];
  77. [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"2" afterDelay:3];
  78. [mainAD performSelector:@selector(setBtnTitleWithString:) withObject:@"1" afterDelay:4];
  79. [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:5];
  80. });
  81. }
  82. - (void)setBtnTitleWithString:(NSString *)str {
  83. dispatch_async(dispatch_get_main_queue(), ^{
  84. [timeBtn setTitle:str forState:UIControlStateNormal];
  85. });
  86. }
  87. //dansonmark 先不用
  88. -(void)skipADLink
  89. {
  90. // NSInteger index = gesture.view.tag;
  91. //
  92. // NSDictionary *dic = _imgArray[index];
  93. //心塞 这个搞不定呀
  94. // NSLog(@"条");
  95. }
  96. +(void)removeADView
  97. {
  98. if (!mainAD) {
  99. return;
  100. }
  101. [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:3];
  102. }
  103. -(void)clickInADView
  104. {
  105. NSLog(@"此方法永不会进入 代理是APPdelegate");
  106. }
  107. -(void)removeImidiate
  108. {
  109. if (!mainAD) {
  110. return;
  111. }
  112. if (self.superview) {
  113. [UIView animateWithDuration:.5 animations:^{
  114. self.alpha = 0;
  115. } completion:^(BOOL finished) {
  116. [self removeFromSuperview];
  117. }];
  118. }
  119. }
  120. /**下载工作可以在每次will进入后台时。调用?
  121. 但其实这个东西不怎么耗时。真正耗时的是加载图片。
  122. 已将广告下载放到ServiceHelper 所以不用这些操作了
  123. */
  124. @end