-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdRFMediationBannerViewController.m
207 lines (159 loc) · 6.75 KB
/
AdRFMediationBannerViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// AdRFMediationBannerViewController.m
// ADRFMediationSDKDemo-iOS
//
// Created by 陈坤 on 2020/4/21.
// Copyright © 2020 陈坤. All rights reserved.
//
#import "AdRFMediationBannerViewController.h"
#import <ADRFMediationSDK/ADRFMediationSDKBannerAdView.h>
#import <ADRFMediationKit/ADRFMediationKitMacros.h>
#import "SetConfigManager.h"
@interface AdRFMediationBannerItem : NSObject
@property (nonatomic, assign) CGFloat rate;
@property (nonatomic, copy) NSString *posId;
@property (nonatomic, copy) NSString *title;
+ (instancetype)itemWithRate:(CGFloat)rate posId:(NSString *)posId title:(NSString *)title;
@end
@interface AdRFMediationBannerViewController ()<ADRFMediationSDKBannerAdViewDelegate>
@property (nonatomic, strong) ADRFMediationSDKBannerAdView * bannerView;
@property (nonatomic, copy) NSArray<AdRFMediationBannerItem *> *array;
@property (nonatomic, strong) NSMutableArray<UIButton *> *btns;
@end
@implementation AdRFMediationBannerViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
for (int i = 0; i < self.array.count; i++) {
[self createButtonWithItem:self.array[i] index:i];
}
}
#pragma mark - Private method
- (void)loadBannerWithRate:(CGFloat)rate posId:(NSString *)posId {
[self.bannerView removeFromSuperview];
self.bannerView.delegate = nil;
self.bannerView = nil;
// 1、初始化banner视图,并给定frame值,rate取值根据banner的尺寸
self.bannerView = [[ADRFMediationSDKBannerAdView alloc] initWithFrame:CGRectMake(0, 250, kADRFScreenWidth, kADRFScreenWidth / rate)];
self.bannerView.delegate = self;
// 2、设置控制器,用来弹出落地页,重要
self.bannerView.controller = self;
// 3、设置广告位id,重要
self.bannerView.posId = posId;
self.bannerView.refershTime = [SetConfigManager sharedManager].bannerAdInterval;
if (![[SetConfigManager sharedManager].bannerAdScenceId isEqualToString:@""])
self.bannerView.scenesId = [SetConfigManager sharedManager].bannerAdScenceId;
// 可设置bannerview的背景色,请按需添加修改该行代码
// self.bannerView.backgroundColor = UIColor.redColor;
// 4、可先展示再请求
// 5、加载并展示
[self.bannerView loadAndShowWithError:nil];
}
#pragma mark - ADRFMediationSDKBannerAdViewDelegate
/**
广告获取成功
@param bannerView banner实例
*/
- (void)adrf_bannerViewDidReceived:(ADRFMediationSDKBannerAdView *)bannerView{
[self.view addSubview:self.bannerView];
}
/**
广告拉取失败
@param bannerView banner实例
@param errorModel 错误描述
*/
- (void)adrf_bannerViewFailToReceived:(ADRFMediationSDKBannerAdView *)bannerView errorModel:(ADRFMediationAdapterErrorDefine *)errorModel{
NSLog(@"adrf_bannerViewFailToReceived:%@, %@",errorModel.errorDescription, errorModel.errorDetailDict);
[_bannerView removeFromSuperview];
_bannerView = nil;
}
/**
广告点击
@param bannerView 广告实例
*/
- (void)adrf_bannerViewClicked:(ADRFMediationSDKBannerAdView *)bannerView{
}
/**
广告关闭
@param bannerView 广告实例
*/
- (void)adrf_bannerViewClose:(ADRFMediationSDKBannerAdView *)bannerView{
_bannerView = nil;
}
/**
广告展示
@param bannerView 广告实例
*/
- (void)adrf_bannerViewExposure:(ADRFMediationSDKBannerAdView *)bannerView{
NSLog(@"============曝光成功=========");
}
#pragma mark - Touch event
- (void)clickLoadBannerButton:(UIButton *)btn {
btn.enabled = NO;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
btn.enabled = YES;
});
for (UIButton *lbtn in self.btns) {
lbtn.backgroundColor = (lbtn == btn) ? [UIColor colorWithRed:222/255.0 green:236/255.0 blue:251/255.0 alpha:1.0] : [UIColor whiteColor];
}
AdRFMediationBannerItem *item = self.array[btn.tag];
[self loadBannerWithRate:item.rate posId:item.posId];
}
#pragma mark - Helper - UI
- (void)createButtonWithItem:(AdRFMediationBannerItem *)item index:(NSInteger)index {
UIButton *btn = [UIButton new];
[self.view addSubview:btn];
static NSInteger btn_num_per_line = 4;
static CGFloat btn_height = 32;
static CGFloat btn_margin_top = 10;
static CGFloat btn_margin_left = 10;
static CGFloat margin_top = 16;
static CGFloat margin_left = 17;
CGFloat btn_width = ((self.view.frame.size.width - margin_left * 2) - ((btn_num_per_line - 1) * btn_margin_left)) / btn_num_per_line;
CGFloat x = ((index % btn_num_per_line) * (btn_margin_left + btn_width)) + margin_left;
CGFloat y = (index / btn_num_per_line) * (btn_margin_top + btn_height) + margin_top + 100;
btn.frame = CGRectMake(x, y, btn_width, btn_height);
btn.tag = index;
[btn addTarget:self action:@selector(clickLoadBannerButton:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:item.title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor colorWithRed:0 green:122/255.0 blue:1 alpha:1] forState:UIControlStateNormal];
btn.layer.borderWidth = 1.0;
btn.layer.borderColor = [UIColor colorWithRed:199/255.0 green:199/255.0 blue:204/255.0 alpha:1.0].CGColor;
btn.titleLabel.font = [UIFont systemFontOfSize:14];
[self.btns addObject:btn];
[self.view addSubview:btn];
}
#pragma mark - Lazy load
- (NSArray<AdRFMediationBannerItem *> *)array {
if(!_array) {
NSMutableArray *array = [NSMutableArray new];
[array addObject:[AdRFMediationBannerItem itemWithRate:640/100.0 posId:@"84aa0f60143e6ab18c" title:@"640*100"]];
[array addObject:[AdRFMediationBannerItem itemWithRate:600/150.0 posId:@"" title:@"600*150"]];
[array addObject:[AdRFMediationBannerItem itemWithRate:600/260.0 posId:@"" title:@"600*260"]];
[array addObject:[AdRFMediationBannerItem itemWithRate:600/300.0 posId:@"" title:@"600*300"]];
[array addObject:[AdRFMediationBannerItem itemWithRate:690/388.0 posId:@"" title:@"690*388"]];
[array addObject:[AdRFMediationBannerItem itemWithRate:600/400.0 posId:@"" title:@"600*400"]];
[array addObject:[AdRFMediationBannerItem itemWithRate:600/500.0 posId:@"" title:@"600*500"]];
_array = [array copy];
}
return _array;
}
- (NSMutableArray<UIButton *> *)btns {
if(!_btns) {
_btns = [NSMutableArray new];
}
return _btns;;
}
@end
@implementation AdRFMediationBannerItem
+ (instancetype)itemWithRate:(CGFloat)rate posId:(NSString *)posId title:(NSString *)title {
AdRFMediationBannerItem *item = [super new];
if(self) {
item.rate = rate;
item.posId = posId;
item.title = title;
}
return item;
}
@end