Verified Commit 49978887 authored by Geoff Pado's avatar Geoff Pado
Browse files

Add pencil interactions

parent 9a32f209
......@@ -98,6 +98,19 @@ open class BasePhotoEditingViewController: UIViewController, UIScrollViewDelegat
updateToolbarItems()
}
@objc func handlePencilInteraction(_ sender: Any, event: PencilInteractionEvent) {
switch UIPencilInteraction.preferredTapAction {
case .ignore: break
case .switchEraser: self.selectEraser()
case .switchPrevious: self.toggleHighlighterTool()
case .showColorPalette:
if #available(iOS 14, *) {
self.showColorPicker(sender)
}
@unknown default: break
}
}
private func updateToolbarItems(animated: Bool = true) {
let undoToolItem = UIBarButtonItem(image: Icons.undo, style: .plain, target: self, action: #selector(BasePhotoEditingViewController.undo))
undoToolItem.isEnabled = undoManager?.canUndo ?? false
......@@ -127,6 +140,10 @@ open class BasePhotoEditingViewController: UIViewController, UIScrollViewDelegat
@available(iOS 14.0, *)
@objc public func showColorPicker(_ sender: Any) {
guard (presentedViewController is ColorPickerViewController) == false else {
return dismiss(animated: true)
}
if traitCollection.userInterfaceIdiom == .mac {
ColorPanel.shared.makeKeyAndOrderFront(sender)
} else if let toolbarItem = toolbarItems?.first(where: { $0.action == #selector(showColorPicker(_:)) }) {
......
......@@ -3,7 +3,7 @@
import PencilKit
@objc class PhotoEditingCanvasView: PKCanvasView {
@objc class PhotoEditingCanvasView: PKCanvasView, UIPencilInteractionDelegate {
init() {
super.init(frame: .zero)
accessibilityIgnoresInvertColors = true
......@@ -13,6 +13,8 @@ import PencilKit
overrideUserInterfaceStyle = .light
tool = tool(forZoomScale: 1)
translatesAutoresizingMaskIntoConstraints = false
pencilInteractionHandler.installInteraction()
}
@objc var color = UIColor.black {
......@@ -68,12 +70,45 @@ import PencilKit
private static let standardLineWidth = CGFloat(10)
private var brushStrokeView: PhotoEditingCanvasBrushStrokeView? {
self.superview as? PhotoEditingCanvasBrushStrokeView
}
private var brushStrokeView: PhotoEditingCanvasBrushStrokeView? { self.superview as? PhotoEditingCanvasBrushStrokeView }
private lazy var pencilInteractionHandler = PencilInteractionHandler(self)
@available(*, unavailable)
required init(coder: NSCoder) {
fatalError("\(String(describing: type(of: Self.self))) does not implement init(coder:)")
}
}
class PencilInteractionHandler: NSObject, UIPencilInteractionDelegate {
init(_ view: UIView) {
self.view = view
super.init()
interaction.delegate = self
}
func installInteraction() {
view?.addInteraction(interaction)
}
func pencilInteractionDidTap(_ interaction: UIPencilInteraction) {
let event = PencilInteractionEvent(interaction: interaction)
guard let responder = view else { return }
let selector = #selector(BasePhotoEditingViewController.handlePencilInteraction(_:event:))
let actionTarget = responder.target(forAction: selector, withSender: responder) as? UIResponder
actionTarget?.perform(selector, with: responder, with: event)
}
private let interaction = UIPencilInteraction()
private weak var view: UIView?
}
class PencilInteractionEvent: UIEvent {
init(interaction: UIPencilInteraction) {
self.interaction = interaction
super.init()
}
let interaction: UIPencilInteraction
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment