import UIKit import SnapKit /// 阅读器 Chrome 的安装、尺寸更新与显示动画。 extension RDPDFReaderView { enum ToolViewPosition { case top, bottom } func toggleToolbars() { isToolViewVisible.toggle() if isToolViewVisible { if let topToolView { showToolView(topToolView, position: .top) } if let bottomToolView { showToolView(bottomToolView, position: .bottom) } } else { if let topToolView { hideToolView(topToolView, position: .top) } if let bottomToolView { hideToolView(bottomToolView, position: .bottom) } } delegate?.toolViewVisibilityChanged?(readerView: self, isVisible: isToolViewVisible) } func showToolView(_ toolView: UIView, position: ToolViewPosition) { installToolViewIfNeeded(toolView, position: position) layoutIfNeeded() toolView.transform = CGAffineTransform(translationX: 0, y: position == .top ? -toolView.bounds.height : toolView.bounds.height) UIView.animate(withDuration: 0.25) { toolView.transform = .identity } } func hideToolView(_ toolView: UIView, position: ToolViewPosition) { UIView.animate(withDuration: 0.25, animations: { toolView.transform = CGAffineTransform(translationX: 0, y: position == .top ? -toolView.bounds.height : toolView.bounds.height) }) { _ in toolView.removeFromSuperview() toolView.transform = .identity } } func installToolViewIfNeeded(_ toolView: UIView, position: ToolViewPosition) { guard toolView.superview !== self else { return } toolView.removeFromSuperview() addSubview(toolView) let heightConstraint: Constraint switch position { case .top: topToolViewHeightConstraint?.deactivate() var height: Constraint! toolView.snp.makeConstraints { make in make.leading.trailing.top.equalToSuperview() height = make.height.equalTo(safeAreaInsets.top + 52).constraint } heightConstraint = height topToolViewHeightConstraint = heightConstraint case .bottom: bottomToolViewHeightConstraint?.deactivate() var height: Constraint! toolView.snp.makeConstraints { make in make.leading.trailing.bottom.equalToSuperview() height = make.height.equalTo(safeAreaInsets.bottom + 52).constraint } heightConstraint = height bottomToolViewHeightConstraint = heightConstraint } } func updateToolViewHeightConstraintsIfNeeded() { topToolViewHeightConstraint?.update(offset: safeAreaInsets.top + 52) bottomToolViewHeightConstraint?.update(offset: safeAreaInsets.bottom + 52) } }