YH_BarrageViewController.m
8.88 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
//
// 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 "UtilsMacros.h"
#import "UIView+YOHO.h"
#import "UIFont+LIVE.h"
#import "UIColor+YOHO.h"
#define kBarrageTableViewHeight 253
#define kBarrageTableViewWidth (245 * kScreenPointScale)
#define kInputTextFieldHeight 34
#define kInputTextFieldBottomEdge 49
#define kPaddingLeft 13
#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];
}
- (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 = kScreenHeight / 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, kScreenWidth, 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(kScreenWidth - 2*kPaddingLeft- kCloseButtonWidth, 0, kCloseButtonWidth + 2*kPaddingLeft, kInputTextFieldHeight)];
[self.closeButtonContainer yh_setCenterY:self.inputTextField.yh_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 yh_setCenterY:self.inputTextField.yh_centerY];
[self.closeButtonContainer addSubview:self.keyBoardCloseButton];
[self.bottomContainerView addSubview:self.closeButtonContainer];
[self.view addSubview:self.bottomContainerView];
self.messageTip = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth - kMessageTipWidth - kMessageTipLeftEdge, self.barrageTableView.yh_bottom - 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, 8, 10, 6)];
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];
CGSize size = CGSizeMake(60,kMessageTipHeight);
CGSize labelsize = [self.newsBubble sizeThatFits:size];
[self.newsBubble setFrame:CGRectMake(25, (kMessageTipHeight - labelsize.height) / 2, labelsize.width, labelsize.height)];
[self.messageTip yh_setWidth:CGRectGetMaxX(self.newsBubble.frame) + 10];
[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 fontOfSCWithSize:11];
_newsBubble.textColor = [UIColor blackColor];
}
return _newsBubble;
}
#pragma mark - HPGrowingTextViewDelegate
- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
return YES;
}
- (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView {
return NO;
}
-(void)textViewDidChange:(HPGrowingTextView *)growingTextView
{
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end