aihome里统一发布消息控件
This commit is contained in:
@@ -18,10 +18,11 @@
|
|||||||
#import "KBUserSessionManager.h"
|
#import "KBUserSessionManager.h"
|
||||||
#import "LSTPopView.h"
|
#import "LSTPopView.h"
|
||||||
#import "KBAIMessageVC.h"
|
#import "KBAIMessageVC.h"
|
||||||
|
#import "KBAICommentInputView.h"
|
||||||
#import <Masonry/Masonry.h>
|
#import <Masonry/Masonry.h>
|
||||||
#import <SDWebImage/SDWebImage.h>
|
#import <SDWebImage/SDWebImage.h>
|
||||||
|
|
||||||
@interface KBAIHomeVC () <UICollectionViewDelegate, UICollectionViewDataSource, KBVoiceToTextManagerDelegate, KBVoiceRecordManagerDelegate, UIGestureRecognizerDelegate, KBChatLimitPopViewDelegate, UITextViewDelegate>
|
@interface KBAIHomeVC () <UICollectionViewDelegate, UICollectionViewDataSource, KBVoiceToTextManagerDelegate, KBVoiceRecordManagerDelegate, UIGestureRecognizerDelegate, KBChatLimitPopViewDelegate>
|
||||||
|
|
||||||
/// 人设列表容器
|
/// 人设列表容器
|
||||||
@property (nonatomic, strong) UICollectionView *collectionView;
|
@property (nonatomic, strong) UICollectionView *collectionView;
|
||||||
@@ -37,16 +38,10 @@
|
|||||||
@property (nonatomic, strong) UITapGestureRecognizer *dismissKeyboardTap;
|
@property (nonatomic, strong) UITapGestureRecognizer *dismissKeyboardTap;
|
||||||
@property (nonatomic, weak) LSTPopView *chatLimitPopView;
|
@property (nonatomic, weak) LSTPopView *chatLimitPopView;
|
||||||
|
|
||||||
/// 文本输入容器视图(键盘弹起时显示)
|
/// 文本输入视图(键盘弹起时显示)
|
||||||
@property (nonatomic, strong) UIView *textInputContainerView;
|
@property (nonatomic, strong) KBAICommentInputView *commentInputView;
|
||||||
/// 文本输入框
|
/// 文本输入视图底部约束
|
||||||
@property (nonatomic, strong) UITextView *textInputTextView;
|
@property (nonatomic, strong) MASConstraint *commentInputBottomConstraint;
|
||||||
/// 发送按钮
|
|
||||||
@property (nonatomic, strong) UIButton *sendButton;
|
|
||||||
/// 占位符标签
|
|
||||||
@property (nonatomic, strong) UILabel *placeholderLabel;
|
|
||||||
/// 文本输入容器底部约束
|
|
||||||
@property (nonatomic, strong) MASConstraint *textInputContainerBottomConstraint;
|
|
||||||
/// 是否处于文本输入模式
|
/// 是否处于文本输入模式
|
||||||
@property (nonatomic, assign) BOOL isTextInputMode;
|
@property (nonatomic, assign) BOOL isTextInputMode;
|
||||||
|
|
||||||
@@ -116,8 +111,8 @@
|
|||||||
if (!firstResponder) {
|
if (!firstResponder) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
// 文本输入模式下,textInputTextView 也算
|
// 文本输入模式下,commentInputView 也算
|
||||||
if (firstResponder == self.textInputTextView) {
|
if ([firstResponder isDescendantOfView:self.commentInputView]) {
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
return [firstResponder isDescendantOfView:self.voiceInputBar];
|
return [firstResponder isDescendantOfView:self.voiceInputBar];
|
||||||
@@ -179,7 +174,7 @@
|
|||||||
#pragma mark - 1:控件初始化
|
#pragma mark - 1:控件初始化
|
||||||
|
|
||||||
- (void)setupUI {
|
- (void)setupUI {
|
||||||
self.voiceInputBarHeight = 80.0;
|
self.voiceInputBarHeight = 52;
|
||||||
self.baseInputBarBottomSpacing = KB_TABBAR_HEIGHT;
|
self.baseInputBarBottomSpacing = KB_TABBAR_HEIGHT;
|
||||||
[self.view addSubview:self.collectionView];
|
[self.view addSubview:self.collectionView];
|
||||||
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
@@ -217,37 +212,13 @@
|
|||||||
|
|
||||||
/// 设置文本输入视图
|
/// 设置文本输入视图
|
||||||
- (void)setupTextInputView {
|
- (void)setupTextInputView {
|
||||||
// 文本输入容器(初始隐藏)
|
// 文本输入视图(初始隐藏)
|
||||||
[self.view addSubview:self.textInputContainerView];
|
[self.view addSubview:self.commentInputView];
|
||||||
[self.textInputContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
|
[self.commentInputView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||||
make.left.right.equalTo(self.view);
|
make.left.equalTo(self.view).offset(12);
|
||||||
self.textInputContainerBottomConstraint = make.bottom.equalTo(self.view).offset(100); // 初始在屏幕外
|
make.right.equalTo(self.view).offset(-12);
|
||||||
make.height.mas_greaterThanOrEqualTo(50);
|
self.commentInputBottomConstraint = make.bottom.equalTo(self.view).offset(100); // 初始在屏幕外
|
||||||
}];
|
make.height.mas_equalTo(self.voiceInputBarHeight);
|
||||||
|
|
||||||
[self.textInputContainerView addSubview:self.textInputTextView];
|
|
||||||
[self.textInputContainerView addSubview:self.sendButton];
|
|
||||||
[self.textInputTextView addSubview:self.placeholderLabel];
|
|
||||||
|
|
||||||
[self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
||||||
make.right.equalTo(self.textInputContainerView).offset(-16);
|
|
||||||
make.bottom.equalTo(self.textInputContainerView).offset(-10);
|
|
||||||
make.width.mas_equalTo(60);
|
|
||||||
make.height.mas_equalTo(36);
|
|
||||||
}];
|
|
||||||
|
|
||||||
[self.textInputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
||||||
make.left.equalTo(self.textInputContainerView).offset(16);
|
|
||||||
make.right.equalTo(self.sendButton.mas_left).offset(-10);
|
|
||||||
make.top.equalTo(self.textInputContainerView).offset(8);
|
|
||||||
make.bottom.equalTo(self.textInputContainerView).offset(-8);
|
|
||||||
make.height.mas_greaterThanOrEqualTo(36);
|
|
||||||
make.height.mas_lessThanOrEqualTo(100);
|
|
||||||
}];
|
|
||||||
|
|
||||||
[self.placeholderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
||||||
make.left.equalTo(self.textInputTextView).offset(15);
|
|
||||||
make.top.equalTo(self.textInputTextView).offset(8);
|
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,18 +235,17 @@
|
|||||||
- (void)showTextInputView {
|
- (void)showTextInputView {
|
||||||
self.isTextInputMode = YES;
|
self.isTextInputMode = YES;
|
||||||
self.voiceInputBar.hidden = YES;
|
self.voiceInputBar.hidden = YES;
|
||||||
self.textInputContainerView.hidden = NO;
|
self.commentInputView.hidden = NO;
|
||||||
[self.textInputTextView becomeFirstResponder];
|
[self.commentInputView showKeyboard];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 隐藏文本输入视图
|
/// 隐藏文本输入视图
|
||||||
- (void)hideTextInputView {
|
- (void)hideTextInputView {
|
||||||
self.isTextInputMode = NO;
|
self.isTextInputMode = NO;
|
||||||
[self.textInputTextView resignFirstResponder];
|
[self.view endEditing:YES];
|
||||||
self.textInputContainerView.hidden = YES;
|
[self.commentInputView clearText];
|
||||||
|
self.commentInputView.hidden = YES;
|
||||||
self.voiceInputBar.hidden = NO;
|
self.voiceInputBar.hidden = NO;
|
||||||
self.textInputTextView.text = @"";
|
|
||||||
self.placeholderLabel.hidden = NO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - 2:数据加载
|
#pragma mark - 2:数据加载
|
||||||
@@ -585,11 +555,11 @@
|
|||||||
bottomSpacing = keyboardHeight - 5.0;
|
bottomSpacing = keyboardHeight - 5.0;
|
||||||
// 文本输入模式:更新文本输入容器位置
|
// 文本输入模式:更新文本输入容器位置
|
||||||
if (self.isTextInputMode) {
|
if (self.isTextInputMode) {
|
||||||
[self.textInputContainerBottomConstraint setOffset:-keyboardHeight];
|
[self.commentInputBottomConstraint setOffset:-keyboardHeight];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bottomSpacing = self.baseInputBarBottomSpacing;
|
bottomSpacing = self.baseInputBarBottomSpacing;
|
||||||
[self.textInputContainerBottomConstraint setOffset:100]; // 移出屏幕
|
[self.commentInputBottomConstraint setOffset:100]; // 移出屏幕
|
||||||
}
|
}
|
||||||
|
|
||||||
[self.voiceInputBarBottomConstraint setOffset:-bottomSpacing];
|
[self.voiceInputBarBottomConstraint setOffset:-bottomSpacing];
|
||||||
@@ -624,7 +594,7 @@
|
|||||||
if ([touch.view isDescendantOfView:self.voiceInputBar]) {
|
if ([touch.view isDescendantOfView:self.voiceInputBar]) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
if ([touch.view isDescendantOfView:self.textInputContainerView]) {
|
if ([touch.view isDescendantOfView:self.commentInputView]) {
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
return YES;
|
return YES;
|
||||||
@@ -718,18 +688,6 @@
|
|||||||
[pop pop];
|
[pop pop];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - UITextViewDelegate
|
|
||||||
|
|
||||||
- (void)textViewDidChange:(UITextView *)textView {
|
|
||||||
self.placeholderLabel.hidden = textView.text.length > 0;
|
|
||||||
// 动态调整高度
|
|
||||||
CGSize size = [textView sizeThatFits:CGSizeMake(textView.frame.size.width, CGFLOAT_MAX)];
|
|
||||||
CGFloat newHeight = MIN(MAX(size.height, 36), 100);
|
|
||||||
[textView mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
||||||
make.height.mas_greaterThanOrEqualTo(newHeight);
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma mark - Lazy Load
|
#pragma mark - Lazy Load
|
||||||
|
|
||||||
- (UICollectionView *)collectionView {
|
- (UICollectionView *)collectionView {
|
||||||
@@ -763,53 +721,19 @@
|
|||||||
return _voiceInputBar;
|
return _voiceInputBar;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (UIView *)textInputContainerView {
|
- (KBAICommentInputView *)commentInputView {
|
||||||
if (!_textInputContainerView) {
|
if (!_commentInputView) {
|
||||||
_textInputContainerView = [[UIView alloc] init];
|
_commentInputView = [[KBAICommentInputView alloc] init];
|
||||||
_textInputContainerView.backgroundColor = [UIColor whiteColor];
|
_commentInputView.layer.cornerRadius = 26;
|
||||||
_textInputContainerView.hidden = YES;
|
_commentInputView.layer.masksToBounds = true;
|
||||||
|
_commentInputView.hidden = YES;
|
||||||
|
_commentInputView.placeholder = KBLocalized(@"send a message");
|
||||||
|
__weak typeof(self) weakSelf = self;
|
||||||
|
_commentInputView.onSend = ^(NSString *text) {
|
||||||
|
[weakSelf handleCommentInputSend:text];
|
||||||
|
};
|
||||||
}
|
}
|
||||||
return _textInputContainerView;
|
return _commentInputView;
|
||||||
}
|
|
||||||
|
|
||||||
- (UITextView *)textInputTextView {
|
|
||||||
if (!_textInputTextView) {
|
|
||||||
_textInputTextView = [[UITextView alloc] init];
|
|
||||||
_textInputTextView.font = [UIFont systemFontOfSize:16];
|
|
||||||
_textInputTextView.textColor = [UIColor blackColor];
|
|
||||||
_textInputTextView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
|
|
||||||
_textInputTextView.layer.cornerRadius = 18;
|
|
||||||
_textInputTextView.layer.masksToBounds = YES;
|
|
||||||
_textInputTextView.textContainerInset = UIEdgeInsetsMake(8, 8, 8, 8);
|
|
||||||
_textInputTextView.delegate = self;
|
|
||||||
_textInputTextView.returnKeyType = UIReturnKeySend;
|
|
||||||
_textInputTextView.enablesReturnKeyAutomatically = YES;
|
|
||||||
}
|
|
||||||
return _textInputTextView;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (UIButton *)sendButton {
|
|
||||||
if (!_sendButton) {
|
|
||||||
_sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
||||||
[_sendButton setTitle:KBLocalized(@"send") forState:UIControlStateNormal];
|
|
||||||
[_sendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
||||||
_sendButton.titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
|
|
||||||
_sendButton.backgroundColor = [UIColor colorWithRed:0.2 green:0.6 blue:1.0 alpha:1.0];
|
|
||||||
_sendButton.layer.cornerRadius = 18;
|
|
||||||
_sendButton.layer.masksToBounds = YES;
|
|
||||||
[_sendButton addTarget:self action:@selector(sendButtonTapped) forControlEvents:UIControlEventTouchUpInside];
|
|
||||||
}
|
|
||||||
return _sendButton;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (UILabel *)placeholderLabel {
|
|
||||||
if (!_placeholderLabel) {
|
|
||||||
_placeholderLabel = [[UILabel alloc] init];
|
|
||||||
_placeholderLabel.text = KBLocalized(@"输入消息...");
|
|
||||||
_placeholderLabel.font = [UIFont systemFontOfSize:16];
|
|
||||||
_placeholderLabel.textColor = [UIColor lightGrayColor];
|
|
||||||
}
|
|
||||||
return _placeholderLabel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - KBChatLimitPopViewDelegate
|
#pragma mark - KBChatLimitPopViewDelegate
|
||||||
@@ -876,22 +800,18 @@
|
|||||||
[self.navigationController pushViewController:vc animated:YES];
|
[self.navigationController pushViewController:vc animated:YES];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 发送按钮点击 - 直接调用 handleTranscribedText
|
/// 文本输入发送 - 直接调用 handleTranscribedText
|
||||||
- (void)sendButtonTapped {
|
- (void)handleCommentInputSend:(NSString *)text {
|
||||||
NSString *text = [self.textInputTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
NSString *trimmedText = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
|
||||||
if (text.length == 0) {
|
if (trimmedText.length == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 清空输入框
|
|
||||||
self.textInputTextView.text = @"";
|
|
||||||
self.placeholderLabel.hidden = NO;
|
|
||||||
|
|
||||||
// 隐藏键盘和文本输入框
|
// 隐藏键盘和文本输入框
|
||||||
[self hideTextInputView];
|
[self hideTextInputView];
|
||||||
|
|
||||||
// 直接调用 handleTranscribedText,不走语音录制流程
|
// 直接调用 handleTranscribedText,不走语音录制流程
|
||||||
[self handleTranscribedText:text];
|
[self handleTranscribedText:trimmedText];
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - KBVoiceToTextManagerDelegate
|
#pragma mark - KBVoiceToTextManagerDelegate
|
||||||
|
|||||||
Reference in New Issue
Block a user