aihome里统一发布消息控件

This commit is contained in:
2026-02-03 13:31:52 +08:00
parent 19cb29616f
commit 08628bcd1d

View File

@@ -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