diff --git a/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Adapter.swift b/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Adapter.swift index 422d1e5..261a1b8 100755 --- a/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Adapter.swift +++ b/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Adapter.swift @@ -88,6 +88,7 @@ extension SessionController { DispatchQueue.main.async {[weak self] in self?.scrollToBottom(self?.tableView) + self?.updateScrToBottomButtonVisibility() } } @@ -99,7 +100,7 @@ extension SessionController { let delaySeconds = delay ?? 0 // let y = max(table.contentSize.height - table.bounds.size.height, 0) // table.setContentOffset(CGPoint(x: 0, y: y), animated: animated) - DispatchQueue.main.asyncAfter(deadline: .now() + delaySeconds) {[weak self] in + DispatchQueue.main.asyncAfter(deadline: .now() + delaySeconds) {[weak self] in if self?.isStreamChatMode == true { let count = self?.streamMessages.count ?? 0 if count > 0 { @@ -111,6 +112,10 @@ extension SessionController { table.scrollToRow(at: IndexPath(row: count - 1, section: 1), at: .bottom, animated: animated) } } + // 滚动完成后更新按钮状态 + DispatchQueue.main.asyncAfter(deadline: .now() + (animated ? 0.3 : 0.1)) { + self?.updateScrToBottomButtonVisibility() + } // Initial state DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) { if table.alpha == 0{ @@ -295,7 +300,7 @@ extension SessionController: UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {} func scrollViewDidScroll(_ scrollView: UIScrollView) { - //... + updateScrToBottomButtonVisibility() } func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { @@ -350,6 +355,48 @@ extension SessionController: UITableViewDelegate { UIPasteboard.general.string = text Hud.toast(str: "复制成功") } + + /// 更新滚动到底部按钮的显示/隐藏状态 + func updateScrToBottomButtonVisibility() { + guard let tableView = tableView, let scrToBottomBtn = scrToBottomBtn else { return } + + // 判断是否有内容 + let hasContent: Bool + if isStreamChatMode { + hasContent = streamMessages.count > 0 + } else { + hasContent = util.cellModels.count > 0 + } + + guard hasContent else { + scrToBottomBtn.isHidden = true + return + } + + // 判断是否可以往下滑动 + let contentHeight = tableView.contentSize.height + let tableViewHeight = tableView.bounds.height + let offsetY = tableView.contentOffset.y + let contentInsetTop = tableView.contentInset.top + let contentInsetBottom = tableView.contentInset.bottom + + // 计算实际内容高度(减去 inset) + let actualContentHeight = contentHeight - contentInsetTop - contentInsetBottom + let actualTableViewHeight = tableViewHeight - contentInsetTop - contentInsetBottom + + // 如果内容高度小于等于 tableView 高度,说明不能滚动,隐藏按钮 + guard actualContentHeight > actualTableViewHeight else { + scrToBottomBtn.isHidden = true + return + } + + // 计算距离底部的距离 + let distanceFromBottom = actualContentHeight - (offsetY - contentInsetTop) - actualTableViewHeight + + // 如果距离底部小于等于 50pt,说明已经接近底部,隐藏按钮 + // 否则显示按钮 + scrToBottomBtn.isHidden = distanceFromBottom <= 50 + } } diff --git a/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Input.swift b/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Input.swift index 6377091..b0651cb 100755 --- a/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Input.swift +++ b/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController+Input.swift @@ -51,6 +51,10 @@ extension SessionController { let scrToBottomBtn = UIButton(type: .custom) scrToBottomBtn.setImage(UIImage(named: "chat_scr_bottom"), for: .normal) scrToBottomBtn.addTarget(self, action: #selector(scrToBottomBtnTap), for: .touchUpInside) + scrToBottomBtn.isHidden = true // 默认隐藏 + + // 保存按钮引用 + self.scrToBottomBtn = scrToBottomBtn let stackView = UIStackView(arrangedSubviews: [scrToBottomBtn, exchangeBtn]) stackView.spacing = 10.0 diff --git a/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController.swift b/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController.swift index fdfa663..0fd160d 100755 --- a/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController.swift +++ b/Visual_Novel_iOS/Src/Modules/Chat/Session/SessionController.swift @@ -31,6 +31,7 @@ class SessionController: CLBaseViewController { // MARK: BottomViews var bottomViewsStackV : InputStackView! var toolView: UIView! + var scrToBottomBtn: UIButton! var inputEntrance: SessionInputOperateView! var inputBar: SessionInputView! var moreView: IMMoreItemView! @@ -251,7 +252,7 @@ extension SessionController { } return v }() - // bgImageView.image = UIImage(named: "egpic")?.cropImageTop(with: 1 / UIScreen.aspectRatio) + bgImageView.image = UIImage(named: "egpic")?.cropImageTop(with: 1 / UIScreen.aspectRatio) overlay = { let v = GradientView(colors: [UIColor.c.cbn.withAlphaComponent(1), UIColor.c.cbn.withAlphaComponent(0), UIColor.c.cbn.withAlphaComponent(0), UIColor.c.cbn.withAlphaComponent(1)], gradientType: .topToBottom) @@ -274,7 +275,8 @@ extension SessionController { swipeBgView = { let bgView = UIView() bgView.alpha = 0.0 - bgView.backgroundColor = UIColor.init(white: 0.0, alpha: 0.8) +// bgView.backgroundColor = UIColor.init(white: 0.0, alpha: 0.8) + bgView.backgroundColor = .clear view.addSubview(bgView) bgView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(bgViewTap))) bgView.snp.makeConstraints { make in @@ -655,6 +657,7 @@ extension SessionController { self.startDisplayLinkIfNeeded() self.smoothScrollToLatest() + self.updateScrToBottomButtonVisibility() } } @@ -672,6 +675,7 @@ extension SessionController { tableView.endUpdates() } smoothScrollToLatest() + updateScrToBottomButtonVisibility() } private func finalizeStreamingMessage() { @@ -695,6 +699,7 @@ extension SessionController { self.startDisplayLinkIfNeeded() } + self.updateScrToBottomButtonVisibility() IMSSEManager.shared.disconnect() } } @@ -835,6 +840,7 @@ extension SessionController { tableView.endUpdates() } smoothScrollToLatest() + updateScrToBottomButtonVisibility() } cleanupTypingState() } diff --git a/Visual_Novel_iOS/Src/Modules/Chat/Session/View/SessionNavigationView.swift b/Visual_Novel_iOS/Src/Modules/Chat/Session/View/SessionNavigationView.swift index 55c2bc0..7c306a3 100644 --- a/Visual_Novel_iOS/Src/Modules/Chat/Session/View/SessionNavigationView.swift +++ b/Visual_Novel_iOS/Src/Modules/Chat/Session/View/SessionNavigationView.swift @@ -174,7 +174,7 @@ class SessionNavigationView: UIView { make.leading.equalToSuperview() make.trailing.equalToSuperview() make.height.equalTo(UIWindow.navBarTotalHeight) - make.bottom.equalToSuperview().offset(-40) + make.bottom.equalToSuperview().offset(0) } }