GiftListView.m
12.5 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
336
337
//
// GiftListView.m
// LiveVideoCloud
//
// Created by 张旭 on 2017/3/14.
// Copyright © 2017年 cnlive. All rights reserved.
//
#import "GiftListView.h"
#import "LiveBaseView.h"
#import "AnimQueueManager.h"
#import "GiftPayButton.h"
#define MAX_LINE_COUNT 4
#define MAX_ROW_COUNT 2
#define GiftPayViewHeight 50
@interface GiftListView ()
@property (nonatomic ,strong) NSArray <GiftModel *>* _Nullable giftArray;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable images;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable giftNames;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable prices;
@property (nonatomic ,strong) NSArray <NSString *>* _Nullable types;
@property (nonatomic ,weak ) LiveBaseView * _Nullable liveBaseView;
@property (nonatomic, strong) AnimQueueManager * animQueueManager;
@property (nonatomic ,strong) GiftModel * _Nullable model;
@property (nonatomic ,strong) UIScrollView * _Nullable scrollView;
@property (nonatomic ,assign) BOOL isSelectedGift;
@property (nonatomic ,assign) NSInteger curruntSelectIdx;
@end
@implementation GiftListView
- (instancetype)initWithFrame:(CGRect)frame liveBaseView:(LiveBaseView *_Nullable)liveBaseView
{
if (self = [super initWithFrame:frame]) {
_isSelectedGift = NO;
_images = [NSArray arrayWithObjects:@"porsch_small80",@"fighter_small80",@"plane_small80",@"love_gift",@"beer",@"chicken",@"cucumber",@"flower",@"hug",@"mouth",@"ring",@"rose",@"sticks",nil];
_giftNames = [NSArray arrayWithObjects:@"跑车",@"歼灭机",@"飞机",@"大大的爱心",@"啤酒",@"小鸡",@"小黄瓜",@"鲜花",@"抱抱",@"亲吻",@"戒指",@"玫瑰花",@"荧光棒",nil];
_prices = [NSArray arrayWithObjects:@"300000000",@"300000000",@"300000",@"30",@"30",@"3",@"3",@"1",@"3",@"1",@"3",@"30",@"300", nil];
_types = [NSArray arrayWithObjects:@"2",@"3",@"4",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil];
self.liveBaseView = liveBaseView;
self.animQueueManager = [AnimQueueManager sharedAnimQueueManager];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height - GiftPayViewHeight)];
scrollView.contentSize = CGSizeMake(self.width *(self.giftNames.count/(MAX_ROW_COUNT*MAX_LINE_COUNT) + 1), self.height - GiftPayViewHeight);
scrollView.pagingEnabled = YES;
scrollView.bounces = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:scrollView];
self.scrollView = scrollView;
int line = 0;
int row = 0;
CGFloat giftX = 0;
CGFloat giftY = 0;
int page = 0;
for (int i = 0; i < self.giftNames.count; i ++) {
if (i % MAX_LINE_COUNT == 0 && i > 0) {
line ++;
}
if (i % (MAX_LINE_COUNT * MAX_ROW_COUNT) == 0 && i > 0) {
page++;
line = 0;
}
row = i % MAX_LINE_COUNT;
CGFloat giftWidth = self.width / MAX_LINE_COUNT * 1.0;
CGFloat giftHeight = (self.height - GiftPayViewHeight) / MAX_ROW_COUNT * 1.0;
giftX = row * giftWidth + page * scrollView.width;
giftY = line * giftHeight;
UIView *giftView = [self giftViewWithFrame:CGRectMake(giftX, giftY, giftWidth, giftHeight) model:self.giftArray[i]];
giftView.backgroundColor = [UIColor clearColor];
//礼物图片
UIImageView *picImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.giftArray[i].giftImageName]];
picImg.centerX = giftView.width * 0.5;
picImg.centerY = giftView.height * 0.5;
[giftView addSubview:picImg];
[scrollView addSubview:giftView];
UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureClick:)];
giftView.tag = i + 10;
[giftView addGestureRecognizer:gesture];
}
UIImageView *giftBottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height - GiftPayViewHeight, self.width, GiftPayViewHeight)];
giftBottomView.image = [UIImage imageNamed:@"背景400x30"];
giftBottomView.alpha = 0.4;
[self addSubview:giftBottomView];
GiftPayButton *payBtn = [[GiftPayButton alloc] initWithFrame:CGRectMake(10, self.height - GiftPayViewHeight, 80, GiftPayViewHeight)];
[payBtn addTarget:self action:@selector(payClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:payBtn];
UIButton *sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 70, self.height - GiftPayViewHeight + (GiftPayViewHeight - 30)*0.5, 60, 30)];
[sendBtn addTarget:self action:@selector(sendClick) forControlEvents:UIControlEventTouchUpInside];
[sendBtn setTitle:@"发送" forState:UIControlStateNormal];
[sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal
];
[sendBtn setBackgroundColor:[UIColor orangeColor]];
sendBtn.layer.cornerRadius = sendBtn.height * 0.5;
sendBtn.layer.masksToBounds = YES;
[self addSubview:sendBtn];
}
return self;
}
- (void)sendClick
{
if (_isSelectedGift) {//当前选择了礼物
if ([self.delegate respondsToSelector:@selector(giftSelectedModel:)]) {
[self.delegate giftSelectedModel:_model];
}
}
}
- (void)payClick
{
NSLog(@"payClick");
}
- (UIView *)giftViewWithFrame:(CGRect)frame model:(GiftModel *)model
{
_model = model;
UIView *giftView = [[UIView alloc] initWithFrame:frame];
if (model.giftStyle == GiftStyleCombo)
{//右上角连击标识
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(giftView.width - 17, 3, 15, 15)];
imageView.image = [UIImage imageNamed:@"continuous"];
[giftView addSubview:imageView];
}
//礼物价格lbl
UILabel *priceLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, giftView.height - 15, 40, 14)];
priceLbl.font = [UIFont systemFontOfSize:kDetailContentFont];
priceLbl.textColor = [UIColor greenColor];
priceLbl.text = model.price;
priceLbl.width = [priceLbl labelSize].width;
priceLbl.height = [priceLbl labelSize].height;
priceLbl.centerX = giftView.width * 0.5 - 4;
[giftView addSubview:priceLbl];
//礼物价格图标
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"diamond"]];
image.left = priceLbl.right + 3;
image.centerY = priceLbl.centerY;
[giftView addSubview:image];
return giftView;
}
- (void)giftViewAddBgView:(UIView *)giftView
{
UIImageView *bgView = [[UIImageView alloc] initWithFrame:giftView.bounds];
bgView.backgroundColor = [UIColor blackColor];
bgView.tag = giftView.tag + 100;
bgView.alpha = 0.2;
[giftView addSubview:bgView];
}
#pragma mark - 选择礼物点击
- (void)gestureClick:(UITapGestureRecognizer *)gesture
{
NSInteger tag = gesture.view.tag - 10;
NSLog(@"%zd",tag);
if (_curruntSelectIdx == 0 && _isSelectedGift == NO) {//没选过
[self giftViewAddBgView:gesture.view];
_isSelectedGift = YES;
}
if (_curruntSelectIdx != 0 && _isSelectedGift == YES) {//选过
if (gesture.view.tag != _curruntSelectIdx) {//选的其他礼物
//增加当前bgView
[self giftViewAddBgView:gesture.view];
for (UIView *view in self.scrollView.subviews) {//移除上一个bgView
if (view.tag == _curruntSelectIdx) {
for (UIView *bg in view.subviews) {
if (bg.tag == view.tag + 100) {
[bg removeFromSuperview];
break;
}
}
}
}
_isSelectedGift = YES;
}else{//选的当前礼物 ,根据是否选中做变化
if (_isSelectedGift == YES) {
_isSelectedGift = NO;
for (UIView *view in self.scrollView.subviews) {//移除上一个bgView
if (view.tag == _curruntSelectIdx) {
for (UIView *bg in view.subviews) {
if (bg.tag == view.tag + 100) {
[bg removeFromSuperview];
break;
}
}
}
}
}else{
_isSelectedGift = YES;
[self giftViewAddBgView:gesture.view];
}
}
}
_curruntSelectIdx = gesture.view.tag;
GiftModel *model = self.giftArray[tag];
model.sender = [CNLiveChatManager sharedCNLiveChatManager].currentUserInfo.name;
model.icon = [CNLiveChatManager sharedCNLiveChatManager].currentUserInfo.portraitUri;
model.giftStyle = [self giftStyleWithTag:tag];
#warning 测试时 price 用假值 model.giftNumber赋值会导致不能本地连击以后再说
model.price = @"0";
// model.giftNumber = 3;
model.index = tag;
_model = model;
}
//应该加验证发送礼物消息成功后 再展示动画
- (void)addGiftAnimationWithModel:(GiftModel *_Nonnull)model
{
GiftModel *tempMdl = self.giftArray[model.index];
model.giftImageName = tempMdl.giftImageName;
dispatch_async(dispatch_get_main_queue(), ^{
if (model.giftStyle == GiftStyleCombo) {
[self.animQueueManager addAnimationWithModel:model animContentView:(UIView *)self.liveBaseView.presentView];
}else
{
[self.animQueueManager addAnimationWithModel:model animContentView:self.liveBaseView];
}
});
}
- (GiftStyle)giftStyleWithTag:(NSInteger)tag
{
GiftStyle style;
if (tag == 0) {
style = GiftStylePorsch;
}else if (tag == 1){
style = GiftStyleFighter;
}else if (tag == 2){
style = GiftStylePlane;
}else{
style = GiftStyleCombo;
}
return style;
}
- (NSString *)giftNameWithImageName:(NSString *)imageName
{
NSString *giftName = @"";
if ([_images containsObject:imageName]) {
NSInteger index = [_images indexOfObject:imageName];
giftName = [_giftNames objectAtIndex:index];
}
return giftName;
}
- (NSInteger)recGiftIndexFromGiftName:(NSString *)giftName
{
NSInteger index = 0;
for (NSString *str in _giftNames) {
if ([str isEqualToString:giftName]) {
index = [_giftNames indexOfObject:str];
NSLog(@"rec index %zd,name %@",index,str);
}
}
return index;
}
- (void)cancelAllGiftAnimations
{
[self.animQueueManager cancelAllGiftOperations];
}
#pragma mark - getter
- (NSArray *)giftArray
{
if (!_giftArray)
{
NSMutableArray *tempArr = [NSMutableArray array];
for (int i = 0; i < _images.count; i ++) {
NSString *type = _types[i];
if ([type isEqualToString:@"1"]) {
//combo
GiftModel *model1 = [GiftModel modelWithGiftName:[self giftNameWithImageName:_images[i]] icon:@"" giftImageName:_images[i]];
model1.giftStyle = GiftStyleCombo;
model1.price = _prices[i];
[tempArr addObject:model1];
}else
{//expen
GiftModel *model = [GiftModel modelWithGiftName:[self giftNameWithImageName:_images[i]] icon:@"" giftImageName:@""];
if ([type isEqualToString:@"2"]) {
model.giftStyle = GiftStylePorsch;
model.giftImageName = @"porsch_small80";
}
if ([type isEqualToString:@"3"]) {
model.giftStyle = GiftStyleFighter;
model.giftImageName = @"fighter_small80";
}
if ([type isEqualToString:@"4"]) {
model.giftStyle = GiftStylePlane;
model.giftImageName = @"plane_small80";
}
model.price = _prices[i];
[tempArr addObject:model];
}
}
_giftArray = [[NSArray alloc] initWithArray:tempArr];
}
return _giftArray;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end