fix(reader): restore tool views and close action

This commit is contained in:
shen 2026-05-26 21:41:04 +08:00
parent 736902b489
commit 22adb76332
2 changed files with 63 additions and 14 deletions

View File

@ -1162,6 +1162,10 @@ public final class RDEPUBReaderController: UIViewController {
navigationController.popViewController(animated: true)
return
}
if let navigationController, navigationController.presentingViewController != nil {
navigationController.dismiss(animated: true)
return
}
dismiss(animated: true)
}

View File

@ -309,6 +309,10 @@ public class RDReaderView: UIView {
private var topToolView: UIView?
///
private var bottomToolView: UIView?
///
private var topToolViewHeightConstraint: NSLayoutConstraint?
///
private var bottomToolViewHeightConstraint: NSLayoutConstraint?
///
private var isShowToolView: Bool = false
///
@ -364,6 +368,7 @@ public class RDReaderView: UIView {
super.layoutSubviews()
guard bounds.width > 0, bounds.height > 0 else { return }
preloadHostView.frame = bounds
updateToolViewHeightConstraintsIfNeeded()
let nowLandscape = isLandscape
if let prev = previousIsLandscape, prev != nowLandscape {
previousIsLandscape = nowLandscape
@ -849,13 +854,7 @@ public class RDReaderView: UIView {
isShowToolView = !isShowToolView
if isShowToolView {
if let topToolView = topToolView {
addSubview(topToolView)
topToolView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
topToolView.leadingAnchor.constraint(equalTo: leadingAnchor),
topToolView.trailingAnchor.constraint(equalTo: trailingAnchor),
topToolView.topAnchor.constraint(equalTo: topAnchor)
])
installToolViewIfNeeded(topToolView, position: .top)
layoutIfNeeded()
topToolView.transform = CGAffineTransform(translationX: 0, y: -topToolView.bounds.height)
UIView.animate(withDuration: toolViewAnimationDuration) {
@ -864,13 +863,7 @@ public class RDReaderView: UIView {
}
if let bottomToolView = bottomToolView {
addSubview(bottomToolView)
bottomToolView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
bottomToolView.leadingAnchor.constraint(equalTo: leadingAnchor),
bottomToolView.trailingAnchor.constraint(equalTo: trailingAnchor),
bottomToolView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
installToolViewIfNeeded(bottomToolView, position: .bottom)
layoutIfNeeded()
bottomToolView.transform = CGAffineTransform(translationX: 0, y: bottomToolView.bounds.height)
UIView.animate(withDuration: toolViewAnimationDuration) {
@ -912,6 +905,58 @@ public class RDReaderView: UIView {
return hitView === toolView || hitView.isDescendant(of: toolView)
}
private enum ToolViewPosition {
case top
case bottom
}
private func installToolViewIfNeeded(_ toolView: UIView, position: ToolViewPosition) {
guard toolView.superview !== self else { return }
toolView.removeFromSuperview()
addSubview(toolView)
toolView.translatesAutoresizingMaskIntoConstraints = false
let heightConstraint: NSLayoutConstraint
switch position {
case .top:
topToolViewHeightConstraint?.isActive = false
heightConstraint = toolView.heightAnchor.constraint(equalToConstant: resolvedToolViewHeight(for: .top))
topToolViewHeightConstraint = heightConstraint
NSLayoutConstraint.activate([
toolView.leadingAnchor.constraint(equalTo: leadingAnchor),
toolView.trailingAnchor.constraint(equalTo: trailingAnchor),
toolView.topAnchor.constraint(equalTo: topAnchor),
heightConstraint
])
case .bottom:
bottomToolViewHeightConstraint?.isActive = false
heightConstraint = toolView.heightAnchor.constraint(equalToConstant: resolvedToolViewHeight(for: .bottom))
bottomToolViewHeightConstraint = heightConstraint
NSLayoutConstraint.activate([
toolView.leadingAnchor.constraint(equalTo: leadingAnchor),
toolView.trailingAnchor.constraint(equalTo: trailingAnchor),
toolView.bottomAnchor.constraint(equalTo: bottomAnchor),
heightConstraint
])
}
}
private func updateToolViewHeightConstraintsIfNeeded() {
topToolViewHeightConstraint?.constant = resolvedToolViewHeight(for: .top)
bottomToolViewHeightConstraint?.constant = resolvedToolViewHeight(for: .bottom)
}
private func resolvedToolViewHeight(for position: ToolViewPosition) -> CGFloat {
let contentHeight: CGFloat = 52
switch position {
case .top:
return safeAreaInsets.top + contentHeight
case .bottom:
return safeAreaInsets.bottom + contentHeight
}
}
/// 仿//
/// PageViewController CollectionView
/// - Parameter displayType: