IVTMeGiftViewController.m
12.7 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
// IVTMeGiftViewController.m
// IVTMeLive
//
// Created by XRG on 16/6/6.
// Copyright © 2016年 Joky. All rights reserved.
//
#import "IVTMeGiftViewController.h"
#import "IVTMeGiftCell.h"
#import "IVTMyAccountViewController.h"
@interface IVTMeGiftViewController ()<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>
{
NSIndexPath *selectedIndexPath;
NSInteger currentChineseCoinNumber;
}
@property(nonatomic,strong)UIButton *recharge;
@property(nonatomic,strong)UILabel *money;
@property(nonatomic,strong)UIImageView *chineseCoin;
@property(nonatomic,strong)UIButton *chongZhi;
@property(nonatomic,strong)UIButton *continueTo;
@property(nonatomic,strong)UIButton *send;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong) IVTMeGiftCell *giftCell;
@property (nonatomic,strong)NSArray *image;
@end
@implementation IVTMeGiftViewController
- (void)viewDidLoad {
[super viewDidLoad];
currentChineseCoinNumber = [[IVTAccountTool getChineseCoinTotal] integerValue]/100;
[self initBottom];
[self creatCollectionView];
self.view.frame =CGRectMake(0, kScreenHeight-240, kScreenWidth, 240);
self.view.backgroundColor = [UIColor YXColorWithHexCode:@"1e1e1e" alpha:0.68];
[self doRequest];
}
- (void)updataCurrentMiTang:(NSInteger)currentTotal{
currentChineseCoinNumber = currentTotal;
_money.text = [NSString stringWithFormat:@"%ld",(long)currentChineseCoinNumber];
}
- (void)doRequest
{
[[IVTNetworkAPIClient sharedClient] getGiftList:nil success:^(NSDictionary *successData) {
NSMutableArray *gifDataArray = [[successData valueForKey:@"data"] mutableCopy];
for (NSInteger i=0; i<gifDataArray.count; i++) {
NSMutableDictionary *dict = [[gifDataArray objectAtIndex:i] mutableCopy];
[dict setObject:@"0" forKey:@"isSelected"];
[gifDataArray replaceObjectAtIndex:i withObject:dict];
}
self.image = [gifDataArray copy];
[self.collectionView reloadData];
} error:^(NSDictionary *successData) {
} failure:^(NSError *failureError) {
LRLog(@"fialureError:%@",failureError);
}];
}
#pragma mark - 初始化底部
-(void)initBottom{
__unsafe_unretained typeof (self) wself = self;
_recharge = [UIButton buttonWithType:UIButtonTypeCustom];
[_recharge setBackgroundColor:[UIColor clearColor]];
[_recharge setTitle:@"充值" forState:UIControlStateNormal];
[_recharge setTitleColor:[UIColor IVTLightRedColor] forState:UIControlStateNormal];
_recharge.titleLabel.font = [UIFont IVTH3Font];
if ([_recharge.titleLabel respondsToSelector:@selector(setAdjustsFontForContentSizeCategory:)]) {
[_recharge.titleLabel setAdjustsFontForContentSizeCategory:YES];
}
[self.view addSubview:_recharge];
[_recharge mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(wself.view.mas_bottom).with.offset(-11);
make.left.equalTo(wself.view).with.offset(10);
make.size.mas_equalTo(CGSizeMake(40, 17));
}];
_money = [[UILabel alloc]init];
_money.text = [NSString stringWithFormat:@"%ld",(long)currentChineseCoinNumber];
_money.textColor = [UIColor YXColorWithHexCode:@"bcbcbc"];
_money.font = [UIFont YXFontOfSize:16];
[self.view addSubview:_money];
[_money mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_recharge.mas_right).with.offset(8);
make.bottom.equalTo(wself.view.mas_bottom).with.offset(-12);
make.size.mas_equalTo(CGSizeMake([IVTThirdFuncTool widthOfLabelWithString:_money.text sizeOfFont:16 height:17 ] + 10, 17));
}];
_chineseCoin = [[UIImageView alloc]init];
_chineseCoin.image = [UIImage imageNamed:@"小中国币(礼物下面)"];
[self.view addSubview:_chineseCoin];
[_chineseCoin mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_money.mas_right).with.offset(6);
make.bottom.equalTo(wself.view.mas_bottom).with.offset(-13);
make.size.mas_equalTo(CGSizeMake(13, 13));
}];
_chongZhi = [UIButton buttonWithType:UIButtonTypeCustom];
[_chongZhi setBackgroundColor:[UIColor clearColor]];
[_chongZhi setBackgroundImage:[UIImage imageNamed:@"更多键"] forState:UIControlStateNormal];
[_chongZhi addTarget:self action:@selector(clickRechargeButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_chongZhi];
[_chongZhi mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(wself.view.mas_bottom).with.offset(-11);
make.left.equalTo(_chineseCoin.mas_right).with.offset(10);
make.size.mas_equalTo(CGSizeMake(9, 15));
}];
UIButton *clickButton = [UIButton buttonWithType:UIButtonTypeCustom];
[clickButton addTarget:self action:@selector(clickRechargeButton) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:clickButton];
[clickButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_recharge.mas_left);
make.top.equalTo(_money.mas_top);
make.right.equalTo(_chineseCoin.mas_right);
make.bottom.equalTo(_recharge.mas_bottom);
}];
_send = [UIButton buttonWithType:UIButtonTypeCustom];
[_send setTitle:@"发送" forState:UIControlStateNormal];
[_send setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_send setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[_send setBackgroundImage:[UIImage imageNamed:@"发送按钮"] forState:UIControlStateNormal];
[_send setBackgroundImage:[UIImage imageNamed:@"发送按钮-灰色"] forState:UIControlStateSelected];
_send.titleLabel.font = [UIFont YXFontOfSize:17];
[self.view addSubview:_send];
[_send mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(wself.view.mas_bottom);
make.right.equalTo(wself.view.mas_right);
make.size.mas_equalTo(CGSizeMake(90, 40));
}];
[_send addTarget:self action:@selector(sendClick:) forControlEvents:UIControlEventTouchUpInside];
_continueTo = [UIButton buttonWithType:UIButtonTypeCustom];
[_continueTo setTitle:@"x10" forState:UIControlStateNormal];
[_continueTo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[_continueTo setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[_continueTo setBackgroundImage:[UIImage imageNamed:@"x10 未选中"] forState:UIControlStateNormal];
[_continueTo setBackgroundImage:[UIImage imageNamed:@"x10 选中-0"] forState:UIControlStateSelected];
_continueTo.titleLabel.font = [UIFont YXFontOfSize:16];
[_continueTo addTarget:self action:@selector(lianFaClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_continueTo];
[_continueTo mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(wself.view.mas_bottom).with.offset(-8);
make.right.equalTo(_send.mas_left).with.offset(-22);
make.size.mas_equalTo(CGSizeMake(35, 25));
}];
}
- (void)lianFaClick{
_continueTo.selected = !_continueTo.selected;
}
-(void)creatCollectionView{
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 190.0)collectionViewLayout:layout];
_collectionView.backgroundColor = [UIColor redColor];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.contentSize = CGSizeMake(kScreenWidth*4, 190.0);
[self.view addSubview:_collectionView];
[_collectionView registerClass:[IVTMeGiftCell class] forCellWithReuseIdentifier:@"giftCell"];
}
#pragma mark - delegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.image.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
IVTMeGiftCell *giftCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"giftCell" forIndexPath:indexPath];
giftCell.entity = self.image[indexPath.row];
return giftCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSMutableArray *gifDataArray = [self.image mutableCopy];
for (NSInteger i=0; i<gifDataArray.count; i++) {
if (indexPath.row != i) {
NSMutableDictionary *dict = [[gifDataArray objectAtIndex:i] mutableCopy];
[dict setObject:@"0" forKey:@"isSelected"];
[gifDataArray replaceObjectAtIndex:i withObject:dict];
}
}
self.image = [gifDataArray copy];
NSMutableDictionary *dict = [[self.image objectAtIndex:indexPath.row] mutableCopy];
if ([dict[@"isSelected"] integerValue] == 0) {
[dict setObject:@"1" forKey:@"isSelected"];
selectedIndexPath = indexPath;
}
else{
[dict setObject:@"0" forKey:@"isSelected"];
selectedIndexPath = nil;
}
NSMutableArray *gifImageArray = [self.image mutableCopy];
[gifImageArray replaceObjectAtIndex:indexPath.row withObject:[dict copy]];
self.image = [gifImageArray copy];
_continueTo.selected = NO;
if ([dict[@"type"] integerValue] == 0) {
_continueTo.hidden = NO;
// _continueTo.userInteractionEnabled = YES;
}
else{
_continueTo.hidden = YES;
// _continueTo.userInteractionEnabled = NO;
}
[_collectionView reloadData];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat itemWidth = [UIScreen mainScreen].bounds.size.width / 4.0;
CGFloat itemHeight = 190.0/2.0;
return CGSizeMake(itemWidth, itemHeight);
}
//这个方法是两个之间的间距(同一行cell的间距)
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
//上下间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0;
}
-(void)clickRechargeButton
{
NSLog(@"充值");
IVTMyAccountViewController *account = [[IVTMyAccountViewController alloc]init];
[self.navigationController pushViewController:account animated:YES];
}
#pragma mark - 发送点击
-(void)sendClick:(UIButton *)sender{
if (selectedIndexPath) {
NSInteger price = [[[self.image objectAtIndex:selectedIndexPath.row] valueForKey:@"price"] integerValue];
if (_continueTo.selected && _continueTo.hidden == NO) {
price = 10*price;
}
if (currentChineseCoinNumber > price) {
if (self.delegate && [self.delegate respondsToSelector:@selector(sendGiftWithData:isTenTimes:)]) {
[self.delegate sendGiftWithData:[self.image objectAtIndex:selectedIndexPath.row] isTenTimes:_continueTo.selected];
}
}
else{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"余额不足,请充值!" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"去充值" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self recharge];
}]];
[self presentViewController:alert animated:YES completion:^{
}];
}
}
else{
UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择礼物" preferredStyle:UIAlertControllerStyleAlert];
[sheet addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:sheet animated:YES completion:^{
}];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end