ADView.m 3.3 KB

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