This commit is contained in:
2026-01-27 16:28:17 +08:00
parent ce889e1ed0
commit 2b749cd2b0
26 changed files with 1092 additions and 128 deletions

View File

@@ -47,6 +47,12 @@ typedef NS_ENUM(NSInteger, KBAiRecordButtonState) {
/// 主色调
@property(nonatomic, strong) UIColor *tintColor;
/// 正常状态左侧图标
@property(nonatomic, strong, nullable) UIImage *normalIconImage;
/// 录音中状态图标(居中显示)
@property(nonatomic, strong, nullable) UIImage *recordingIconImage;
/// 更新音量(用于波形动画)
/// @param rms 音量 RMS 值 (0.0 - 1.0)
- (void)updateVolumeRMS:(float)rms;

View File

@@ -14,6 +14,7 @@
@property(nonatomic, strong) UILabel *titleLabel;
@property(nonatomic, strong) KBAiWaveformView *waveformView;
@property(nonatomic, strong) UIImageView *micIconView;
@property(nonatomic, strong) UIImageView *recordingIconView;
@property(nonatomic, assign) BOOL isPressing;
@end
@@ -50,7 +51,7 @@
self.backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.backgroundView];
//
//
self.micIconView = [[UIImageView alloc] init];
self.micIconView.image = [UIImage systemImageNamed:@"mic.fill"];
self.micIconView.tintColor = self.tintColor;
@@ -58,6 +59,13 @@
self.micIconView.translatesAutoresizingMaskIntoConstraints = NO;
[self.backgroundView addSubview:self.micIconView];
//
self.recordingIconView = [[UIImageView alloc] init];
self.recordingIconView.contentMode = UIViewContentModeScaleAspectFit;
self.recordingIconView.translatesAutoresizingMaskIntoConstraints = NO;
self.recordingIconView.hidden = YES;
[self.backgroundView addSubview:self.recordingIconView];
//
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.text = self.normalTitle;
@@ -104,6 +112,13 @@
constraintEqualToAnchor:self.backgroundView.centerYAnchor],
[self.waveformView.widthAnchor constraintEqualToConstant:60],
[self.waveformView.heightAnchor constraintEqualToConstant:30],
[self.recordingIconView.centerXAnchor
constraintEqualToAnchor:self.backgroundView.centerXAnchor],
[self.recordingIconView.centerYAnchor
constraintEqualToAnchor:self.backgroundView.centerYAnchor],
[self.recordingIconView.widthAnchor constraintEqualToConstant:36],
[self.recordingIconView.heightAnchor constraintEqualToConstant:36],
]];
//
@@ -131,6 +146,18 @@
self.waveformView.waveColor = tintColor;
}
- (void)setNormalIconImage:(UIImage *)normalIconImage {
_normalIconImage = normalIconImage;
if (normalIconImage) {
self.micIconView.image = normalIconImage;
}
}
- (void)setRecordingIconImage:(UIImage *)recordingIconImage {
_recordingIconImage = recordingIconImage;
self.recordingIconView.image = recordingIconImage;
}
#pragma mark - Public Methods
- (void)updateVolumeRMS:(float)rms {
@@ -144,18 +171,21 @@
case KBAiRecordButtonStateNormal:
self.titleLabel.text = self.normalTitle;
self.backgroundView.backgroundColor = [UIColor systemGray6Color];
self.micIconView.alpha = 1;
self.micIconView.hidden = NO;
self.titleLabel.hidden = NO;
self.recordingIconView.hidden = YES;
self.waveformView.alpha = 0;
[self.waveformView stopAnimation];
break;
case KBAiRecordButtonStateRecording:
self.titleLabel.text = self.recordingTitle;
self.backgroundView.backgroundColor =
[self.tintColor colorWithAlphaComponent:0.15];
self.micIconView.alpha = 1;
self.waveformView.alpha = 1;
[self.waveformView startIdleAnimation];
self.backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];
self.micIconView.hidden = YES;
self.titleLabel.hidden = YES;
self.recordingIconView.hidden = NO;
self.waveformView.alpha = 0;
[self.waveformView stopAnimation];
break;
case KBAiRecordButtonStateDisabled:

View File

