ReadViewSDK/Sources/RDEpubReaderView/EPUBUI/RDEPUBReaderTopToolView.swift
shenlei 1422461224 将 PDF 成品阅读控制器下沉为 RDPDFReaderView SDK,并修复手机横屏单页连续竖滑
SDK 化收尾:
- 新增 RDPDFReaderView 本地 pod(阅读容器、成品控制器、划线/注释、画笔、
  目录/设置面板、主题与持久化契约),Demo 精简为 PDFKit 页面提供者 + 持久化宿主
- Podfile/工程接入 RDPDFReaderView 与 SnapKit,示例书改用 PDF 阅读器示例文件

手机横屏单页竖滑修复:
- cell 高度改为宽度适配后的纸张高度(sizeForItemAt + 宿主按页提供纵横比),
  纵向超出一屏的内容由外层 collectionView 连续滚动承接,不再被裁掉
- 页面内部不再开启同向嵌套的兜底竖向拖动,修复外层滚动被吞、无法滚到下一页
- 修复 applyContentSize 先评估拖动开关后写 contentSize 的顺序缺陷
- 页码按视口中心命中 cell 计算,补 didEndDragging 兜底;旋转重锚点与
  transitionToPage 改用真实布局位置
- 横竖屏同为竖滑时也重建 cell 并刷新全部可见页的适配配置
- 横屏单页点击不再跳页,只显隐工具栏;翻页交给连续竖滑

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-15 18:49:44 +09:00

165 lines
6.7 KiB
Swift

