From 687fd9be55b6a948a5c15e5da1897848b7877f33 Mon Sep 17 00:00:00 2001 From: dscyrescotti Date: Wed, 3 Jul 2024 18:47:11 +0700 Subject: [PATCH 1/2] feat: use throttle to reduce frame rate --- .../View/Bridge/Views/DrawingView.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Memola/Canvas/View/Bridge/Views/DrawingView.swift b/Memola/Canvas/View/Bridge/Views/DrawingView.swift index 74ff29d..2e98577 100644 --- a/Memola/Canvas/View/Bridge/Views/DrawingView.swift +++ b/Memola/Canvas/View/Bridge/Views/DrawingView.swift @@ -18,6 +18,8 @@ class DrawingView: UIView { var ratio: CGFloat { canvas.size.width / bounds.width } private var disablesUserInteraction: Bool = false + private var lastDrawTime: CFTimeInterval = 0 + private let minDrawInterval: CFTimeInterval = 1.0 / 60.0 required init(tool: Tool, canvas: Canvas, history: History) { self.tool = tool @@ -46,7 +48,7 @@ class DrawingView: UIView { return } guard let touch = touches.first else { return } - let point = touch.location(in: self) + let point = touch.preciseLocation(in: self) touchBegan(at: point) } @@ -57,21 +59,23 @@ class DrawingView: UIView { return } guard let touch = touches.first else { return } - let point = touch.location(in: self) - touchMoved(to: point) + if let _touch = event?.coalescedTouches(for: touch)?.last { + let point = _touch.preciseLocation(in: self) + touchMoved(to: point) + } } override func touchesEnded(_ touches: Set, with event: UIEvent?) { super.touchesEnded(touches, with: event) guard let touch = touches.first else { return } - let point = touch.location(in: self) + let point = touch.preciseLocation(in: self) touchEnded(at: point) } override func touchesCancelled(_ touches: Set, with event: UIEvent?) { super.touchesCancelled(touches, with: event) guard let touch = touches.first else { return } - let point = touch.location(in: self) + let point = touch.preciseLocation(in: self) touchEnded(at: point) } @@ -87,6 +91,11 @@ class DrawingView: UIView { guard !disablesUserInteraction else { return } canvas.moveTouch(to: point.muliply(by: ratio)) if canvas.hasValidStroke { + let currentTime = CACurrentMediaTime() + if currentTime - lastDrawTime < minDrawInterval { + return + } + lastDrawTime = currentTime draw() } } From 8fa17a358e8ae8a6d6562565eca1153a198c9233 Mon Sep 17 00:00:00 2001 From: dscyrescotti Date: Wed, 3 Jul 2024 18:54:15 +0700 Subject: [PATCH 2/2] chore: correct padding --- Memola/Features/Memo/PenDock/PenDock.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Memola/Features/Memo/PenDock/PenDock.swift b/Memola/Features/Memo/PenDock/PenDock.swift index 8624221..f053c3a 100644 --- a/Memola/Features/Memo/PenDock/PenDock.swift +++ b/Memola/Features/Memo/PenDock/PenDock.swift @@ -19,9 +19,7 @@ struct PenDock: View { var height: CGFloat { horizontalSizeClass == .compact ? 90 : 30 } - var factor: CGFloat { - horizontalSizeClass == .compact ? 0.9 : 0.9 - } + var factor: CGFloat = 0.9 @State var refreshScrollId: UUID = UUID() @State var opensColorPicker: Bool = false @@ -156,7 +154,7 @@ struct PenDock: View { .resizable() } .frame(width: width * factor, height: height * factor) - .padding(.horizontal, 5) + .padding(.vertical, 5) .contentShape(.rect(cornerRadii: .init(topLeading: 10, bottomLeading: 10))) .onTapGesture { if tool.selectedPen !== pen {