@@ -0,0 +1,28 @@
//
// KBChatLimitPopView.h
// keyBoard
//
// Created by Codex on 2026/1/27.
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@class KBChatLimitPopView;
@protocol KBChatLimitPopViewDelegate <NSObject>
@optional
- (void)chatLimitPopViewDidTapCancel:(KBChatLimitPopView *)view;
- (void)chatLimitPopViewDidTapRecharge:(KBChatLimitPopView *)view;
@end
/// 聊天次数用尽提示弹窗内容视图(配合 LSTPopView 使用)
@interface KBChatLimitPopView : UIView
@property (nonatomic, weak) id<KBChatLimitPopViewDelegate> delegate;
@property (nonatomic, copy) NSString *message;
@end
NS_ASSUME_NONNULL_END

View File

@@ -0,0 +1,157 @@
//
// KBChatLimitPopView.m
// keyBoard
//
// Created by Codex on 2026/1/27.
//
#import "KBChatLimitPopView.h"
#import <Masonry/Masonry.h>
@interface KBChatLimitPopView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *rechargeButton;
@property (nonatomic, strong) UIView *buttonDivider;
@end
@implementation KBChatLimitPopView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor whiteColor];
self.layer.cornerRadius = 16.0;
self.layer.masksToBounds = YES;
[self setupUI];
}
return self;
}
#pragma mark - UI
- (void)setupUI {
[self addSubview:self.titleLabel];
[self addSubview:self.messageLabel];
[self addSubview:self.buttonDivider];
[self addSubview:self.cancelButton];
[self addSubview:self.rechargeButton];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self).offset(20);
make.left.equalTo(self).offset(20);
make.right.equalTo(self).offset(-20);
}];
[self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
make.left.equalTo(self).offset(20);
make.right.equalTo(self).offset(-20);
}];
[self.buttonDivider mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.height.mas_equalTo(1);
make.top.greaterThanOrEqualTo(self.messageLabel.mas_bottom).offset(16);
make.bottom.equalTo(self).offset(-48);
}];
[self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.equalTo(self);
make.top.equalTo(self.buttonDivider.mas_bottom);
make.right.equalTo(self.mas_centerX);
}];
[self.rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.bottom.equalTo(self);
make.top.equalTo(self.buttonDivider.mas_bottom);
make.left.equalTo(self.mas_centerX);
}];
UIView *verticalLine = [[UIView alloc] init];
verticalLine.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
[self addSubview:verticalLine];
[verticalLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self);
make.top.equalTo(self.buttonDivider.mas_bottom);
make.bottom.equalTo(self);
make.width.mas_equalTo(1);
}];
}
#pragma mark - Actions
- (void)onTapCancel {
if ([self.delegate respondsToSelector:@selector(chatLimitPopViewDidTapCancel:)]) {
[self.delegate chatLimitPopViewDidTapCancel:self];
}
}
- (void)onTapRecharge {
if ([self.delegate respondsToSelector:@selector(chatLimitPopViewDidTapRecharge:)]) {
[self.delegate chatLimitPopViewDidTapRecharge:self];
}
}
#pragma mark - Setter
- (void)setMessage:(NSString *)message {
_message = [message copy];
self.messageLabel.text = _message.length > 0 ? _message : @"";
}
#pragma mark - Lazy
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[UILabel alloc] init];
_titleLabel.text = KBLocalized(@"提示");
_titleLabel.font = [UIFont boldSystemFontOfSize:18];
_titleLabel.textColor = [UIColor blackColor];
_titleLabel.textAlignment = NSTextAlignmentCenter;
}
return _titleLabel;
}
- (UILabel *)messageLabel {
if (!_messageLabel) {
_messageLabel = [[UILabel alloc] init];
_messageLabel.font = [UIFont systemFontOfSize:14];
_messageLabel.textColor = [UIColor colorWithWhite:0.2 alpha:1.0];
_messageLabel.textAlignment = NSTextAlignmentCenter;
_messageLabel.numberOfLines = 0;
}
return _messageLabel;
}
- (UIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_cancelButton setTitle:KBLocalized(@"取消") forState:UIControlStateNormal];
_cancelButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
[_cancelButton setTitleColor:[UIColor colorWithWhite:0.2 alpha:1.0] forState:UIControlStateNormal];
[_cancelButton addTarget:self action:@selector(onTapCancel) forControlEvents:UIControlEventTouchUpInside];
}
return _cancelButton;
}
- (UIButton *)rechargeButton {
if (!_rechargeButton) {
_rechargeButton = [UIButton buttonWithType:UIButtonTypeSystem];
[_rechargeButton setTitle:KBLocalized(@"去充值") forState:UIControlStateNormal];
_rechargeButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
[_rechargeButton setTitleColor:[UIColor colorWithRed:0.28 green:0.45 blue:0.94 alpha:1.0] forState:UIControlStateNormal];
[_rechargeButton addTarget:self action:@selector(onTapRecharge) forControlEvents:UIControlEventTouchUpInside];
}
return _rechargeButton;
}
- (UIView *)buttonDivider {
if (!_buttonDivider) {
_buttonDivider = [[UIView alloc] init];
_buttonDivider.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
}
return _buttonDivider;
}
@end