import UIKit
import SnapKit
open class RDEPUBReaderTopToolView: RDEPUBReaderToolView, RDEPUBReaderTopToolViewProtocol {
public var onBack: (() -> Void)?
public var onToggleBookmark: (() -> Void)?
public var onSearch: (() -> Void)?
/// 宿 /
public var onAuthorization: (() -> Void)?
private let backButton = RDEPUBReaderTintButton(type: .system)
private let searchButton = RDEPUBReaderTintButton(type: .system)
private let bookmarkButton = RDEPUBReaderTintButton(type: .system)
private let authorizationButton = RDEPUBReaderTintButton(type: .system)
private var authorizationButtonWidthConstraint: Constraint!
private let titleLabel: UILabel = {
let label = UILabel()
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
label.numberOfLines = 1
return label
}()
private var isBookmarked = false
public override init(frame: CGRect) {
super.init(frame: frame)
accessibilityIdentifier = "epub.reader.topToolbar"
self.backgroundColor = .white
addSubview(backButton)
addSubview(searchButton)
addSubview(bookmarkButton)
addSubview(authorizationButton)
addSubview(titleLabel)
backButton.addTarget(self, action: #selector(backAction), for: .touchUpInside)
searchButton.addTarget(self, action: #selector(searchAction), for: .touchUpInside)
bookmarkButton.addTarget(self, action: #selector(bookmarkAction), for: .touchUpInside)
authorizationButton.addTarget(self, action: #selector(authorizationAction), for: .touchUpInside)
backButton.snp.makeConstraints { make in
make.leading.equalToSuperview().offset(8)
make.top.equalTo(safeAreaLayoutGuide).offset(4)
make.bottom.equalToSuperview().inset(4)
make.size.equalTo(44)
}
authorizationButton.snp.makeConstraints { make in
make.trailing.equalToSuperview().inset(8)
make.top.equalTo(safeAreaLayoutGuide).offset(4)
make.bottom.equalToSuperview().inset(4)
make.height.equalTo(44)
authorizationButtonWidthConstraint = make.width.equalTo(0).constraint
}
bookmarkButton.snp.makeConstraints { make in
make.trailing.equalTo(authorizationButton.snp.leading).offset(-4)
make.top.equalTo(safeAreaLayoutGuide).offset(4)
make.bottom.equalToSuperview().inset(4)
make.size.equalTo(44)
}
searchButton.snp.makeConstraints { make in
make.trailing.equalTo(bookmarkButton.snp.leading).offset(-4)
make.top.equalTo(safeAreaLayoutGuide).offset(4)
make.bottom.equalToSuperview().inset(4)
make.size.equalTo(44)
}
titleLabel.snp.makeConstraints { make in
make.leading.equalTo(backButton.snp.trailing).offset(8)
make.trailing.equalTo(searchButton.snp.leading).offset(-8)
make.centerY.equalTo(backButton)
}
if #available(iOS 13.0, *) {
backButton.setImage(UIImage(systemName: "chevron.left")?.withRenderingMode(.alwaysTemplate), for: .normal)
searchButton.setImage(UIImage(systemName: "magnifyingglass")?.withRenderingMode(.alwaysTemplate), for: .normal)
} else {
backButton.setTitle("返回", for: .normal)
searchButton.setTitle("搜索", for: .normal)
}
backButton.accessibilityIdentifier = "epub.reader.back"
searchButton.accessibilityIdentifier = "epub.reader.search"
bookmarkButton.accessibilityIdentifier = "epub.reader.bookmark"
authorizationButton.accessibilityIdentifier = "epub.reader.authorization"
titleLabel.accessibilityIdentifier = "epub.reader.title"
updateBookmarkButtonAppearance()
}
public required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func lineFrame(in bounds: CGRect) -> CGRect {
CGRect(x: 0, y: bounds.height - 0.5, width: bounds.width, height: 0.5)
}
override public func apply(theme: RDEPUBReaderTheme) {
super.apply(theme: theme)
titleLabel.textColor = theme.toolControlTextColor
backButton.tintColor = theme.toolControlTextColor
searchButton.tintColor = theme.toolControlTextColor
bookmarkButton.tintColor = theme.toolControlTextColor
authorizationButton.tintColor = theme.toolControlTextColor
if #unavailable(iOS 13.0) {
backButton.setTitleColor(theme.toolControlTextColor, for: .normal)
searchButton.setTitleColor(theme.toolControlTextColor, for: .normal)
bookmarkButton.setTitleColor(theme.toolControlTextColor, for: .normal)
authorizationButton.setTitleColor(theme.toolControlTextColor, for: .normal)
}
updateBookmarkButtonAppearance()
}
public func setTitle(_ title: String?) {
titleLabel.text = title
}
public func setBookmarkSelected(_ isSelected: Bool) {
isBookmarked = isSelected
updateBookmarkButtonAppearance()
}
public func setBookmarkEnabled(_ isEnabled: Bool) {
bookmarkButton.isEnabled = isEnabled
bookmarkButton.alpha = isEnabled ? 1 : 0.45
}
/// `nil`
public func setAuthorizationButton(title: String?) {
let resolvedTitle = title?.trimmingCharacters(in: .whitespacesAndNewlines)
let isVisible = resolvedTitle?.isEmpty == false
authorizationButton.setTitle(resolvedTitle, for: .normal)
authorizationButton.isHidden = !isVisible
authorizationButton.isEnabled = isVisible
authorizationButtonWidthConstraint.update(offset: isVisible ? 56 : 0)
authorizationButton.accessibilityLabel = resolvedTitle
}
@objc private func backAction() {
onBack?()
}
@objc private func searchAction() {
onSearch?()
}
@objc private func bookmarkAction() {
onToggleBookmark?()
}
@objc private func authorizationAction() {
onAuthorization?()
}
private func updateBookmarkButtonAppearance() {
if #available(iOS 13.0, *) {
let imageName = isBookmarked ? "bookmark.fill" : "bookmark"
bookmarkButton.setImage(UIImage(systemName: imageName)?.withRenderingMode(.alwaysTemplate), for: .normal)
} else {
bookmarkButton.setTitle(isBookmarked ? "已签" : "书签", for: .normal)
}
bookmarkButton.accessibilityValue = isBookmarked ? "selected" : "unselected"
}
}