This repository was archived by the owner on Jul 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathLCCKChatBar.m
More file actions
executable file
·864 lines (771 loc) · 34.4 KB
/
LCCKChatBar.m
File metadata and controls
executable file
·864 lines (771 loc) · 34.4 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//
// LCCKChatBar.m
// LCCKChatBarExample
//
// v0.8.5 Created by ElonChan ( https://github.com/leancloud/ChatKit-OC ) on 15/8/17.
// Copyright (c) 2015年 https://LeanCloud.cn . All rights reserved.
//
#import "LCCKChatBar.h"
#import "LCCKChatMoreView.h"
#import "LCCKChatFaceView.h"
#import "LCCKRecordAudioHUD.h"
#import "Mp3Recorder.h"
#if __has_include(<Masonry/Masonry.h>)
#import <Masonry/Masonry.h>
#else
#import "Masonry.h"
#endif
#import "LCCKUIService.h"
#import "UIImage+LCCKExtension.h"
#import "NSString+LCCKExtension.h"
#import "LCCKConversationService.h"
NSString *const kLCCKBatchDeleteTextPrefix = @"kLCCKBatchDeleteTextPrefix";
NSString *const kLCCKBatchDeleteTextSuffix = @"kLCCKBatchDeleteTextSuffix";
@interface LCCKChatBar () <UITextViewDelegate, UINavigationControllerDelegate, Mp3RecorderDelegate, LCCKChatFaceViewDelegate>
@property (strong, nonatomic) Mp3Recorder *MP3;
@property (nonatomic, strong) UIView *inputBarBackgroundView; /**< 输入栏目背景视图 */
@property (strong, nonatomic) UIButton *voiceButton; /**< 切换录音模式按钮 */
@property (strong, nonatomic) UIButton *voiceRecordButton; /**< 录音按钮 */
@property (strong, nonatomic) UIButton *faceButton; /**< 表情按钮 */
@property (strong, nonatomic) UIButton *moreButton; /**< 更多按钮 */
@property (weak, nonatomic) LCCKChatFaceView *faceView; /**< 当前活跃的底部view,用来指向faceView */
@property (weak, nonatomic) LCCKChatMoreView *moreView; /**< 当前活跃的底部view,用来指向moreView */
@property (assign, nonatomic, readonly) CGFloat bottomHeight;
@property (strong, nonatomic, readonly) UIViewController *rootViewController;
@property (assign, nonatomic) CGSize keyboardSize;
@property (strong, nonatomic) UITextView *textView;
@property (assign, nonatomic) CGFloat oldTextViewHeight;
@property (nonatomic, assign, getter=shouldAllowTextViewContentOffset) BOOL allowTextViewContentOffset;
@property (nonatomic, assign, getter=isClosed) BOOL close;
#pragma mark - MessageInputView Customize UI
///=============================================================================
/// @name MessageInputView Customize UI
///=============================================================================
@property (nonatomic, strong) UIColor *messageInputViewBackgroundColor;
@property (nonatomic, strong) UIColor *messageInputViewTextFieldTextColor;
@property (nonatomic, strong) UIColor *messageInputViewTextFieldBackgroundColor;
@property (nonatomic, strong) UIColor *messageInputViewRecordTextColor;
//TODO:MessageInputView-Tint-Color
@end
@implementation LCCKChatBar
#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
}
- (void)setupConstraints {
CGFloat offset = 5;
[self.inputBarBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.and.top.mas_equalTo(self);
make.bottom.mas_equalTo(self).priorityLow();
}];
[self.voiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.inputBarBackgroundView.mas_left).with.offset(offset);
make.bottom.equalTo(self.inputBarBackgroundView.mas_bottom).with.offset(-kChatBarBottomOffset);
make.width.equalTo(self.voiceButton.mas_height);
}];
[self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.inputBarBackgroundView.mas_right).with.offset(-offset);
make.bottom.equalTo(self.inputBarBackgroundView.mas_bottom).with.offset(-kChatBarBottomOffset);
make.width.equalTo(self.moreButton.mas_height);
}];
[self.faceButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.moreButton.mas_left).with.offset(-offset);
make.bottom.equalTo(self.inputBarBackgroundView.mas_bottom).with.offset(-kChatBarBottomOffset);
make.width.equalTo(self.faceButton.mas_height);
}];
[self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.voiceButton.mas_right).with.offset(offset);
make.right.equalTo(self.faceButton.mas_left).with.offset(-offset);
make.top.equalTo(self.inputBarBackgroundView).with.offset(kChatBarTextViewBottomOffset);
make.bottom.equalTo(self.inputBarBackgroundView).with.offset(-kChatBarTextViewBottomOffset);
make.height.mas_greaterThanOrEqualTo(kLCCKChatBarTextViewFrameMinHeight);
}];
CGFloat voiceRecordButtoInsets = -5.f;
[self.voiceRecordButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.textView).insets(UIEdgeInsetsMake(voiceRecordButtoInsets, voiceRecordButtoInsets, voiceRecordButtoInsets, voiceRecordButtoInsets));
}];
[self.faceView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.and.left.mas_equalTo(self);
make.height.mas_equalTo(kFunctionViewHeight);
make.top.mas_equalTo(self.mas_bottom);
}];
[self.moreView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.and.left.mas_equalTo(self);
make.height.mas_equalTo(kFunctionViewHeight);
make.top.mas_equalTo(self.mas_bottom);
}];
}
- (void)dealloc {
self.delegate = nil;
_faceView.delegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark -
#pragma mark - Setter Method
- (void)setCachedText:(NSString *)cachedText {
_cachedText = [cachedText copy];
if ([_cachedText isEqualToString:@""]) {
[self updateChatBarConstraintsIfNeededShouldCacheText:NO];
self.allowTextViewContentOffset = YES;
return;
}
if ([_cachedText lcck_isSpace]) {
_cachedText = @"";
return;
}
}
- (UIViewController *)controllerRef {
return (UIViewController *)self.delegate;
}
- (void)setDelegate:(id<LCCKChatBarDelegate>)delegate {
_delegate = delegate;
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (range.location == [textView.text length]) {
self.allowTextViewContentOffset = YES;
} else {
self.allowTextViewContentOffset = NO;
}
if ([text isEqualToString:@"\n"]) {
[self sendTextMessage:textView.text];
return NO;
} else if (text.length == 0){
//构造元素需要使用两个空格来进行缩进,右括号]或者}写在新的一行,并且与调用语法糖那行代码的第一个非空字符对齐:
NSArray *defaultRegulations = @[
//判断删除的文字是否符合表情文字规则
@{
kLCCKBatchDeleteTextPrefix : @"[",
kLCCKBatchDeleteTextSuffix : @"]",
},
//判断删除的文字是否符合提醒群成员的文字规则
@{
kLCCKBatchDeleteTextPrefix : @"@",
kLCCKBatchDeleteTextSuffix : @" ",
},
];
NSArray *additionRegulation;
if ([self.delegate respondsToSelector:@selector(regulationForBatchDeleteText)]) {
additionRegulation = [self.delegate regulationForBatchDeleteText];
}
if (additionRegulation.count > 0) {
defaultRegulations = [defaultRegulations arrayByAddingObjectsFromArray:additionRegulation];
}
for (NSDictionary *regulation in defaultRegulations) {
NSString *prefix = regulation[kLCCKBatchDeleteTextPrefix];
NSString *suffix = regulation[kLCCKBatchDeleteTextSuffix];
if (![self textView:textView shouldChangeTextInRange:range deleteBatchOfTextWithPrefix:prefix suffix:suffix]) {
return NO;
}
}
return YES;
} else if ([text isEqualToString:@"@"]) {
if ([self.delegate respondsToSelector:@selector(didInputAtSign:)]) {
[self.delegate didInputAtSign:self];
}
return YES;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView {
[self textViewDidChange:textView shouldCacheText:YES];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range deleteBatchOfTextWithPrefix:(NSString *)prefix
suffix:(NSString *)suffix {
NSString *substringOfText = [textView.text substringWithRange:range];
if ([substringOfText isEqualToString:suffix]) {
NSUInteger location = range.location;
NSUInteger length = range.length;
NSString *subText;
while (YES) {
if (location == 0) {
return YES;
}
location -- ;
length ++ ;
subText = [textView.text substringWithRange:NSMakeRange(location, length)];
if (([subText hasPrefix:prefix] && [subText hasSuffix:suffix])) {
//这里注意,批量删除的字符串,除了前缀和后缀,中间不能有空格出现
NSString *string = [textView.text substringWithRange:NSMakeRange(location, length-1)];
if (![string lcck_containsString:@" "]) {
break;
}
}
}
textView.text = [textView.text stringByReplacingCharactersInRange:NSMakeRange(location, length) withString:@""];
[textView setSelectedRange:NSMakeRange(location, 0)];
[self textViewDidChange:self.textView];
return NO;
}
return YES;
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
self.faceButton.selected = self.moreButton.selected = self.voiceButton.selected = NO;
[self showFaceView:NO];
[self showMoreView:NO];
[self showVoiceView:NO];
return YES;
}
#pragma mark -
#pragma mark - Private Methods
- (void)updateChatBarConstraintsIfNeeded {
NSString *reason = [NSString stringWithFormat:@"🔴类名与方法名:%@(在第%@行),描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), @"Should update on main thread"];
NSAssert([NSThread mainThread], reason);
BOOL shouldCacheText = NO;
BOOL shouldScrollToBottom = YES;
LCCKFunctionViewShowType functionViewShowType = self.showType;
switch (functionViewShowType) {
case LCCKFunctionViewShowNothing: {
shouldScrollToBottom = NO;
shouldCacheText = YES;
}
break;
case LCCKFunctionViewShowFace:
case LCCKFunctionViewShowMore:
case LCCKFunctionViewShowKeyboard: {
shouldCacheText = YES;
}
break;
case LCCKFunctionViewShowVoice:
shouldCacheText = NO;
break;
}
[self updateChatBarConstraintsIfNeededShouldCacheText:shouldCacheText];
[self chatBarFrameDidChangeShouldScrollToBottom:shouldScrollToBottom];
}
- (void)updateChatBarConstraintsIfNeededShouldCacheText:(BOOL)shouldCacheText {
[self textViewDidChange:self.textView shouldCacheText:shouldCacheText];
}
- (void)updateChatBarKeyBoardConstraints {
[self mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-self.keyboardSize.height);
}];
[UIView animateWithDuration:LCCKAnimateDuration animations:^{
[self layoutIfNeeded];
} completion:nil];
}
#pragma mark - 核心方法
///=============================================================================
/// @name 核心方法
///=============================================================================
/*!
* updateChatBarConstraintsIfNeeded: WhenTextViewHeightDidChanged
* 只要文本修改了就会调用,特殊情况,也会调用:刚刚进入对话追加草稿、键盘类型切换、添加表情信息
*/
- (void)textViewDidChange:(UITextView *)textView
shouldCacheText:(BOOL)shouldCacheText {
if (shouldCacheText) {
self.cachedText = self.textView.text;
}
CGRect textViewFrame = self.textView.frame;
CGSize textSize = [self.textView sizeThatFits:CGSizeMake(CGRectGetWidth(textViewFrame), 1000.0f)];
// from iOS 7, the content size will be accurate only if the scrolling is enabled.
textView.scrollEnabled = (textSize.height > kLCCKChatBarTextViewFrameMinHeight);
// textView 控件的高度在 kLCCKChatBarTextViewFrameMinHeight 和 kLCCKChatBarMaxHeight-offset 之间
CGFloat newTextViewHeight = MAX(kLCCKChatBarTextViewFrameMinHeight, MIN(kLCCKChatBarTextViewFrameMaxHeight, textSize.height));
BOOL textViewHeightChanged = (self.oldTextViewHeight != newTextViewHeight);
if (textViewHeightChanged) {
//FIXME:如果有草稿,且超出了最低高度,会产生约束警告。
self.oldTextViewHeight = newTextViewHeight;
[self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
CGFloat height = newTextViewHeight;
make.height.mas_equalTo(height);
}];
[self chatBarFrameDidChangeShouldScrollToBottom:YES];
}
void(^setContentOffBlock)() = ^() {
if (textView.scrollEnabled && self.allowTextViewContentOffset) {
if (newTextViewHeight == kLCCKChatBarTextViewFrameMaxHeight) {
[textView setContentOffset:CGPointMake(0, textView.contentSize.height - newTextViewHeight) animated:YES];
} else {
[textView setContentOffset:CGPointZero animated:YES];
}
}
};
//FIXME:issue #178
//在输入换行的时候,textView的内容向上偏移,再下次输入后恢复正常,原因是高度变化后,textView更新约束,重新设置了contentOffset;我是在设置contentOffset做了0.01秒的延迟,发现能解决这个问题
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
setContentOffBlock();
});
}
#pragma mark - LCCKChatFaceViewDelegate
- (void)faceViewSendFace:(NSString *)faceName {
if ([faceName isEqualToString:@"[删除]"]) {
[self textView:self.textView shouldChangeTextInRange:NSMakeRange(self.textView.text.length - 1, 1) replacementText:@""];
} else if ([faceName isEqualToString:@"发送"]) {
NSString *text = self.textView.text;
if (!text || text.length == 0) {
return;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendMessage:)]) {
[self.delegate chatBar:self sendMessage:text];
}
self.textView.text = @"";
self.cachedText = @"";
self.showType = LCCKFunctionViewShowFace;
} else {
[self appendString:faceName beginInputing:NO];
}
}
#pragma mark - Public Methods
- (void)close {
//关闭
self.close = YES;
}
- (void)open {
self.close = NO;
}
- (void)endInputing {
if (self.voiceButton.selected) {
return;
}
self.faceButton.selected = self.moreButton.selected = self.voiceButton.selected = NO;
self.showType = LCCKFunctionViewShowNothing;
}
- (void)appendString:(NSString *)string beginInputing:(BOOL)beginInputing {
self.allowTextViewContentOffset = YES;
if (self.textView.text.length > 0 && [string hasPrefix:@"@"] && ![self.textView.text hasSuffix:@" "]) {
self.textView.text = [self.textView.text stringByAppendingString:@" "];
}
NSString *textViewText;
//特殊情况:处于语音按钮显示时,self.textView.text无信息,但self.cachedText有信息
if (self.textView.text.length == 0 && self.cachedText.length > 0) {
textViewText = self.cachedText;
} else {
textViewText = self.textView.text;
}
NSString *appendedString = [textViewText stringByAppendingString:string];
self.cachedText = appendedString;
self.textView.text = appendedString;
if (beginInputing && self.keyboardSize.height == 0) {
[self beginInputing];
} else {
[self updateChatBarConstraintsIfNeeded];
}
}
- (void)appendString:(NSString *)string {
[self appendString:string beginInputing:YES];
}
- (void)beginInputing {
[self.textView becomeFirstResponder];
}
#pragma mark - MP3RecordedDelegate
- (void)averagePowerWithVolume:(CGFloat)volume {
[LCCKRecordAudioHUD changeVolume:volume];
}
- (void)endConvertWithMP3FileName:(NSString *)fileName {
if (fileName) {
[LCCKRecordAudioHUD dismissWithState:LCCKRecoderResultStateSuccess];
[self sendVoiceMessage:fileName seconds:[LCCKRecordAudioHUD seconds]];
} else {
[LCCKRecordAudioHUD dismissWithState:LCCKRecoderResultStateFail];
}
}
- (void)tooShortFailRecord {
[LCCKRecordAudioHUD dismissWithState:LCCKRecoderResultStateShort];
}
#pragma mark - Private Methods
/**
* 开始录音
*/
- (void)startRecordVoice {
[LCCKRecordAudioHUD show];
self.voiceRecordButton.selected = YES;
[self.MP3 startRecord];
}
/**
* 取消录音
*/
- (void)cancelRecordVoice {
[LCCKRecordAudioHUD dismissWithState:LCCKRecoderResultStateCancel];
self.voiceRecordButton.selected = NO;
[self.MP3 cancelRecord];
}
/**
* 录音结束
*/
- (void)confirmRecordVoice {
self.voiceRecordButton.selected = NO;
[self.MP3 stopRecord];
}
/**
* 更新录音显示状态,手指向上滑动后提示松开取消录音
*/
- (void)updateCancelRecordVoice {
[LCCKRecordAudioHUD changeProgressState:LCCKRecordProgressStateOutSide];
[_voiceRecordButton setTitle:@"松开 取消" forState:UIControlStateSelected];
}
/**
* 更新录音状态,手指重新滑动到范围内,提示向上取消录音
*/
- (void)updateContinueRecordVoice {
[LCCKRecordAudioHUD changeProgressState:LCCKRecordProgressStateInSide];
[_voiceRecordButton setTitle:@"松开 结束" forState:UIControlStateSelected];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSString *reason = [NSString stringWithFormat:@"🔴类名与方法名:%@(在第%@行),描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), @"Should update on main thread"];
NSAssert([NSThread mainThread], reason);
if (self.isClosed) {
return;
}
self.keyboardSize = CGSizeZero;
if (_showType == LCCKFunctionViewShowKeyboard) {
_showType = LCCKFunctionViewShowNothing;
}
[self updateChatBarKeyBoardConstraints];
[self updateChatBarConstraintsIfNeeded];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSString *reason = [NSString stringWithFormat:@"🔴类名与方法名:%@(在第%@行),描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), @"Should update on main thread"];
NSAssert([NSThread mainThread], reason);
if (self.isClosed) {
return;
}
CGFloat oldHeight = self.keyboardSize.height;
self.keyboardSize = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
//兼容搜狗输入法:一次键盘事件会通知两次,且键盘高度不一。
if (self.keyboardSize.height != oldHeight) {
_showType = LCCKFunctionViewShowNothing;
}
if (self.keyboardSize.height == 0) {
_showType = LCCKFunctionViewShowNothing;
return;
}
self.allowTextViewContentOffset = YES;
[self updateChatBarKeyBoardConstraints];
self.showType = LCCKFunctionViewShowKeyboard;
}
/**
* lazy load inputBarBackgroundView
*
* @return UIView
*/
- (UIView *)inputBarBackgroundView {
if (_inputBarBackgroundView == nil) {
UIView *inputBarBackgroundView = [[UIView alloc] init];
_inputBarBackgroundView = inputBarBackgroundView;
}
return _inputBarBackgroundView;
}
- (void)setup {
self.close = NO;
self.oldTextViewHeight = kLCCKChatBarTextViewFrameMinHeight;
self.allowTextViewContentOffset = YES;
self.MP3 = [[Mp3Recorder alloc] initWithDelegate:self];
[self faceView];
[self moreView];
[self addSubview:self.inputBarBackgroundView];
[self.inputBarBackgroundView addSubview:self.voiceButton];
[self.inputBarBackgroundView addSubview:self.moreButton];
[self.inputBarBackgroundView addSubview:self.faceButton];
[self.inputBarBackgroundView addSubview:self.textView];
[self.inputBarBackgroundView addSubview:self.voiceRecordButton];
UIImageView *topLine = [[UIImageView alloc] init];
topLine.backgroundColor = kLCCKTopLineBackgroundColor;
[self.inputBarBackgroundView addSubview:topLine];
[topLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.and.right.and.top.equalTo(self.inputBarBackgroundView);
make.height.mas_equalTo(.5f);
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(confirmRecordVoice) name:kLCCKRecordAudioTooLong object:nil];
self.backgroundColor = self.messageInputViewBackgroundColor;
[self setupConstraints];
[LCCKRecordAudioHUD instance];
}
- (void)setShowType:(LCCKFunctionViewShowType)showType {
if (_showType == showType) {
return;
}
_showType = showType;
//显示对应的View
[self showMoreView:showType == LCCKFunctionViewShowMore && self.moreButton.selected];
[self showVoiceView:showType == LCCKFunctionViewShowVoice && self.voiceButton.selected];
[self showFaceView:showType == LCCKFunctionViewShowFace && self.faceButton.selected];
switch (showType) {
case LCCKFunctionViewShowNothing: {
self.textView.text = self.cachedText;
[self.textView resignFirstResponder];
}
break;
case LCCKFunctionViewShowVoice: {
self.cachedText = self.textView.text;
self.textView.text = nil;
[self.textView resignFirstResponder];
}
break;
case LCCKFunctionViewShowMore:
case LCCKFunctionViewShowFace:
self.textView.text = self.cachedText;
[self.textView resignFirstResponder];
break;
case LCCKFunctionViewShowKeyboard:
self.textView.text = self.cachedText;
break;
}
[self updateChatBarConstraintsIfNeeded];
}
- (void)buttonAction:(UIButton *)button {
LCCKFunctionViewShowType showType = button.tag;
//更改对应按钮的状态
if (button == self.faceButton) {
[self.faceButton setSelected:!self.faceButton.selected];
[self.moreButton setSelected:NO];
[self.voiceButton setSelected:NO];
} else if (button == self.moreButton){
[self.faceButton setSelected:NO];
[self.moreButton setSelected:!self.moreButton.selected];
[self.voiceButton setSelected:NO];
} else if (button == self.voiceButton){
[self.faceButton setSelected:NO];
[self.moreButton setSelected:NO];
[self.voiceButton setSelected:!self.voiceButton.selected];
}
if (!button.selected) {
showType = LCCKFunctionViewShowKeyboard;
[self beginInputing];
}
self.showType = showType;
}
- (void)showFaceView:(BOOL)show {
if (show) {
self.faceView.hidden = NO;
[UIView animateWithDuration:LCCKAnimateDuration animations:^{
[self.faceView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.superview.mas_bottom).offset(-kFunctionViewHeight);
}];
[self.faceView layoutIfNeeded];
} completion:nil];
[self.faceView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.inputBarBackgroundView.mas_bottom);
}];
} else if (self.faceView.superview) {
self.faceView.hidden = YES;
[self.faceView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.and.left.mas_equalTo(self);
make.height.mas_equalTo(kFunctionViewHeight);
make.top.mas_equalTo(self.mas_bottom);
}];
[self.faceView layoutIfNeeded];
}
}
/**
* 显示moreView
* @param show 要显示的moreView
*/
- (void)showMoreView:(BOOL)show {
if (show) {
self.moreView.hidden = NO;
[UIView animateWithDuration:LCCKAnimateDuration animations:^{
[self.moreView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.superview.mas_bottom).offset(-kFunctionViewHeight);
}];
[self.moreView layoutIfNeeded];
} completion:nil];
[self.moreView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.inputBarBackgroundView.mas_bottom);
}];
} else if (self.moreView.superview) {
self.moreView.hidden = YES;
[self.moreView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.and.left.mas_equalTo(self);
make.height.mas_equalTo(kFunctionViewHeight);
make.top.mas_equalTo(self.mas_bottom);
}];
[self.moreView layoutIfNeeded];
}
}
- (void)showVoiceView:(BOOL)show {
self.voiceButton.selected = show;
// self.voiceRecordButton.selected = show;
self.voiceRecordButton.hidden = !show;
self.textView.hidden = !self.voiceRecordButton.hidden;
}
/**
* 发送普通的文本信息,通知代理
*
* @param text 发送的文本信息
*/
- (void)sendTextMessage:(NSString *)text{
if (!text || text.length == 0 || [text lcck_isSpace]) {
return;
}
if (self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendMessage:)]) {
[self.delegate chatBar:self sendMessage:text];
}
self.textView.text = @"";
self.cachedText = @"";
self.showType = LCCKFunctionViewShowKeyboard;
}
/**
* 通知代理发送语音信息
*
* @param voiceData 发送的语音信息data
* @param seconds 语音时长
*/
- (void)sendVoiceMessage:(NSString *)voiceFileName seconds:(NSTimeInterval)seconds {
if ((seconds > 0) && self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendVoice:seconds:)]) {
[self.delegate chatBar:self sendVoice:voiceFileName seconds:seconds];
}
}
/**
* 通知代理发送图片信息
*
* @param image 发送的图片
*/
- (void)sendImageMessage:(UIImage *)image {
if (self.delegate && [self.delegate respondsToSelector:@selector(chatBar:sendPictures:)]) {
[self.delegate chatBar:self sendPictures:@[image]];
}
}
- (void)chatBarFrameDidChangeShouldScrollToBottom:(BOOL)shouldScrollToBottom {
NSString *reason = [NSString stringWithFormat:@"🔴类名与方法名:%@(在第%@行),描述:%@", @(__PRETTY_FUNCTION__), @(__LINE__), @"Should update on main thread"];
NSAssert([NSThread mainThread], reason);
if (self.delegate && [self.delegate respondsToSelector:@selector(chatBarFrameDidChange:shouldScrollToBottom:)]) {
[self.delegate chatBarFrameDidChange:self shouldScrollToBottom:shouldScrollToBottom];
}
}
- (UIImage *)imageInBundlePathForImageName:(NSString *)imageName {
UIImage *image = [UIImage lcck_imageNamed:imageName bundleName:@"ChatKeyboard" bundleForClass:[self class]];
return image;
}
#pragma mark - Getters
- (LCCKChatFaceView *)faceView {
if (!_faceView) {
LCCKChatFaceView *faceView = [[LCCKChatFaceView alloc] init];
faceView.delegate = self;
faceView.hidden = YES;
faceView.backgroundColor = self.backgroundColor;
[self addSubview:(_faceView = faceView)];
}
return _faceView;
}
- (LCCKChatMoreView *)moreView {
if (!_moreView) {
LCCKChatMoreView *moreView = [[LCCKChatMoreView alloc] init];
moreView.inputViewRef = self;
moreView.hidden = YES;
[self addSubview:(_moreView = moreView)];
}
return _moreView;
}
- (UITextView *)textView {
if (!_textView) {
_textView = [[UITextView alloc] init];
_textView.font = [UIFont systemFontOfSize:16.0f];
_textView.delegate = self;
_textView.layer.cornerRadius = 4.0f;
_textView.textColor = self.messageInputViewTextFieldTextColor;
_textView.backgroundColor = self.messageInputViewTextFieldBackgroundColor;
_textView.layer.borderColor = [UIColor colorWithRed:204.0/255.0f green:204.0/255.0f blue:204.0/255.0f alpha:1.0f].CGColor;
_textView.returnKeyType = UIReturnKeySend;
_textView.layer.borderWidth = .5f;
_textView.layer.masksToBounds = YES;
_textView.scrollsToTop = NO;
}
return _textView;
}
- (UIButton *)voiceButton {
if (!_voiceButton) {
_voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
_voiceButton.tag = LCCKFunctionViewShowVoice;
[_voiceButton setTitleColor:self.messageInputViewRecordTextColor forState:UIControlStateNormal];
[_voiceButton setTitleColor:self.messageInputViewRecordTextColor forState:UIControlStateHighlighted];
[_voiceButton setBackgroundImage:[self imageInBundlePathForImageName:@"ToolViewInputVoice"] forState:UIControlStateNormal];
[_voiceButton setBackgroundImage:[self imageInBundlePathForImageName:@"ToolViewKeyboard"] forState:UIControlStateSelected];
[_voiceButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[_voiceButton sizeToFit];
}
return _voiceButton;
}
- (UIButton *)voiceRecordButton {
if (!_voiceRecordButton) {
_voiceRecordButton = [UIButton buttonWithType:UIButtonTypeCustom];
_voiceRecordButton.hidden = YES;
_voiceRecordButton.frame = self.textView.bounds;
_voiceRecordButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_voiceRecordButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
UIEdgeInsets edgeInsets = UIEdgeInsetsMake(9, 9, 9, 9);
UIImage *voiceRecordButtonNormalBackgroundImage = [[self imageInBundlePathForImageName:@"VoiceBtn_Black"] resizableImageWithCapInsets:edgeInsets resizingMode:UIImageResizingModeStretch];
UIImage *voiceRecordButtonHighlightedBackgroundImage = [[self imageInBundlePathForImageName:@"VoiceBtn_BlackHL"] resizableImageWithCapInsets:edgeInsets resizingMode:UIImageResizingModeStretch];
[_voiceRecordButton setBackgroundImage:voiceRecordButtonNormalBackgroundImage forState:UIControlStateNormal];
[_voiceRecordButton setBackgroundImage:voiceRecordButtonHighlightedBackgroundImage forState:UIControlStateSelected];
_voiceRecordButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
[_voiceRecordButton setTitle:@"按住 说话" forState:UIControlStateNormal];
[_voiceRecordButton setTitle:@"松开 结束" forState:UIControlStateSelected];
[_voiceRecordButton addTarget:self action:@selector(startRecordVoice) forControlEvents:UIControlEventTouchDown];
[_voiceRecordButton addTarget:self action:@selector(cancelRecordVoice) forControlEvents:UIControlEventTouchUpOutside];
[_voiceRecordButton addTarget:self action:@selector(confirmRecordVoice) forControlEvents:UIControlEventTouchUpInside];
[_voiceRecordButton addTarget:self action:@selector(updateCancelRecordVoice) forControlEvents:UIControlEventTouchDragExit];
[_voiceRecordButton addTarget:self action:@selector(updateContinueRecordVoice) forControlEvents:UIControlEventTouchDragEnter];
}
return _voiceRecordButton;
}
- (UIButton *)moreButton {
if (!_moreButton) {
_moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
_moreButton.tag = LCCKFunctionViewShowMore;
[_moreButton setBackgroundImage:[self imageInBundlePathForImageName:@"TypeSelectorBtn_Black"] forState:UIControlStateNormal];
[_moreButton setBackgroundImage:[self imageInBundlePathForImageName:@"TypeSelectorBtn_Black"] forState:UIControlStateSelected];
[_moreButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[_moreButton sizeToFit];
}
return _moreButton;
}
- (UIButton *)faceButton {
if (!_faceButton) {
_faceButton = [UIButton buttonWithType:UIButtonTypeCustom];
_faceButton.tag = LCCKFunctionViewShowFace;
[_faceButton setBackgroundImage:[self imageInBundlePathForImageName:@"ToolViewEmotion"] forState:UIControlStateNormal];
[_faceButton setBackgroundImage:[self imageInBundlePathForImageName:@"ToolViewKeyboard"] forState:UIControlStateSelected];
[_faceButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[_faceButton sizeToFit];
}
return _faceButton;
}
- (CGFloat)bottomHeight {
if (self.faceView.superview || self.moreView.superview) {
return MAX(self.keyboardSize.height, MAX(self.faceView.frame.size.height, self.moreView.frame.size.height));
} else {
return MAX(self.keyboardSize.height, CGFLOAT_MIN);
}
}
- (UIViewController *)rootViewController {
return [[UIApplication sharedApplication] keyWindow].rootViewController;
}
#pragma mark -
#pragma mark - MessageInputView Customize UI Method
- (UIColor *)messageInputViewBackgroundColor {
if (_messageInputViewBackgroundColor) {
return _messageInputViewBackgroundColor;
}
_messageInputViewBackgroundColor = [[LCCKSettingService sharedInstance] defaultThemeColorForKey:@"MessageInputView-BackgroundColor"];
return _messageInputViewBackgroundColor;
}
- (UIColor *)messageInputViewTextFieldTextColor {
if (_messageInputViewTextFieldTextColor) {
return _messageInputViewTextFieldTextColor;
}
_messageInputViewTextFieldTextColor = [[LCCKSettingService sharedInstance] defaultThemeColorForKey:@"MessageInputView-TextField-TextColor"];
return _messageInputViewTextFieldTextColor;
}
- (UIColor *)messageInputViewTextFieldBackgroundColor {
if (_messageInputViewTextFieldBackgroundColor) {
return _messageInputViewTextFieldBackgroundColor;
}
_messageInputViewTextFieldBackgroundColor = [[LCCKSettingService sharedInstance] defaultThemeColorForKey:@"MessageInputView-TextField-BackgroundColor"];
return _messageInputViewTextFieldBackgroundColor;
}
- (UIColor *)messageInputViewRecordTextColor {
if (_messageInputViewRecordTextColor) {
return _messageInputViewRecordTextColor;
}
_messageInputViewRecordTextColor = [[LCCKSettingService sharedInstance] defaultThemeColorForKey:@"MessageInputView-Record-TextColor"];
return _messageInputViewRecordTextColor;
}
@end