View File

@@ -57,6 +57,9 @@ NS_ASSUME_NONNULL_BEGIN
/// 重置无更多数据状态
- (void)resetNoMoreData;
/// 更新底部内容 inset用于避开输入栏/键盘)
- (void)updateContentBottomInset:(CGFloat)bottomInset;
/// 添加自定义消息(可用于历史消息或打字机)
- (void)addMessage:(KBAiChatMessage *)message
autoScroll:(BOOL)autoScroll;

View File

@@ -30,6 +30,7 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
@property (nonatomic, strong) NSIndexPath *playingCellIndexPath;
@property (nonatomic, strong) AiVM *aiVM;
@property (nonatomic, assign) BOOL hasMoreData;
@property (nonatomic, assign) CGFloat contentBottomInset;
@end
@@ -78,11 +79,12 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
//
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.edges.equalTo(self);
make.top.left.right.equalTo(self);
make.bottom.equalTo(self).offset(-KB_TABBAR_HEIGHT - 40 - 10);
make.edges.equalTo(self);
}];
self.contentBottomInset = KB_TABBAR_HEIGHT + 40 + 10;
[self updateContentBottomInset:self.contentBottomInset];
__weak typeof(self) weakSelf = self;
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
__strong typeof(weakSelf) strongSelf = weakSelf;
@@ -207,6 +209,14 @@ static const NSTimeInterval kTimestampInterval = 5 * 60; // 5 分钟
[self updateFooterVisibility];
}
- (void)updateContentBottomInset:(CGFloat)bottomInset {
self.contentBottomInset = bottomInset;
UIEdgeInsets insets = self.tableView.contentInset;
insets.bottom = bottomInset;
self.tableView.contentInset = insets;
self.tableView.scrollIndicatorInsets = insets;
}
- (void)addMessage:(KBAiChatMessage *)message
autoScroll:(BOOL)autoScroll {
if (!message) {

View File

@@ -26,6 +26,9 @@ NS_ASSUME_NONNULL_BEGIN
- (void)appendAssistantMessage:(NSString *)text
audioId:(nullable NSString *)audioId;
/// 更新聊天列表底部 inset
- (void)updateChatViewBottomInset:(CGFloat)bottomInset;
@end
NS_ASSUME_NONNULL_END

View File

@@ -288,6 +288,10 @@
[self.chatView addMessage:message autoScroll:YES];
}
- (void)updateChatViewBottomInset:(CGFloat)bottomInset {
[self.chatView updateContentBottomInset:bottomInset];
}
#pragma mark - KBChatTableViewDelegate
- (void)chatTableViewDidScroll:(KBChatTableView *)chatView

View File

@@ -27,6 +27,18 @@ NS_ASSUME_NONNULL_BEGIN
@end
typedef NS_ENUM(NSInteger, KBVoiceInputBarMode) {
KBVoiceInputBarModeText,
KBVoiceInputBarModeVoice
};
typedef NS_ENUM(NSInteger, KBVoiceInputBarState) {
KBVoiceInputBarStateText,
KBVoiceInputBarStateVoice,
KBVoiceInputBarStateRecording,
KBVoiceInputBarStateCancel
};
/// 底部语音输入栏
/// 包含:毛玻璃背景 + 录音按钮
@interface KBVoiceInputBar : UIView
@@ -37,6 +49,12 @@ NS_ASSUME_NONNULL_BEGIN
/// 状态文本(显示在按钮上方)
@property (nonatomic, copy) NSString *statusText;
/// 输入模式(文字/语音)
@property (nonatomic, assign) KBVoiceInputBarMode inputMode;
/// 输入状态(文字/语音/录音/取消)
@property (nonatomic, assign) KBVoiceInputBarState inputState;
/// 是否启用(禁用时按钮不可点击)
@property (nonatomic, assign) BOOL enabled;

View File

@@ -11,18 +11,37 @@
@interface KBVoiceInputBar () <KBAiRecordButtonDelegate>
///
@property (nonatomic, strong) UIView *backgroundView;
///
@property (nonatomic, strong) UIVisualEffectView *blurEffectView;
///
@property (nonatomic, strong) UILabel *statusLabel;
///
@property (nonatomic, strong) KBAiRecordButton *recordButton;
///
@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;
///
@property (nonatomic, strong) UIView *cancelView;
@property (nonatomic, strong) UILabel *cancelLabel;
///
@property (nonatomic, strong) UITextField *hiddenTextField;
///
@property (nonatomic, assign) BOOL isRecording;
@@ -52,30 +71,8 @@
self.backgroundColor = [UIColor clearColor];
self.enabled = YES;
self.isRecording = NO;
//
[self addSubview:self.backgroundView];
[self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
//
[self.backgroundView addSubview:self.blurEffectView];
[self.blurEffectView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.backgroundView);
}];
// blurEffectView mask
CAGradientLayer *maskLayer = [CAGradientLayer layer];
maskLayer.startPoint = CGPointMake(0.5, 1); //
maskLayer.endPoint = CGPointMake(0.5, 0); //
maskLayer.colors = @[
(__bridge id)[UIColor whiteColor].CGColor, //
(__bridge id)[UIColor whiteColor].CGColor, //
(__bridge id)[UIColor clearColor].CGColor //
];
maskLayer.locations = @[@(0.0), @(0.5), @(1.0)];
self.blurEffectView.layer.mask = maskLayer;
self.inputMode = KBVoiceInputBarModeVoice;
self.inputState = KBVoiceInputBarStateText;
//
[self addSubview:self.statusLabel];
@@ -86,24 +83,92 @@
make.height.mas_equalTo(20);
}];
//
[self addSubview:self.recordButton];
[self.recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
//
[self addSubview:self.inputContainer];
[self.inputContainer mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.statusLabel.mas_bottom).offset(12);
make.left.equalTo(self).offset(20);
make.right.equalTo(self).offset(-20);
make.height.mas_equalTo(50);
make.bottom.lessThanOrEqualTo(self).offset(-16);
}];
}
- (void)layoutSubviews {
[super layoutSubviews];
UILongPressGestureRecognizer *longPress =
[[UILongPressGestureRecognizer alloc] initWithTarget:self
action:@selector(handleVoiceLongPress:)];
longPress.minimumPressDuration = 0.05;
longPress.cancelsTouchesInView = NO;
[self.inputContainer addGestureRecognizer:longPress];
// mask frame
if (self.blurEffectView.layer.mask) {
self.blurEffectView.layer.mask.frame = self.blurEffectView.bounds;
}
//
[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);
}];
//
[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;
}
#pragma mark - Setter
@@ -111,17 +176,55 @@
- (void)setStatusText:(NSString *)statusText {
_statusText = [statusText copy];
self.statusLabel.text = statusText;
[self updateCenterTextIfNeeded];
}
- (void)setEnabled:(BOOL)enabled {
_enabled = enabled;
self.recordButton.userInteractionEnabled = enabled;
self.recordButton.alpha = enabled ? 1.0 : 0.5;
self.inputContainer.userInteractionEnabled = enabled;
self.inputContainer.alpha = enabled ? 1.0 : 0.5;
}
- (void)setRecording:(BOOL)recording {
_isRecording = recording;
self.recordButton.state = recording ? KBAiRecordButtonStateRecording : KBAiRecordButtonStateNormal;
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];
}
[self updateCenterTextIfNeeded];
}
#pragma mark - Public Methods
@@ -162,22 +265,6 @@
#pragma mark - Lazy Load
- (UIView *)backgroundView {
if (!_backgroundView) {
_backgroundView = [[UIView alloc] init];
_backgroundView.clipsToBounds = YES;
}
return _backgroundView;
}
- (UIVisualEffectView *)blurEffectView {
if (!_blurEffectView) {
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
_blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
}
return _blurEffectView;
}
- (UILabel *)statusLabel {
if (!_statusLabel) {
_statusLabel = [[UILabel alloc] init];
@@ -195,8 +282,198 @@
_recordButton.delegate = self;
_recordButton.normalTitle = @"按住说话";
_recordButton.recordingTitle = @"松开结束";
_recordButton.normalIconImage = [UIImage imageNamed:@"ai_jianpan_icon"];
_recordButton.recordingIconImage = [UIImage imageNamed:@"ai_luyining_icon"];
_recordButton.hidden = YES;
}
return _recordButton;
}
- (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;
}
- (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 {
self.inputState = KBVoiceInputBarStateText;
[self.hiddenTextField becomeFirstResponder];
}
- (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 = @"按住说话";
}
}
@end