YH_BarrageViewController.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
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// YH_Barrage.m
// YHBarrage
//
// Created by Arthur on 16/6/6.
// Copyright © 2016年 YOHO!. All rights reserved.
//
#import "YH_BarrageViewController.h"
#import "YH_BarrageTableView.h"
//#import "YH_YohoLoginViewModel.h"
#import "M16HUDUtil.h"
#define kBarrageTableViewHeight 253
#define kBarrageTableViewWidth (245 * kScreenPointScale)
#define kInputTextFieldHeight 34
#define kInputTextFieldBottomEdge 49
#define kPaddingLeft 15
#define kCloseButtonWidth 19
#define kMessageTipLeftEdge 111
#define kMessageTipHeight 21.5
#define kMessageTipWidth 87.5
@interface YH_BarrageViewController ()<YH_BarrageTableViewScrollingDelegate, HPGrowingTextViewDelegate>
@property (strong, nonatomic) UIImageView *inputTextBgView;
@property (strong, nonatomic) UIButton *keyBoardCloseButton;
@property (strong, nonatomic) UIView *closeButtonContainer;
@property (strong, nonatomic) UIView *bottomContainerView;
@property (strong, nonatomic) UIImageView *messageTip;
@property (strong, nonatomic) UIImageView *messageArrow;
@property (strong, nonatomic)YH_BarrageTableView *barrageTableView;
@property (nonatomic) YHBarrageType barrageType;
@property (assign, nonatomic) CGSize keyboardSize;
@property (strong, nonatomic) UILabel *newsBubble;
@end
@implementation YH_BarrageViewController
#pragma mark - life cycle
- (instancetype)initWithType:(YHBarrageType)barrageType
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.barrageType = barrageType;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.barrageTableView];
[self setBottomView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.barrageType == YHBarrageTypeSend) {
// 显示输入框
self.inputTextField.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
self.inputTextField.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
self.inputTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
} else if(self.barrageType == YHBarrageTypeReadonly) {
// 不显示输入框
self.bottomContainerView.hidden = YES;
self.messageTip.hidden = YES;
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)setBottomView
{
CGFloat scalePoint = SCREEN_HEIGHT / 667.0;
[self.barrageTableView setFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-kBarrageTableViewHeight * scalePoint-kInputTextFieldBottomEdge, kBarrageTableViewWidth, kBarrageTableViewHeight * scalePoint)];
self.bottomContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.barrageTableView.frame) + 5, SCREEN_WIDTH, kInputTextFieldHeight)];
self.bottomContainerView.backgroundColor = [UIColor clearColor];
[self.inputTextField setFrame:CGRectMake(kPaddingLeft, 0, CGRectGetWidth(self.view.bounds) - 2*kPaddingLeft, kInputTextFieldHeight)];
self.inputTextBgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds) - 2*kPaddingLeft, kInputTextFieldHeight)];
self.inputTextBgView.image = [UIImage imageNamed:@"inputTextBg"];
[self.inputTextField addSubview:self.inputTextBgView];
[self.inputTextField sendSubviewToBack:self.inputTextBgView];
[self.bottomContainerView addSubview:self.inputTextField];
[self.bottomContainerView bringSubviewToFront:self.inputTextField];
self.closeButtonContainer = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 2*kPaddingLeft- kCloseButtonWidth, 0, kCloseButtonWidth + 2*kPaddingLeft, kInputTextFieldHeight)];
self.closeButtonContainer.centerY = self.inputTextField.centerY;
UITapGestureRecognizer *closeGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoard:)];
[self.closeButtonContainer addGestureRecognizer:closeGesture];
self.closeButtonContainer.userInteractionEnabled = YES;
self.closeButtonContainer.hidden = YES;
self.keyBoardCloseButton = [[UIButton alloc] initWithFrame:CGRectMake(kPaddingLeft, 0, kCloseButtonWidth, kCloseButtonWidth)];
[self.keyBoardCloseButton setImage:[UIImage imageNamed:@"keyBoardClose"] forState:UIControlStateNormal];
self.keyBoardCloseButton.userInteractionEnabled = NO;
self.keyBoardCloseButton.centerY = self.inputTextField.centerY;
[self.closeButtonContainer addSubview:self.keyBoardCloseButton];
[self.bottomContainerView addSubview:self.closeButtonContainer];
[self.view addSubview:self.bottomContainerView];
self.messageTip = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - kMessageTipWidth - kMessageTipLeftEdge, self.barrageTableView.bottom - 5 - kMessageTipHeight, kMessageTipWidth, kMessageTipHeight)];
self.messageTip.image = [UIImage imageNamed:@"message_Tip"];
self.messageTip.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(newsBubbleClick:)];
tapGR.numberOfTapsRequired = 1;
[self.messageTip addGestureRecognizer:tapGR];
self.messageArrow = [[UIImageView alloc] initWithFrame:CGRectMake(10, 7, 10, 8)];
self.messageArrow.image = [UIImage imageNamed:@"messageArrow"];
[self.messageTip addSubview:self.messageArrow];
[self.newsBubble setFrame:CGRectMake(CGRectGetMaxX(self.messageArrow.frame) + 5, 0, kMessageTipWidth - CGRectGetMaxX(self.messageArrow.frame) - 10, kMessageTipHeight)];
[self.messageTip addSubview:self.newsBubble];
self.messageTip.hidden = YES;
[self.view addSubview:self.messageTip];
}
-(void)closeKeyBoard:(UIButton *)sender
{
[self.inputTextField resignFirstResponder];
}
#pragma mark -
- (void)show
{
[[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:self.view];
[[UIApplication sharedApplication].keyWindow.rootViewController addChildViewController:self];
}
- (void)insertItem:(NSDictionary *)itemdic
{
[self.barrageTableView yhBarrageTableView:self.barrageTableView insertCellWithItem:itemdic];
}
- (void)reset
{
[self.barrageTableView reset];
}
- (void)newsBubbleClick:(id)sender
{
[self.barrageTableView startAutoScolling];
[self.messageTip setHidden:YES];
}
#pragma mark - YH_BarrageTableViewScrollingDelegate
- (void)manualScrollingWithNewMsgCount:(NSString *)newMsgCount
{
NSString *msgCount ;
if ([newMsgCount integerValue] > 99) {
msgCount = @"99+条信息";
}else {
msgCount = [NSString stringWithFormat:@"%ld条信息",(long)[newMsgCount integerValue]];
}
[self.newsBubble setText:msgCount];
[self.messageTip setHidden:NO];
}
- (void)autoScollingCallback
{
[self.messageTip setHidden:YES];
}
#pragma mark - getters and setters
- (YH_BarrageTableView *)barrageTableView
{
if (_barrageTableView == nil) {
_barrageTableView = [[YH_BarrageTableView alloc]init];
_barrageTableView.barrageScrollingDelegate = self;
}
return _barrageTableView;
}
- (HPGrowingTextView *)inputTextField
{
if (_inputTextField == nil) {
_inputTextField = [[HPGrowingTextView alloc]init];
_inputTextField.isScrollable = NO;
_inputTextField.returnKeyType = UIReturnKeySend;
_inputTextField.backgroundColor = [UIColor clearColor];
_inputTextField.placeholder = @"说点什么吧...";
_inputTextField.font = [UIFont systemFontOfSize:15.0f];
_inputTextField.textColor = [UIColor whiteColor];
_inputTextField.minNumberOfLines = 1;
_inputTextField.maxNumberOfLines = 1;
_inputTextField.internalTextView.textContainer.maximumNumberOfLines = 1;
_inputTextField.internalTextView.textContainer.lineBreakMode = NSLineBreakByTruncatingHead;
_inputTextField.delegate = self;
}
return _inputTextField;
}
- (UILabel *)newsBubble
{
if (_newsBubble == nil) {
_newsBubble = [[UILabel alloc]init];
_newsBubble.userInteractionEnabled = YES;
_newsBubble.textAlignment = NSTextAlignmentCenter;
_newsBubble.font = [UIFont fontOfLightWithSize:11];
_newsBubble.textColor = [UIColor blackColor];
}
return _newsBubble;
}
#pragma mark - HPGrowingTextViewDelegate
- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
// if ([text isEqualToString:@"\n"]) {
// if (![self.inputTextField.text isEqualToString:@""]) {
// if ([GVUserDefaults standardUserDefaults].userLogged) {
// [self.inputTextField resignFirstResponder];
// if (self.sendMsgBlock) {
// self.sendMsgBlock(self.inputTextField.text);
// }
// self.inputTextField.text = @"";
// return YES;
// } else {
// __weak typeof(self) weakSelf = self;
// [self.inputTextField resignFirstResponder];
// [[YH_YohoLoginViewModel loginMgr] excuteBlockRequireHasLogin:^{
// __strong typeof(weakSelf) strongSelf = weakSelf;
// if (strongSelf.sendMsgBlock) {
// strongSelf.sendMsgBlock(self.inputTextField.text);
// }
//
// if (strongSelf.loginToTCPServerBlock) {
// strongSelf.loginToTCPServerBlock();
// }
// strongSelf.inputTextField.text = @"";
// }];
// return YES;
// }
// }else {
// return NO;
// }
// }else {
// NSString * aString = [self.inputTextField.text stringByReplacingCharactersInRange:range withString:text];
// if (aString.length > 30) {
// [self showText:@"内容不可以超过30个字"];
// self.inputTextField.text = [aString substringToIndex:30];
// return NO;
// }
// }
return YES;
}
//-(void)textViewDidChange:(HPGrowingTextView *)growingTextView
//{
// NSInteger length = self.inputTextField.text.length;
// if (length > 30) {
// [self showText:@"内容不可以超过30个字"];
// self.inputTextField.text = [self.inputTextField.text substringToIndex:30];
// }
//}
#pragma mark - UIKeyboard changes notification
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
self.keyboardSize = [value CGRectValue].size;
[UIView animateWithDuration:0.3 animations:^{
CGRect iRect = self.bottomContainerView.frame;
iRect.origin.y = SCREEN_HEIGHT - self.keyboardSize.height - CGRectGetHeight(self.bottomContainerView.frame) - 5;
self.bottomContainerView.frame = iRect;
self.inputTextField.width = SCREEN_WIDTH - 3*kPaddingLeft - kCloseButtonWidth;
self.inputTextBgView.width = SCREEN_WIDTH - 3*kPaddingLeft - kCloseButtonWidth;
self.closeButtonContainer.hidden = NO;
CGRect tRect = self.barrageTableView.frame;
tRect.origin.y = self.bottomContainerView.top - CGRectGetHeight(tRect) - 5;
self.barrageTableView.frame = tRect;
[self.messageTip setY:self.barrageTableView.bottom - 5 - kMessageTipHeight];
}];
if (self.keyBoardShowBlock) {
self.keyBoardShowBlock(YES, self.keyboardSize.height);
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
self.keyboardSize = CGSizeZero;
[UIView animateWithDuration:0.3 animations:^{
CGRect iRect = self.bottomContainerView.frame;
iRect.origin.y = SCREEN_HEIGHT - iRect.size.height - 5;
self.bottomContainerView.frame = iRect;
self.inputTextField.width = SCREEN_WIDTH - 2*kPaddingLeft;
self.inputTextBgView.width = SCREEN_WIDTH - 2*kPaddingLeft;
self.closeButtonContainer.hidden = YES;
CGRect tRect = self.barrageTableView.frame;
tRect.origin.y = SCREEN_HEIGHT - CGRectGetHeight(tRect) - kInputTextFieldBottomEdge;
self.barrageTableView.frame = tRect;
[self.messageTip setY:self.barrageTableView.bottom - 5 - kMessageTipHeight];
}];
if (self.keyBoardShowBlock) {
self.keyBoardShowBlock(NO, self.keyboardSize.height);
}
}
//- (void)showText:(NSString *)text
//{
// if ([text notNilOrEmpty]) {
// [M16HUDUtil textOnly:text inView:self.view];
// }
//}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end