-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace PencilKit brush stroke view with CALayer-based
- Loading branch information
Showing
4 changed files
with
137 additions
and
168 deletions.
There are no files selected for viewing
85 changes: 0 additions & 85 deletions
85
...les/Legacy/Editing/Sources/Editing View/Workspace/PhotoEditingCanvasBrushStrokeView.swift
This file was deleted.
Oops, something went wrong.
82 changes: 0 additions & 82 deletions
82
Modules/Legacy/Editing/Sources/Editing View/Workspace/PhotoEditingCanvasView.swift
This file was deleted.
Oops, something went wrong.
136 changes: 136 additions & 0 deletions
136
Modules/Legacy/Editing/Sources/Editing View/Workspace/PhotoEditingPathBrushStrokeView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Created by Geoff Pado on 10/31/24. | ||
// Copyright © 2024 Cocoatype, LLC. All rights reserved. | ||
|
||
final class PhotoEditingPathBrushStrokeView: UIControl, PhotoEditingBrushStrokeView { | ||
var color = UIColor.black { | ||
didSet { pathLayer?.strokeColor = color.cgColor } | ||
} | ||
private(set) var currentPath: UIBezierPath? | ||
|
||
init() { | ||
super.init(frame: .zero) | ||
backgroundColor = .clear | ||
isOpaque = false | ||
translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
|
||
override class var layerClass: AnyClass { PathLayer.self } | ||
private var pathLayer: PathLayer? { layer as? PathLayer } | ||
|
||
private static var standardLineWidth = 10.0 | ||
private var lineWidth = PhotoEditingPathBrushStrokeView.standardLineWidth { | ||
didSet { pathLayer?.lineWidth = lineWidth } | ||
} | ||
func updateTool(currentZoomScale: CGFloat) { | ||
lineWidth = Self.standardLineWidth * pow(currentZoomScale, -1.0) | ||
} | ||
|
||
// MARK: Touch Handling | ||
|
||
private var previousPoint: CGPoint? | ||
private var previousEndPoint: CGPoint? | ||
|
||
private func newPath() -> UIBezierPath { | ||
let newPath = UIBezierPath() | ||
newPath.lineCapStyle = .butt | ||
newPath.lineJoinStyle = .bevel | ||
newPath.lineWidth = lineWidth | ||
return newPath | ||
} | ||
|
||
private func clearPath() { | ||
currentPath = nil | ||
previousPoint = nil | ||
previousEndPoint = nil | ||
pathLayer?.path = nil | ||
} | ||
|
||
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
super.touchesBegan(touches, with: event) | ||
guard let touch = touches.first else { return } | ||
|
||
let location = touch.location(in: self) | ||
currentPath = newPath() | ||
currentPath?.move(to: location) | ||
previousPoint = location | ||
|
||
pathLayer?.path = currentPath?.cgPath | ||
} | ||
|
||
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
super.touchesMoved(touches, with: event) | ||
guard let touch = touches.first, | ||
let path = currentPath, | ||
let previousPoint = previousPoint else { return } | ||
|
||
let currentPoint = touch.location(in: self) | ||
|
||
// Smooth the line by using quadratic curves | ||
let midPoint = CGPoint( | ||
x: (previousPoint.x + currentPoint.x) / 2, | ||
y: (previousPoint.y + currentPoint.y) / 2 | ||
) | ||
|
||
if previousEndPoint != nil { | ||
path.addQuadCurve(to: midPoint, controlPoint: previousPoint) | ||
} else { | ||
path.addLine(to: midPoint) | ||
} | ||
|
||
previousEndPoint = midPoint | ||
self.previousPoint = currentPoint | ||
|
||
// Update shape layer with the new path | ||
CATransaction.begin() | ||
CATransaction.setDisableActions(true) | ||
pathLayer?.path = path.cgPath | ||
CATransaction.commit() | ||
} | ||
|
||
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
super.touchesCancelled(touches, with: event) | ||
clearPath() | ||
} | ||
|
||
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | ||
super.touchesEnded(touches, with: event) | ||
guard let touch = touches.first, | ||
let path = currentPath else { return } | ||
|
||
let location = touch.location(in: self) | ||
path.addLine(to: location) | ||
pathLayer?.path = path.cgPath | ||
|
||
sendActions(for: .touchUpInside) | ||
clearPath() | ||
} | ||
|
||
// MARK: Boilerplate | ||
|
||
@available(*, unavailable) | ||
required init(coder: NSCoder) { | ||
let typeName = NSStringFromClass(type(of: self)) | ||
fatalError("\(typeName) does not implement init(coder:)") | ||
} | ||
|
||
private class PathLayer: CAShapeLayer { | ||
override init() { | ||
super.init() | ||
fillColor = nil | ||
strokeColor = UIColor.black.cgColor | ||
lineCap = .butt | ||
lineJoin = .bevel | ||
lineWidth = PhotoEditingPathBrushStrokeView.standardLineWidth | ||
} | ||
|
||
override init(layer: Any) { | ||
super.init(layer: layer) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init(coder: NSCoder) { | ||
let typeName = NSStringFromClass(type(of: self)) | ||
fatalError("\(typeName) does not implement init(coder:)") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters