ADView.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. */
  3. #import "ADView.h"
  4. #import "UIImageView+WebCache.h"
  5. /**并不是要保留ADView内存。
  6. 而是。在系统进入后台后,要添加ADView。
  7. 在becomeActive里面。计时2秒移除。
  8. */
  9. static ADView* mainAD;
  10. @interface ADView()
  11. @property(nonatomic,strong) NSMutableArray* ivs;
  12. @end
  13. @implementation ADView
  14. +(void)showADView
  15. {
  16. if (!defUser.adPaths || defUser.adPaths.count <1) {
  17. [ADView loadADPaths];
  18. return;
  19. }
  20. mainAD = [[ADView alloc] initWithFrame:kFrame];
  21. UIWindow* window = [UIApplication sharedApplication].keyWindow;
  22. dispatch_after(1, dispatch_get_main_queue(), ^{
  23. [window addSubview:mainAD];
  24. [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:3];
  25. });
  26. }
  27. - (instancetype)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. self.userInteractionEnabled = YES;
  32. [self setImage:[UIImage imageNamed:@"LaunchImage.png"]];
  33. [self setContentMode:UIViewContentModeScaleAspectFill];
  34. NSString *path = defUser.adPaths[arc4random()%defUser.adPaths.count];
  35. UIImageView* iv = [[UIImageView alloc] initWithFrame:kFrame];
  36. //iv.height -= kSize.height*.25;
  37. if (!path) {
  38. path = @"";
  39. }
  40. [iv sd_setImageWithURL:[NSURL URLWithString:path] placeholderImage:[UIImage imageNamed:@"firstAD"]];
  41. [self addSubview:iv];
  42. UIButton *skipBtn = [[UIButton alloc]
  43. initWithFrame:CGRectMake(kSize.width - 55, kStatusHeight+5, 35, 35)];
  44. skipBtn.backgroundColor = [UIColor darkGrayColor];
  45. UIColor *newcolor = [UIColor whiteColor];
  46. [skipBtn setTitle:@"跳过" textColor:newcolor font:Font16 fotState:UIControlStateNormal];
  47. skipBtn.titleLabel.font = [UIFont systemFontOfSize:14];
  48. skipBtn.layer.masksToBounds = YES;
  49. skipBtn.layer.cornerRadius = 35/2;
  50. skipBtn.layer.borderWidth = 1.0;
  51. skipBtn.layer.borderColor = newcolor.CGColor;
  52. [skipBtn addTarget:self action:@selector(removeImidiate) forControlEvents:UIControlEventTouchUpInside];
  53. [self addSubview:skipBtn];
  54. }
  55. return self;
  56. }
  57. +(void)removeADView
  58. {
  59. if (!mainAD) {
  60. return;
  61. }
  62. [mainAD performSelector:@selector(removeImidiate) withObject:nil afterDelay:2];
  63. }
  64. -(void)removeImidiate
  65. {
  66. if (self.superview) {
  67. [UIView animateWithDuration:.5 animations:^{
  68. self.alpha = 0;
  69. } completion:^(BOOL finished) {
  70. [self removeFromSuperview];
  71. mainAD = nil;
  72. }];
  73. }
  74. }
  75. void loadADPaths(){
  76. [ADView loadADPaths];
  77. }
  78. /**下载工作可以在每次will进入后台时。调用?
  79. 但其实这个东西不怎么耗时。真正耗时的是加载图片。
  80. */
  81. +(void)loadADPaths
  82. {
  83. if (![Util connectedToNetWork]) {
  84. return ;
  85. }
  86. NSMutableArray *arr=[NSMutableArray array];
  87. [arr addPro:@"type" Value:@"4"];
  88. NSString* method = @"getAppAdsNew";
  89. [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) {
  90. RemoveHUD();
  91. if (!dict) {
  92. return ;
  93. }
  94. if ( [dict[@"code"] isEqualToString:@"1"]) {
  95. return ;
  96. }
  97. NSArray* body = dict[@"body"];
  98. [arr removeAllObjects];
  99. for (NSDictionary *dic in body) {
  100. if ([dic[@"LOCATION"] isEqualToString:@"11"]) {
  101. [arr addObject:[dic objectForKey:@"IMG"]];
  102. }
  103. }
  104. defUser.adPaths = arr;
  105. }];
  106. }
  107. @end