fix: restore demo build and drawing interactions
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "RDEpubReaderView"
|
||||
s.module_name = "RDEpubReaderView"
|
||||
s.version = "0.0.1"
|
||||
s.version = "0.0.2"
|
||||
s.summary = "A reader view for EPUB and plain-text books"
|
||||
s.platform = :ios, "15.0"
|
||||
s.swift_versions = ["5.10"]
|
||||
@@ -9,11 +9,14 @@ Pod::Spec.new do |s|
|
||||
s.author = { "shenlei" => "shenlei@touchread.com" }
|
||||
s.source = { :git => "http://192.168.21.200:8418/4v5u09Z5a4Yuc/ReadViewSDK.git", :tag => "#{s.version}" }
|
||||
s.license = "MIT"
|
||||
s.source_files = "Sources/RDEpubReaderView/**/*.{swift}"
|
||||
# This podspec lives inside Sources/RDEpubReaderView, so paths must be
|
||||
# relative to this directory when it is consumed as a local pod.
|
||||
s.source_files = "**/*.swift"
|
||||
s.resource_bundles = {
|
||||
"RDEpubReaderViewAssets" => ["Sources/RDEpubReaderView/EPUBCore/Resources/**/*"]
|
||||
"RDEpubReaderViewAssets" => ["EPUBCore/Resources/**/*"]
|
||||
}
|
||||
s.dependency "ZIPFoundation", "~> 0.9"
|
||||
s.dependency "DTCoreText", "~> 1.6"
|
||||
s.dependency "SnapKit", "~> 5.7"
|
||||
s.requires_arc = true
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = "RDPDFReaderView"
|
||||
s.module_name = "RDPDFReaderView"
|
||||
s.version = "0.1.0"
|
||||
s.version = "0.0.1"
|
||||
s.summary = "Independent UIKit PDF reader interaction engine"
|
||||
s.platform = :ios, "15.0"
|
||||
s.swift_versions = ["5.10"]
|
||||
|
||||
@@ -4,7 +4,8 @@ import SnapKit
|
||||
/// 对齐 SheetMusic 的画笔工具栏:上方为尺寸与颜色,下方为笔刷、橡皮、图层及撤销重做。
|
||||
/// 工具栏只表达 UI 状态,不持有任何 PDF 页或笔迹数据。
|
||||
public final class RDPDFReaderDrawingToolbar: UIView {
|
||||
public var toolChangedHandler: ((RDPDFReaderDrawingTool) -> Void)?
|
||||
/// `nil` 表示没有激活绘图工具,此时阅读页可用于拖动画面。
|
||||
public var toolChangedHandler: ((RDPDFReaderDrawingTool?) -> Void)?
|
||||
public var colorChangedHandler: ((UIColor) -> Void)?
|
||||
public var lineWidthChangedHandler: ((CGFloat) -> Void)?
|
||||
public var undoHandler: (() -> Void)?
|
||||
@@ -17,10 +18,13 @@ public final class RDPDFReaderDrawingToolbar: UIView {
|
||||
private let sizeValueLabel = UILabel()
|
||||
private let colors: [UIColor] = [.white, .black, UIColor(red: 0.87, green: 0.17, blue: 0.17, alpha: 1), UIColor(red: 1, green: 0.56, blue: 0.16, alpha: 1), UIColor(red: 0.96, green: 0.89, blue: 0.20, alpha: 1), UIColor(red: 0.24, green: 0.76, blue: 0.24, alpha: 1), UIColor(red: 0.12, green: 0.83, blue: 0.93, alpha: 1), UIColor(red: 0.21, green: 0.36, blue: 0.90, alpha: 1), UIColor(red: 0.68, green: 0.36, blue: 1, alpha: 1), UIColor(red: 0.99, green: 0.40, blue: 0.61, alpha: 1)]
|
||||
private var colorButtons: [UIButton] = []
|
||||
private var selectedTool: RDPDFReaderDrawingTool = .pen
|
||||
private var selectedTool: RDPDFReaderDrawingTool?
|
||||
private var penButton: UIButton?
|
||||
private var eraserButton: UIButton?
|
||||
|
||||
public override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
accessibilityIdentifier = "pdf.reader.drawing.toolbar"
|
||||
backgroundColor = UIColor(red: 0.20, green: 0.20, blue: 0.20, alpha: 0.98)
|
||||
setupViews()
|
||||
}
|
||||
@@ -98,8 +102,16 @@ public final class RDPDFReaderDrawingToolbar: UIView {
|
||||
make.bottom.equalTo(safeAreaLayoutGuide).inset(9)
|
||||
make.height.equalTo(32)
|
||||
}
|
||||
toolStack.addArrangedSubview(makeIconButton(symbol: "pencil", action: #selector(penTapped), selected: true))
|
||||
toolStack.addArrangedSubview(makeIconButton(symbol: "eraser", action: #selector(eraserTapped)))
|
||||
let penButton = makeIconButton(symbol: "pencil", action: #selector(penTapped))
|
||||
let eraserButton = makeIconButton(symbol: "eraser", action: #selector(eraserTapped))
|
||||
penButton.accessibilityIdentifier = "pdf.reader.drawing.pen"
|
||||
penButton.accessibilityLabel = "画笔"
|
||||
eraserButton.accessibilityIdentifier = "pdf.reader.drawing.eraser"
|
||||
eraserButton.accessibilityLabel = "橡皮擦"
|
||||
self.penButton = penButton
|
||||
self.eraserButton = eraserButton
|
||||
toolStack.addArrangedSubview(penButton)
|
||||
toolStack.addArrangedSubview(eraserButton)
|
||||
toolStack.addArrangedSubview(makeIconButton(symbol: "square.3.layers.3d", action: #selector(layersTapped)))
|
||||
|
||||
let clearButton = makeTextButton("清空", action: #selector(clearTapped))
|
||||
@@ -117,10 +129,10 @@ public final class RDPDFReaderDrawingToolbar: UIView {
|
||||
undo.snp.makeConstraints { make in make.centerY.equalTo(toolStack); make.trailing.equalTo(redo.snp.leading).offset(-20) }
|
||||
}
|
||||
|
||||
private func makeIconButton(symbol: String, action: Selector, selected: Bool = false) -> UIButton {
|
||||
private func makeIconButton(symbol: String, action: Selector) -> UIButton {
|
||||
let button = UIButton(type: .system)
|
||||
button.setImage(UIImage(systemName: symbol), for: .normal)
|
||||
button.tintColor = selected ? UIColor(red: 0.45, green: 0.52, blue: 1, alpha: 1) : .white
|
||||
button.tintColor = .white
|
||||
button.contentEdgeInsets = .init(top: 3, left: 3, bottom: 3, right: 3)
|
||||
button.addTarget(self, action: action, for: .touchUpInside)
|
||||
button.snp.makeConstraints { $0.size.equalTo(28) }
|
||||
@@ -156,7 +168,10 @@ public final class RDPDFReaderDrawingToolbar: UIView {
|
||||
@objc private func doneTapped() { doneHandler?() }
|
||||
|
||||
private func selectTool(_ tool: RDPDFReaderDrawingTool) {
|
||||
selectedTool = tool
|
||||
toolChangedHandler?(tool)
|
||||
selectedTool = selectedTool == tool ? nil : tool
|
||||
let activeColor = UIColor(red: 0.45, green: 0.52, blue: 1, alpha: 1)
|
||||
penButton?.tintColor = selectedTool == .pen ? activeColor : .white
|
||||
eraserButton?.tintColor = selectedTool == .eraser ? activeColor : .white
|
||||
toolChangedHandler?(selectedTool)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,13 +63,21 @@ public final class RDPDFReaderPageView: UIView, RDPDFReaderPageInteractable, UIG
|
||||
private var tappedHighlight: RDPDFReaderAnnotation?
|
||||
|
||||
/// 画笔层属于单个实际 PDF 页,而不是横屏双页容器;因此笔迹不能越过书脊。
|
||||
/// 画笔面板已打开时,禁用文本选区以保证单指手势可以用于拖动画面。
|
||||
public var isDrawingSessionActive = false {
|
||||
didSet {
|
||||
textLayer.isSelectionEnabled = !isDrawingSessionActive
|
||||
if isDrawingSessionActive { textLayer.clearSelection() }
|
||||
}
|
||||
}
|
||||
|
||||
/// 仅在画笔或橡皮擦被选中时启用画布,单指手势才会被画布消费。
|
||||
public var isDrawingMode = false {
|
||||
didSet {
|
||||
// 退出编辑只关闭触摸,不隐藏画布;否则已持久化的笔迹会和工具栏一起消失。
|
||||
drawingCanvas.isHidden = false
|
||||
drawingCanvas.isUserInteractionEnabled = isDrawingMode
|
||||
textLayer.isSelectionEnabled = !isDrawingMode
|
||||
if isDrawingMode { textLayer.clearSelection() }
|
||||
zoomView.isDrawingMode = isDrawingMode
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,8 @@ public final class RDPDFReaderViewController: UIViewController, RDPDFReaderDataS
|
||||
private weak var bottomToolbar: RDPDFReaderKitBottomToolView?
|
||||
private let drawingToolbar = RDPDFReaderDrawingToolbar()
|
||||
private var isDrawingMode = false
|
||||
private var currentDrawingTool: RDPDFReaderDrawingTool = .pen
|
||||
/// `nil` 时处于画笔面板内的浏览状态,可单指拖动画面。
|
||||
private var currentDrawingTool: RDPDFReaderDrawingTool?
|
||||
private var currentDrawingColor = UIColor.black
|
||||
private var currentDrawingLineWidth: CGFloat = 4
|
||||
private weak var continuedDrawingPage: RDPDFReaderPageView?
|
||||
@@ -127,13 +128,12 @@ public final class RDPDFReaderViewController: UIViewController, RDPDFReaderDataS
|
||||
public func setDrawingMode(_ enabled: Bool) {
|
||||
guard isDrawingMode != enabled else { return }
|
||||
isDrawingMode = enabled
|
||||
readerView.setPagingEnabled(!enabled)
|
||||
if enabled {
|
||||
installDrawingToolbar()
|
||||
} else {
|
||||
drawingToolbar.removeFromSuperview()
|
||||
}
|
||||
configureVisibleDrawingPages()
|
||||
updateDrawingInteractionState()
|
||||
}
|
||||
|
||||
public func pageCountOfReaderView(readerView: RDPDFReaderView) -> Int { book.totalPages }
|
||||
@@ -185,7 +185,8 @@ public final class RDPDFReaderViewController: UIViewController, RDPDFReaderDataS
|
||||
let runs = descriptor?.textRuns ?? ocrRuns[index] ?? []
|
||||
let source: RDPDFReaderAnnotationSource = descriptor?.textRuns != nil ? .text : (configuration.enablesOCR ? .ocr : configuration.missingTextSource)
|
||||
page.configureTextLayer(pageIndex: index, textRuns: runs, textSource: source, annotations: annotations(for: index))
|
||||
page.isDrawingMode = isDrawingMode
|
||||
page.isDrawingSessionActive = isDrawingMode
|
||||
page.isDrawingMode = isDrawingMode && currentDrawingTool != nil
|
||||
page.configureDrawing(
|
||||
pageIndex: index,
|
||||
document: (annotationPersistence as? RDPDFReaderPersistenceStore)?.drawingDocument(pageNo: index) ?? .init(pageNo: index, paths: []),
|
||||
@@ -193,7 +194,9 @@ public final class RDPDFReaderViewController: UIViewController, RDPDFReaderDataS
|
||||
(self?.annotationPersistence as? RDPDFReaderPersistenceStore)?.saveDrawingDocument(document, pageNo: index)
|
||||
}
|
||||
)
|
||||
page.setDrawingTool(currentDrawingTool, color: currentDrawingColor, lineWidth: currentDrawingLineWidth)
|
||||
if let currentDrawingTool {
|
||||
page.setDrawingTool(currentDrawingTool, color: currentDrawingColor, lineWidth: currentDrawingLineWidth)
|
||||
}
|
||||
page.drawingStrokeEventHandler = { [weak self] source, point, phase in
|
||||
if phase == .began { self?.activeDrawingPage = source }
|
||||
self?.routeDrawingStroke(from: source, point: point, phase: phase)
|
||||
@@ -282,7 +285,9 @@ public final class RDPDFReaderViewController: UIViewController, RDPDFReaderDataS
|
||||
drawingToolbar.lineWidthChangedHandler = { [weak self] width in
|
||||
guard let self else { return }
|
||||
self.currentDrawingLineWidth = width
|
||||
self.visibleDrawingPages.forEach { $0.setDrawingTool(self.currentDrawingTool, lineWidth: width) }
|
||||
if let tool = self.currentDrawingTool {
|
||||
self.visibleDrawingPages.forEach { $0.setDrawingTool(tool, lineWidth: width) }
|
||||
}
|
||||
}
|
||||
drawingToolbar.undoHandler = { [weak self] in self?.visibleDrawingPages.forEach { $0.undoDrawing() } }
|
||||
drawingToolbar.redoHandler = { [weak self] in self?.visibleDrawingPages.forEach { $0.redoDrawing() } }
|
||||
@@ -296,15 +301,27 @@ public final class RDPDFReaderViewController: UIViewController, RDPDFReaderDataS
|
||||
}
|
||||
|
||||
private func configureVisibleDrawingPages() {
|
||||
visibleDrawingPages.forEach { page in page.isDrawingMode = isDrawingMode }
|
||||
visibleDrawingPages.forEach { page in
|
||||
page.isDrawingSessionActive = isDrawingMode
|
||||
page.isDrawingMode = isDrawingMode && currentDrawingTool != nil
|
||||
}
|
||||
}
|
||||
|
||||
private func applyDrawingTool(_ tool: RDPDFReaderDrawingTool, color: UIColor? = nil) {
|
||||
private func applyDrawingTool(_ tool: RDPDFReaderDrawingTool?, color: UIColor? = nil) {
|
||||
currentDrawingTool = tool
|
||||
if let color { currentDrawingColor = color }
|
||||
updateDrawingInteractionState()
|
||||
guard let tool else { return }
|
||||
visibleDrawingPages.forEach { $0.setDrawingTool(tool, color: color, lineWidth: currentDrawingLineWidth) }
|
||||
}
|
||||
|
||||
/// 未选择画笔工具时恢复阅读器自身的滚动。手机横屏单页的连续竖滑
|
||||
/// 由外层 collectionView 承担,不能在画笔面板打开期间一概禁用。
|
||||
private func updateDrawingInteractionState() {
|
||||
readerView.setPagingEnabled(!isDrawingMode || currentDrawingTool == nil)
|
||||
configureVisibleDrawingPages()
|
||||
}
|
||||
|
||||
private func showDrawingLayers() {
|
||||
let page = activeDrawingPage ?? visibleDrawingPages.first
|
||||
guard let page else { return }
|
||||
|
||||
Reference in New Issue
Block a user