Merge pull request #57 from dscyrescotti/feature/renderer-optimization

Optimize rendering
This commit is contained in:
Aye Chan
2024-07-04 00:46:50 +08:00
committed by GitHub
2 changed files with 16 additions and 9 deletions

View File

@@ -18,6 +18,8 @@ class DrawingView: UIView {
var ratio: CGFloat { canvas.size.width / bounds.width } var ratio: CGFloat { canvas.size.width / bounds.width }
private var disablesUserInteraction: Bool = false 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) { required init(tool: Tool, canvas: Canvas, history: History) {
self.tool = tool self.tool = tool
@@ -46,7 +48,7 @@ class DrawingView: UIView {
return return
} }
guard let touch = touches.first else { return } guard let touch = touches.first else { return }
let point = touch.location(in: self) let point = touch.preciseLocation(in: self)
touchBegan(at: point) touchBegan(at: point)
} }
@@ -57,21 +59,23 @@ class DrawingView: UIView {
return return
} }
guard let touch = touches.first else { return } guard let touch = touches.first else { return }
let point = touch.location(in: self) if let _touch = event?.coalescedTouches(for: touch)?.last {
touchMoved(to: point) let point = _touch.preciseLocation(in: self)
touchMoved(to: point)
}
} }
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event) super.touchesEnded(touches, with: event)
guard let touch = touches.first else { return } guard let touch = touches.first else { return }
let point = touch.location(in: self) let point = touch.preciseLocation(in: self)
touchEnded(at: point) touchEnded(at: point)
} }
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event) super.touchesCancelled(touches, with: event)
guard let touch = touches.first else { return } guard let touch = touches.first else { return }
let point = touch.location(in: self) let point = touch.preciseLocation(in: self)
touchEnded(at: point) touchEnded(at: point)
} }
@@ -87,6 +91,11 @@ class DrawingView: UIView {
guard !disablesUserInteraction else { return } guard !disablesUserInteraction else { return }
canvas.moveTouch(to: point.muliply(by: ratio)) canvas.moveTouch(to: point.muliply(by: ratio))
if canvas.hasValidStroke { if canvas.hasValidStroke {
let currentTime = CACurrentMediaTime()
if currentTime - lastDrawTime < minDrawInterval {
return
}
lastDrawTime = currentTime
draw() draw()
} }
} }

View File

@@ -19,9 +19,7 @@ struct PenDock: View {
var height: CGFloat { var height: CGFloat {
horizontalSizeClass == .compact ? 90 : 30 horizontalSizeClass == .compact ? 90 : 30
} }
var factor: CGFloat { var factor: CGFloat = 0.9
horizontalSizeClass == .compact ? 0.9 : 0.9
}
@State var refreshScrollId: UUID = UUID() @State var refreshScrollId: UUID = UUID()
@State var opensColorPicker: Bool = false @State var opensColorPicker: Bool = false
@@ -156,7 +154,7 @@ struct PenDock: View {
.resizable() .resizable()
} }
.frame(width: width * factor, height: height * factor) .frame(width: width * factor, height: height * factor)
.padding(.horizontal, 5) .padding(.vertical, 5)
.contentShape(.rect(cornerRadii: .init(topLeading: 10, bottomLeading: 10))) .contentShape(.rect(cornerRadii: .init(topLeading: 10, bottomLeading: 10)))
.onTapGesture { .onTapGesture {
if tool.selectedPen !== pen { if tool.selectedPen !== pen {