diff --git a/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m b/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m index 37c041b..0e1c39a 100644 --- a/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m +++ b/keyBoard/Class/AiTalk/VC/KBAIHomeVC.m @@ -82,6 +82,12 @@ @property (nonatomic, assign) NSInteger pendingAIRequestCount; +/// 是否处于语音流程中(录音/识别中,用于禁止滚动) +@property (nonatomic, assign) BOOL isVoiceProcessing; + +/// 是否正在语音录制中(用于禁止滚动) +@property (nonatomic, assign) BOOL isVoiceRecording; + /// 右上角消息按钮 @property (nonatomic, strong) UIButton *messageButton; @@ -491,8 +497,7 @@ - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { if (self.isWaitingForAIResponse) { NSLog(@"[KBAIHomeVC] 正在等待 AI 回复,禁止滚动"); - scrollView.scrollEnabled = NO; - scrollView.scrollEnabled = YES; + [self updateCollectionViewScrollState]; } } @@ -641,6 +646,14 @@ #pragma mark - Private +- (void)updateCollectionViewScrollState { + BOOL shouldEnable = !self.isWaitingForAIResponse + && !self.isVoiceRecording + && !self.isVoiceProcessing; + self.collectionView.scrollEnabled = shouldEnable; + self.collectionView.panGestureRecognizer.enabled = shouldEnable; +} + - (void)updateChatViewBottomInset { CGFloat bottomInset; @@ -827,14 +840,23 @@ } - (void)voiceToTextManagerDidBeginRecording:(KBVoiceToTextManager *)manager { + self.isVoiceRecording = YES; + self.isVoiceProcessing = YES; + [self updateCollectionViewScrollState]; [self.voiceRecordManager startRecording]; } - (void)voiceToTextManagerDidEndRecording:(KBVoiceToTextManager *)manager { + self.isVoiceRecording = NO; + self.isVoiceProcessing = YES; + [self updateCollectionViewScrollState]; [self.voiceRecordManager stopRecording]; } - (void)voiceToTextManagerDidCancelRecording:(KBVoiceToTextManager *)manager { + self.isVoiceRecording = NO; + self.isVoiceProcessing = NO; + [self updateCollectionViewScrollState]; [self.voiceRecordManager cancelRecording]; } @@ -871,6 +893,8 @@ if (cell) { [cell updateLastUserMessage:KBLocalized(@"语音识别失败")]; } + strongSelf.isVoiceProcessing = NO; + [strongSelf updateCollectionViewScrollState]; return; } @@ -881,6 +905,8 @@ if (cell) { [cell removeLoadingUserMessage]; } + strongSelf.isVoiceProcessing = NO; + [strongSelf updateCollectionViewScrollState]; return; } @@ -888,6 +914,7 @@ [cell updateLastUserMessage:transcript]; } + strongSelf.isVoiceProcessing = NO; [strongSelf handleTranscribedText:transcript appendToUI:NO]; }); }]; @@ -901,6 +928,9 @@ - (void)voiceRecordManager:(KBVoiceRecordManager *)manager didFailWithError:(NSError *)error { NSLog(@"[KBAIHomeVC] 录音失败:%@", error.localizedDescription); + self.isVoiceRecording = NO; + self.isVoiceProcessing = NO; + [self updateCollectionViewScrollState]; } #pragma mark - Private @@ -931,7 +961,7 @@ self.pendingAIRequestCount += 1; self.isWaitingForAIResponse = (self.pendingAIRequestCount > 0); if (self.pendingAIRequestCount == 1) { - self.collectionView.scrollEnabled = NO; + [self updateCollectionViewScrollState]; NSLog(@"[KBAIHomeVC] 开始等待 AI 回复,禁止 CollectionView 滚动"); } @@ -950,7 +980,7 @@ } strongSelf.isWaitingForAIResponse = (strongSelf.pendingAIRequestCount > 0); if (strongSelf.pendingAIRequestCount == 0) { - strongSelf.collectionView.scrollEnabled = YES; + [strongSelf updateCollectionViewScrollState]; NSLog(@"[KBAIHomeVC] AI 回复完成,恢复 CollectionView 滚动"); }