feat: load only visible strokes

This commit is contained in:
dscyrescotti
2024-05-14 23:53:24 +07:00
parent c1b9baa354
commit 8ee010b77a
8 changed files with 87 additions and 15 deletions
@@ -41,13 +41,13 @@ class CanvasViewController: UIViewController {
super.viewDidLoad()
configureViews()
configureListeners()
loadMemo()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
resizeDocumentView()
updateDocumentBounds()
loadMemo()
}
override func viewDidLayoutSubviews() {
@@ -119,11 +119,11 @@ extension CanvasViewController {
let newFrame = CGRect(x: 0, y: 0, width: width, height: height)
drawingView.frame = newFrame
scrollView.setZoomScale(canvas.minimumZoomScale, animated: true)
scrollView.setZoomScale(canvas.defaultZoomScale, animated: true)
centerDocumentView(to: newSize)
let offsetX = (newFrame.width * canvas.minimumZoomScale - view.frame.width) / 2
let offsetY = (newFrame.height * canvas.minimumZoomScale - view.frame.height) / 2
let offsetX = (newFrame.width * canvas.defaultZoomScale - view.frame.width) / 2
let offsetY = (newFrame.height * canvas.defaultZoomScale - view.frame.height) / 2
let point = CGPoint(x: offsetX, y: offsetY)
scrollView.setContentOffset(point, animated: true)
@@ -138,6 +138,20 @@ extension CanvasViewController {
let horizontalPadding = documentViewSize.width < scrollViewSize.width ? (scrollViewSize.width - documentViewSize.width) / 2 : 0
self.scrollView.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
}
func updateDocumentBounds() {
var bounds = scrollView.bounds.muliply(by: drawingView.ratio / scrollView.zoomScale)
let xDelta = bounds.minX * 0.05
let yDelta = bounds.minY * 0.05
bounds.origin.x -= xDelta
bounds.origin.y -= yDelta
bounds.size.width += xDelta * 2
bounds.size.height += yDelta * 2
canvas.bounds = bounds
if canvas.state == .loaded {
canvas.loadStrokes(bounds)
}
}
}
extension CanvasViewController {
@@ -208,6 +222,7 @@ extension CanvasViewController: UIScrollViewDelegate {
}
func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
updateDocumentBounds()
centerDocumentView()
magnificationEnded()
}
@@ -234,6 +249,7 @@ extension CanvasViewController: UIScrollViewDelegate {
}
func scrollViewDidEndScrolling(_ scrollView: UIScrollView) {
updateDocumentBounds()
draggingEnded()
}
}