Files
keyboard/keyBoard/Class/AiTalk/V/KBVoiceInputBar.m

550 lines
19 KiB
Mathematica
Raw Normal View History

2026-01-26 18:17:02 +08:00
//
// KBVoiceInputBar.m
// keyBoard
//
// Created by Kiro on 2026/1/26.
//
#import "KBVoiceInputBar.h"
#import "KBAiRecordButton.h"
2026-02-02 19:07:00 +08:00
#import "KBAiWaveformView.h"
2026-01-26 18:17:02 +08:00
#import <Masonry/Masonry.h>
@interface KBVoiceInputBar () <KBAiRecordButtonDelegate>
///
@property (nonatomic, strong) UILabel *statusLabel;
///
@property (nonatomic, strong) KBAiRecordButton *recordButton;
2026-01-27 16:28:17 +08:00
///
@property (nonatomic, strong) UIView *inputContainer;
///
@property (nonatomic, strong) UIView *textInputView;
@property (nonatomic, strong) UIButton *textCenterButton;
///
@property (nonatomic, strong) UIView *voiceInputView;
@property (nonatomic, strong) UILabel *voiceCenterLabel;
/// /
@property (nonatomic, strong) UIButton *toggleIconButton;
///
@property (nonatomic, strong) UIView *recordingView;
@property (nonatomic, strong) UIImageView *recordingCenterIconView;
2026-02-02 19:07:00 +08:00
@property (nonatomic, strong) KBAiWaveformView *leftWaveformView;
@property (nonatomic, strong) KBAiWaveformView *rightWaveformView;
2026-01-27 16:28:17 +08:00
///
@property (nonatomic, strong) UIView *cancelView;
@property (nonatomic, strong) UILabel *cancelLabel;
///
@property (nonatomic, strong) UITextField *hiddenTextField;
2026-01-26 18:17:02 +08:00
///
@property (nonatomic, assign) BOOL isRecording;
@end
@implementation KBVoiceInputBar
#pragma mark - Lifecycle
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self setupUI];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self = [super initWithCoder:coder]) {
[self setupUI];
}
return self;
}
#pragma mark - 1
- (void)setupUI {
self.backgroundColor = [UIColor clearColor];
self.enabled = YES;
self.isRecording = NO;
2026-01-27 16:28:17 +08:00
self.inputMode = KBVoiceInputBarModeVoice;
self.inputState = KBVoiceInputBarStateText;
2026-01-26 18:17:02 +08:00
//
[self addSubview:self.statusLabel];
[self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(16);
make.left.equalTo(self).offset(20);
make.right.equalTo(self).offset(-20);
make.height.mas_equalTo(20);
}];
2026-01-27 16:28:17 +08:00
//
[self addSubview:self.inputContainer];
[self.inputContainer mas_makeConstraints:^(MASConstraintMaker *make) {
2026-01-28 17:21:19 +08:00
// make.top.equalTo(self.statusLabel.mas_bottom).offset(12);
2026-01-26 18:17:02 +08:00
make.left.equalTo(self).offset(20);
make.right.equalTo(self).offset(-20);
make.height.mas_equalTo(50);
2026-01-28 17:21:19 +08:00
make.bottom.lessThanOrEqualTo(self).offset(-10);
2026-01-26 18:17:02 +08:00
}];
2026-01-27 16:28:17 +08:00
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleVoiceLongPress:)];
longPress.minimumPressDuration = 0.05;
longPress.cancelsTouchesInView = NO;
[self.inputContainer addGestureRecognizer:longPress];
2026-01-26 18:17:02 +08:00
2026-01-27 16:28:17 +08:00
//
[self.inputContainer addSubview:self.textInputView];
[self.textInputView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.inputContainer);
}];
[self.inputContainer addSubview:self.toggleIconButton];
[self.toggleIconButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.textInputView).offset(16);
make.centerY.equalTo(self.textInputView);
make.width.height.mas_equalTo(24);
}];
[self.textInputView addSubview:self.textCenterButton];
[self.textCenterButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.toggleIconButton.mas_right).offset(12);
make.right.equalTo(self.textInputView).offset(-16);
make.centerY.equalTo(self.textInputView);
make.height.mas_equalTo(30);
}];
//
[self.inputContainer addSubview:self.voiceInputView];
[self.voiceInputView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.inputContainer);
}];
[self.voiceInputView addSubview:self.voiceCenterLabel];
[self.voiceCenterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.toggleIconButton.mas_right).offset(12);
make.right.equalTo(self.voiceInputView).offset(-16);
make.centerY.equalTo(self.voiceInputView);
}];
//
[self.inputContainer addSubview:self.recordingView];
[self.recordingView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.inputContainer);
}];
[self.recordingView addSubview:self.recordingCenterIconView];
[self.recordingCenterIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.recordingView);
make.width.height.mas_equalTo(36);
}];
2026-02-02 19:07:00 +08:00
[self.recordingView addSubview:self.leftWaveformView];
[self.recordingView addSubview:self.rightWaveformView];
[self.leftWaveformView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.recordingCenterIconView);
make.right.equalTo(self.recordingCenterIconView.mas_left).offset(-16);
make.width.mas_equalTo(84);
make.height.mas_equalTo(34);
}];
[self.rightWaveformView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.recordingCenterIconView);
make.left.equalTo(self.recordingCenterIconView.mas_right).offset(16);
make.width.mas_equalTo(84);
make.height.mas_equalTo(34);
}];
2026-01-27 16:28:17 +08:00
//
[self.inputContainer addSubview:self.cancelView];
[self.cancelView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.inputContainer);
}];
[self.cancelView addSubview:self.cancelLabel];
[self.cancelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.cancelView);
}];
//
[self addSubview:self.hiddenTextField];
[self.hiddenTextField mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.height.mas_equalTo(1);
make.left.top.equalTo(self);
}];
//
self.statusLabel.hidden = YES;
//
self.inputState = KBVoiceInputBarStateText;
2026-01-26 18:17:02 +08:00
}
#pragma mark - Setter
- (void)setStatusText:(NSString *)statusText {
_statusText = [statusText copy];
self.statusLabel.text = statusText;
2026-01-27 16:28:17 +08:00
[self updateCenterTextIfNeeded];
2026-01-26 18:17:02 +08:00
}
- (void)setEnabled:(BOOL)enabled {
_enabled = enabled;
self.recordButton.userInteractionEnabled = enabled;
self.recordButton.alpha = enabled ? 1.0 : 0.5;
2026-01-27 16:28:17 +08:00
self.inputContainer.userInteractionEnabled = enabled;
self.inputContainer.alpha = enabled ? 1.0 : 0.5;
2026-01-26 18:17:02 +08:00
}
- (void)setRecording:(BOOL)recording {
_isRecording = recording;
self.recordButton.state = recording ? KBAiRecordButtonStateRecording : KBAiRecordButtonStateNormal;
2026-01-27 16:28:17 +08:00
if (recording) {
self.inputState = KBVoiceInputBarStateRecording;
} else if (self.inputState == KBVoiceInputBarStateRecording) {
self.inputState = KBVoiceInputBarStateVoice;
}
}
- (void)setInputMode:(KBVoiceInputBarMode)inputMode {
_inputMode = inputMode;
if (inputMode == KBVoiceInputBarModeText) {
[self.toggleIconButton setImage:[UIImage imageNamed:@"ai_maikefeng_icon"]
forState:UIControlStateNormal];
} else {
[self.toggleIconButton setImage:[UIImage imageNamed:@"ai_jianpan_icon"]
forState:UIControlStateNormal];
}
}
- (void)setInputState:(KBVoiceInputBarState)inputState {
_inputState = inputState;
self.textInputView.hidden = (inputState != KBVoiceInputBarStateText);
self.voiceInputView.hidden = (inputState != KBVoiceInputBarStateVoice);
self.recordingView.hidden = (inputState != KBVoiceInputBarStateRecording);
self.cancelView.hidden = (inputState != KBVoiceInputBarStateCancel);
self.toggleIconButton.hidden = (inputState == KBVoiceInputBarStateRecording ||
inputState == KBVoiceInputBarStateCancel);
if (inputState == KBVoiceInputBarStateText) {
self.inputMode = KBVoiceInputBarModeText;
} else if (inputState == KBVoiceInputBarStateVoice) {
self.inputMode = KBVoiceInputBarModeVoice;
}
if (!self.toggleIconButton.hidden) {
[self.inputContainer bringSubviewToFront:self.toggleIconButton];
}
2026-02-02 19:07:00 +08:00
if (inputState == KBVoiceInputBarStateRecording) {
[self startRecordingWaveAnimationIfNeeded];
} else {
[self stopRecordingWaveAnimation];
}
2026-01-27 16:28:17 +08:00
[self updateCenterTextIfNeeded];
2026-01-26 18:17:02 +08:00
}
#pragma mark - Public Methods
- (void)updateVolumeRMS:(float)rms {
[self.recordButton updateVolumeRMS:rms];
2026-02-02 19:07:00 +08:00
if (self.inputState == KBVoiceInputBarStateRecording) {
CGFloat safeRMS = MAX(rms, 0.6f);
[self.leftWaveformView updateWithRMS:safeRMS];
[self.rightWaveformView updateWithRMS:safeRMS];
}
2026-01-26 18:17:02 +08:00
}
#pragma mark - KBAiRecordButtonDelegate
- (void)recordButtonDidBeginPress:(KBAiRecordButton *)button {
if (!self.enabled) {
return;
}
self.isRecording = YES;
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidBeginRecording:)]) {
[self.delegate voiceInputBarDidBeginRecording:self];
}
}
- (void)recordButtonDidEndPress:(KBAiRecordButton *)button {
self.isRecording = NO;
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidEndRecording:)]) {
[self.delegate voiceInputBarDidEndRecording:self];
}
}
- (void)recordButtonDidCancelPress:(KBAiRecordButton *)button {
self.isRecording = NO;
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidCancelRecording:)]) {
[self.delegate voiceInputBarDidCancelRecording:self];
}
}
#pragma mark - Lazy Load
- (UILabel *)statusLabel {
if (!_statusLabel) {
_statusLabel = [[UILabel alloc] init];
_statusLabel.text = @"按住按钮开始对话";
_statusLabel.font = [UIFont systemFontOfSize:14];
_statusLabel.textColor = [UIColor secondaryLabelColor];
_statusLabel.textAlignment = NSTextAlignmentCenter;
}
return _statusLabel;
}
- (KBAiRecordButton *)recordButton {
if (!_recordButton) {
_recordButton = [[KBAiRecordButton alloc] init];
_recordButton.delegate = self;
_recordButton.normalTitle = @"按住说话";
_recordButton.recordingTitle = @"松开结束";
2026-01-27 16:28:17 +08:00
_recordButton.normalIconImage = [UIImage imageNamed:@"ai_jianpan_icon"];
_recordButton.recordingIconImage = [UIImage imageNamed:@"ai_luyining_icon"];
_recordButton.hidden = YES;
2026-01-26 18:17:02 +08:00
}
return _recordButton;
}
2026-01-27 16:28:17 +08:00
- (UIView *)inputContainer {
if (!_inputContainer) {
_inputContainer = [[UIView alloc] init];
_inputContainer.clipsToBounds = YES;
_inputContainer.layer.cornerRadius = 25;
_inputContainer.backgroundColor = [UIColor clearColor];
}
return _inputContainer;
}
- (UIView *)textInputView {
if (!_textInputView) {
_textInputView = [[UIView alloc] init];
_textInputView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
}
return _textInputView;
}
- (UIButton *)textCenterButton {
if (!_textCenterButton) {
_textCenterButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_textCenterButton setTitle:@"发送一个消息给她" forState:UIControlStateNormal];
_textCenterButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
[_textCenterButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_textCenterButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[_textCenterButton addTarget:self
action:@selector(handleTextCenterTap)
forControlEvents:UIControlEventTouchUpInside];
}
return _textCenterButton;
}
- (UIView *)voiceInputView {
if (!_voiceInputView) {
_voiceInputView = [[UIView alloc] init];
_voiceInputView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];
}
return _voiceInputView;
}
- (UILabel *)voiceCenterLabel {
if (!_voiceCenterLabel) {
_voiceCenterLabel = [[UILabel alloc] init];
_voiceCenterLabel.text = @"按住说话";
_voiceCenterLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
_voiceCenterLabel.textColor = [UIColor whiteColor];
_voiceCenterLabel.textAlignment = NSTextAlignmentCenter;
}
return _voiceCenterLabel;
}
- (UIButton *)toggleIconButton {
if (!_toggleIconButton) {
_toggleIconButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_toggleIconButton setImage:[UIImage imageNamed:@"ai_maikefeng_icon"]
forState:UIControlStateNormal];
[_toggleIconButton addTarget:self
action:@selector(handleToggleIconTap)
forControlEvents:UIControlEventTouchUpInside];
_toggleIconButton.exclusiveTouch = YES;
}
return _toggleIconButton;
}
- (UIView *)recordingView {
if (!_recordingView) {
_recordingView = [[UIView alloc] init];
_recordingView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
}
return _recordingView;
}
- (UIImageView *)recordingCenterIconView {
if (!_recordingCenterIconView) {
_recordingCenterIconView = [[UIImageView alloc] init];
_recordingCenterIconView.image = [UIImage imageNamed:@"ai_luyining_icon"];
_recordingCenterIconView.contentMode = UIViewContentModeScaleAspectFit;
}
return _recordingCenterIconView;
}
2026-02-02 19:07:00 +08:00
- (KBAiWaveformView *)leftWaveformView {
if (!_leftWaveformView) {
_leftWaveformView = [[KBAiWaveformView alloc] init];
_leftWaveformView.waveColor = [UIColor whiteColor];
_leftWaveformView.barCount = 7;
_leftWaveformView.barWidth = 3;
_leftWaveformView.barSpacing = 6;
_leftWaveformView.barHeightPattern = @[@0.35, @0.85, @0.5, @0.35, @0.9, @0.55, @0.35];
}
return _leftWaveformView;
}
- (KBAiWaveformView *)rightWaveformView {
if (!_rightWaveformView) {
_rightWaveformView = [[KBAiWaveformView alloc] init];
_rightWaveformView.waveColor = [UIColor whiteColor];
_rightWaveformView.barCount = 7;
_rightWaveformView.barWidth = 3;
_rightWaveformView.barSpacing = 6;
_rightWaveformView.barHeightPattern = @[@0.35, @0.85, @0.5, @0.35, @0.9, @0.55, @0.35];
}
return _rightWaveformView;
}
2026-01-27 16:28:17 +08:00
- (UIView *)cancelView {
if (!_cancelView) {
_cancelView = [[UIView alloc] init];
_cancelView.backgroundColor = [UIColor colorWithRed:0.75 green:0.3 blue:0.3 alpha:1.0];
}
return _cancelView;
}
- (UILabel *)cancelLabel {
if (!_cancelLabel) {
_cancelLabel = [[UILabel alloc] init];
_cancelLabel.text = @"Release To Cancel";
_cancelLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
_cancelLabel.textColor = [UIColor whiteColor];
}
return _cancelLabel;
}
- (UITextField *)hiddenTextField {
if (!_hiddenTextField) {
_hiddenTextField = [[UITextField alloc] init];
_hiddenTextField.hidden = YES;
}
return _hiddenTextField;
}
#pragma mark - Actions
- (void)handleToggleIconTap {
if (self.inputState == KBVoiceInputBarStateText) {
self.inputState = KBVoiceInputBarStateVoice;
[self endEditing:YES];
} else {
self.inputState = KBVoiceInputBarStateText;
}
}
- (void)handleTextCenterTap {
2026-01-29 17:56:53 +08:00
// VC
if (self.onTextSend) {
self.onTextSend(nil);
}
2026-01-27 16:28:17 +08:00
}
- (void)handleVoiceLongPress:(UILongPressGestureRecognizer *)gesture {
if (self.inputState != KBVoiceInputBarStateVoice &&
self.inputState != KBVoiceInputBarStateRecording &&
self.inputState != KBVoiceInputBarStateCancel) {
return;
}
CGPoint location = [gesture locationInView:self.inputContainer];
BOOL isInside = CGRectContainsPoint(self.inputContainer.bounds, location);
CGPoint iconPoint = [gesture locationInView:self.toggleIconButton];
BOOL isOnToggleIcon = CGRectContainsPoint(self.toggleIconButton.bounds, iconPoint);
if (isOnToggleIcon) {
return;
}
switch (gesture.state) {
case UIGestureRecognizerStateBegan: {
if (self.inputState != KBVoiceInputBarStateVoice) {
return;
}
self.inputState = KBVoiceInputBarStateRecording;
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidBeginRecording:)]) {
[self.delegate voiceInputBarDidBeginRecording:self];
}
} break;
case UIGestureRecognizerStateChanged: {
if (isInside) {
self.inputState = KBVoiceInputBarStateRecording;
} else {
self.inputState = KBVoiceInputBarStateCancel;
}
} break;
case UIGestureRecognizerStateEnded: {
if (isInside) {
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidEndRecording:)]) {
[self.delegate voiceInputBarDidEndRecording:self];
}
} else {
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidCancelRecording:)]) {
[self.delegate voiceInputBarDidCancelRecording:self];
}
}
self.inputState = KBVoiceInputBarStateVoice;
} break;
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed: {
if ([self.delegate respondsToSelector:@selector(voiceInputBarDidCancelRecording:)]) {
[self.delegate voiceInputBarDidCancelRecording:self];
}
self.inputState = KBVoiceInputBarStateVoice;
} break;
default:
break;
}
}
- (void)updateCenterTextIfNeeded {
if (self.inputState == KBVoiceInputBarStateText) {
[self.textCenterButton setTitle:@"发送一个消息给她" forState:UIControlStateNormal];
} else if (self.inputState == KBVoiceInputBarStateVoice) {
self.voiceCenterLabel.text = @"按住说话";
}
}
2026-02-02 19:07:00 +08:00
#pragma mark - Recording Wave
- (void)startRecordingWaveAnimationIfNeeded {
[self.leftWaveformView startIdleAnimation];
[self.rightWaveformView startIdleAnimation];
[self.leftWaveformView updateWithRMS:0.7f];
[self.rightWaveformView updateWithRMS:0.7f];
}
- (void)stopRecordingWaveAnimation {
[self.leftWaveformView stopAnimation];
[self.rightWaveformView stopAnimation];
[self.leftWaveformView reset];
[self.rightWaveformView reset];
}
2026-01-26 18:17:02 +08:00
@end