ReadViewSDK/Sources/RDPDFReaderView/ReaderView/RDPDFReaderView+CollectionView.swift

69 lines
3.3 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
extension RDPDFReaderView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
pageCount()
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let view = dataSource?.pageContentView(readerView: self, pageNum: indexPath.item) ?? UIView()
view.tag = indexPath.item
configureSpreadAlignment(for: view, page: indexPath.item)
configureInteraction(for: view)
if let cell = view.superview?.superview as? RDPDFReaderContentCell { return cell }
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fallback", for: indexPath) as! RDPDFReaderContentCell
cell.hostedView = view
return cell
}
public func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
// cell =
// collectionView
if usesWidthFitVerticalScroll,
let height = verticalScrollWidthFitPageHeightProvider?(indexPath.item, bounds.width),
height > 0 {
return CGSize(width: bounds.width, height: height)
}
return usesLandscapeSpread
? CGSize(width: bounds.width / 2, height: bounds.height)
: bounds.size
}
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
updateCurrentPageAfterScroll(scrollView)
}
public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
// isPagingEnabled = false
//
if !decelerate { updateCurrentPageAfterScroll(scrollView) }
}
private func updateCurrentPageAfterScroll(_ scrollView: UIScrollView) {
guard currentDisplayType != .pageCurl else { return }
//
pendingWidthFitJumpPage = nil
if currentDisplayType == .verticalScroll {
// cell cell
// / cell
let centerY = scrollView.contentOffset.y + scrollView.bounds.height / 2
let center = CGPoint(
x: scrollView.bounds.width / 2,
y: min(max(0, centerY), max(0, collectionView.contentSize.height - 1))
)
if let indexPath = collectionView.indexPathForItem(at: center) {
currentPage = indexPath.item
} else if let first = collectionView.indexPathsForVisibleItems.min(by: { $0.item < $1.item }) {
currentPage = first.item
}
return
}
let value = scrollView.contentOffset.x / max(bounds.width, 1)
currentPage = Int(value.rounded()) * (usesLandscapeSpread ? 2 : 1)
}
}