源码注释: - 为 ~60 个 Swift 文件补充缺失的 doc comment(file header、类型、属性、方法) - 修正 4 处错误注释:翻页模式数量、搜索行为描述、手势识别器描述、悬空文档块 文档维护: - 删除重复文档:WXRead/读书EPUB阅读器实现架构.md(与微信读书版完全一致) - 合并重叠文档:阅读器规划.md → 阅读器功能开发计划.md(单一真值) - 修正过时内容:所有文档中"四种翻页模式"→"三种",移除 horizontalCoverScroll - 更新架构图:补齐 EPUBUI/ReaderController、Paging/、Typesetter/ 等子目录 - 更新 index.md 索引:新增开发计划和架构对比文档引用
57 lines
2.6 KiB
Swift
57 lines
2.6 KiB
Swift
//
|
||
// RDReaderView+CollectionView.swift
|
||
// ReadViewSDK
|
||
//
|
||
// 文件职责:UICollectionView 数据源和自定义布局代理实现,处理水平滚动和垂直滚动两种翻页模式。
|
||
//
|
||
|
||
import UIKit
|
||
|
||
/// UICollectionView 数据源和自定义布局代理实现
|
||
/// 处理水平滚动和垂直滚动两种翻页模式
|
||
extension RDReaderView: UICollectionViewDataSource, RDReaderFlowLayoutDelegate, RDReaderFlowLayoutDataSoure {
|
||
/// 配置 UICollectionViewCell
|
||
/// 通过 DataSource 获取页面内容视图,支持 cell 复用和预加载视图复用
|
||
/// RTL 水平模式下翻转 cell 内容使文字方向正常
|
||
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||
|
||
if let identifer = pageReuseIdentifier(for: indexPath.row) {
|
||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifer, for: IndexPath(item: indexPath.row, section: 0)) as! RDReaderContentCell
|
||
let preloadedView = preloadController.takePreloadedView(for: indexPath.row)
|
||
let reusableView = cell.containerView ?? preloadedView
|
||
cell.containerView = contentViewForPage(indexPath.row, reusableView: reusableView)
|
||
// RTL 水平模式:翻转 cell 内容使文字方向正常(collectionView 已整体翻转)
|
||
if pageDirection == .rightToLeft && currentDisplayType != .verticalScroll {
|
||
cell.contentView.transform = CGAffineTransform(scaleX: -1, y: 1)
|
||
} else {
|
||
cell.contentView.transform = .identity
|
||
}
|
||
return cell
|
||
}
|
||
|
||
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(UICollectionViewCell.self), for: indexPath)
|
||
|
||
return cell
|
||
}
|
||
|
||
/// 返回每组的 item 数量,即总页数
|
||
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||
return numberOfPages()
|
||
}
|
||
|
||
/// 布局代理回调:当前可见页码变化时通知
|
||
public func pageNum(flowLayout: RDReaderFlowLayout, pageIndex: Int) {
|
||
if currentPage >= 0, currentPage != pageIndex {
|
||
predictedPageDirection = pageIndex >= currentPage
|
||
}
|
||
currentPage = pageIndex
|
||
primePageCache(around: pageIndex, preferredForward: predictedPageDirection)
|
||
}
|
||
|
||
/// 布局数据源回调:返回垂直滚动模式下指定页的高度
|
||
/// 当前返回 nil 使用默认高度
|
||
public func heigtOfVerticalScrollPage(flowLayout: RDReaderFlowLayout, pageIndex: Int) -> CGFloat? {
|
||
return nil
|
||
}
|
||
}
|