From 08628bcd1d170b36d35e60521596db7aa673220b Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Tue, 3 Feb 2026 13:31:52 +0800 Subject: [PATCH] =?UTF-8?q?aihome=E9=87=8C=E7=BB=9F=E4=B8=80=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E6=B6=88=E6=81=AF=E6=8E=A7=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keyBoard/Class/AiTalk/VC/KBAIHomeVC.m | 164 +++++++------------------- 1 file changed, 42 insertions(+), 122 deletions(-) diff --git a/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m b/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m index de1adfe..37c041b 100644 --- a/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m +++ b/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m @@ -18,10 +18,11 @@ #import "KBUserSessionManager.h" #import "LSTPopView.h" #import "KBAIMessageVC.h" +#import "KBAICommentInputView.h" #import #import -@interface KBAIHomeVC () +@interface KBAIHomeVC () /// 人设列表容器 @property (nonatomic, strong) UICollectionView *collectionView; @@ -37,16 +38,10 @@ @property (nonatomic, strong) UITapGestureRecognizer *dismissKeyboardTap; @property (nonatomic, weak) LSTPopView *chatLimitPopView; -/// 文本输入容器视图(键盘弹起时显示) -@property (nonatomic, strong) UIView *textInputContainerView; -/// 文本输入框 -@property (nonatomic, strong) UITextView *textInputTextView; -/// 发送按钮 -@property (nonatomic, strong) UIButton *sendButton; -/// 占位符标签 -@property (nonatomic, strong) UILabel *placeholderLabel; -/// 文本输入容器底部约束 -@property (nonatomic, strong) MASConstraint *textInputContainerBottomConstraint; +/// 文本输入视图(键盘弹起时显示) +@property (nonatomic, strong) KBAICommentInputView *commentInputView; +/// 文本输入视图底部约束 +@property (nonatomic, strong) MASConstraint *commentInputBottomConstraint; /// 是否处于文本输入模式 @property (nonatomic, assign) BOOL isTextInputMode; @@ -116,8 +111,8 @@ if (!firstResponder) { return NO; } - // 文本输入模式下,textInputTextView 也算 - if (firstResponder == self.textInputTextView) { + // 文本输入模式下,commentInputView 也算 + if ([firstResponder isDescendantOfView:self.commentInputView]) { return YES; } return [firstResponder isDescendantOfView:self.voiceInputBar]; @@ -179,7 +174,7 @@ #pragma mark - 1:控件初始化 - (void)setupUI { - self.voiceInputBarHeight = 80.0; + self.voiceInputBarHeight = 52; self.baseInputBarBottomSpacing = KB_TABBAR_HEIGHT; [self.view addSubview:self.collectionView]; [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) { @@ -217,37 +212,13 @@ /// 设置文本输入视图 - (void)setupTextInputView { - // 文本输入容器(初始隐藏) - [self.view addSubview:self.textInputContainerView]; - [self.textInputContainerView mas_makeConstraints:^(MASConstraintMaker *make) { - make.left.right.equalTo(self.view); - self.textInputContainerBottomConstraint = make.bottom.equalTo(self.view).offset(100); // 初始在屏幕外 - make.height.mas_greaterThanOrEqualTo(50); - }]; - - [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); + // 文本输入视图(初始隐藏) + [self.view addSubview:self.commentInputView]; + [self.commentInputView mas_makeConstraints:^(MASConstraintMaker *make) { + make.left.equalTo(self.view).offset(12); + make.right.equalTo(self.view).offset(-12); + self.commentInputBottomConstraint = make.bottom.equalTo(self.view).offset(100); // 初始在屏幕外 + make.height.mas_equalTo(self.voiceInputBarHeight); }]; } @@ -264,18 +235,17 @@ - (void)showTextInputView { self.isTextInputMode = YES; self.voiceInputBar.hidden = YES; - self.textInputContainerView.hidden = NO; - [self.textInputTextView becomeFirstResponder]; + self.commentInputView.hidden = NO; + [self.commentInputView showKeyboard]; } /// 隐藏文本输入视图 - (void)hideTextInputView { self.isTextInputMode = NO; - [self.textInputTextView resignFirstResponder]; - self.textInputContainerView.hidden = YES; + [self.view endEditing:YES]; + [self.commentInputView clearText]; + self.commentInputView.hidden = YES; self.voiceInputBar.hidden = NO; - self.textInputTextView.text = @""; - self.placeholderLabel.hidden = NO; } #pragma mark - 2:数据加载 @@ -585,11 +555,11 @@ bottomSpacing = keyboardHeight - 5.0; // 文本输入模式:更新文本输入容器位置 if (self.isTextInputMode) { - [self.textInputContainerBottomConstraint setOffset:-keyboardHeight]; + [self.commentInputBottomConstraint setOffset:-keyboardHeight]; } } else { bottomSpacing = self.baseInputBarBottomSpacing; - [self.textInputContainerBottomConstraint setOffset:100]; // 移出屏幕 + [self.commentInputBottomConstraint setOffset:100]; // 移出屏幕 } [self.voiceInputBarBottomConstraint setOffset:-bottomSpacing]; @@ -624,7 +594,7 @@ if ([touch.view isDescendantOfView:self.voiceInputBar]) { return NO; } - if ([touch.view isDescendantOfView:self.textInputContainerView]) { + if ([touch.view isDescendantOfView:self.commentInputView]) { return NO; } return YES; @@ -718,18 +688,6 @@ [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 - (UICollectionView *)collectionView { @@ -763,53 +721,19 @@ return _voiceInputBar; } -- (UIView *)textInputContainerView { - if (!_textInputContainerView) { - _textInputContainerView = [[UIView alloc] init]; - _textInputContainerView.backgroundColor = [UIColor whiteColor]; - _textInputContainerView.hidden = YES; +- (KBAICommentInputView *)commentInputView { + if (!_commentInputView) { + _commentInputView = [[KBAICommentInputView alloc] init]; + _commentInputView.layer.cornerRadius = 26; + _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; -} - -- (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; + return _commentInputView; } #pragma mark - KBChatLimitPopViewDelegate @@ -876,22 +800,18 @@ [self.navigationController pushViewController:vc animated:YES]; } -/// 发送按钮点击 - 直接调用 handleTranscribedText -- (void)sendButtonTapped { - NSString *text = [self.textInputTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - if (text.length == 0) { +/// 文本输入发送 - 直接调用 handleTranscribedText +- (void)handleCommentInputSend:(NSString *)text { + NSString *trimmedText = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; + if (trimmedText.length == 0) { return; } - - // 清空输入框 - self.textInputTextView.text = @""; - self.placeholderLabel.hidden = NO; - + // 隐藏键盘和文本输入框 [self hideTextInputView]; // 直接调用 handleTranscribedText,不走语音录制流程 - [self handleTranscribedText:text]; + [self handleTranscribedText:trimmedText]; } #pragma mark - KBVoiceToTextManagerDelegate