ADView.m 3.7 KB

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