Hello. My name is HAAAAAN. This time, as the title suggests, I made an app that allows you to scribble on a PDF file. I would like to write it with the meaning of memorization.
Since I am a beginner, I know that there may be incorrect expressions and understandings, but thank you for your understanding.
In addition, this app is just for scribbling, so There is no save function. (Future tasks)
MacOS Xcode swift iPad Pro(iOS:13.6)
① When the scribble is finished, save the PDF file with the scribble
② When ① is completed, convert the image file to a PDF file
-As an image, it feels like taking a screenshot of the screen and converting the image to PDF.
(I don't know if it can be done)
--Open the storyboard and add one View to the original view controller. --Set PDFView to Class name of CustomClass of the added View and make an Outlet connection. --Add the PDF file you want to display to the project file.
qiita.swift
import UIKit
import PDFKit
import PencilKit
class ViewController: UIViewController{
@IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let sourceURL = Bundle.main.url(forResource: "Test", withExtension: "pdf") {
if let source = PDFDocument(url: sourceURL) {
pdfView.document = source //PDF URL
pdfView.autoScales = true //PDF fits on screen
pdfView.displayMode = .singlePage //1 page display
}
}
let canvas = PKCanvasView(frame: view.frame) //Drawing area settings
view.addSubview(canvas) //Add drawing area above PDF
canvas.tool = PKInkingTool(.pen, color: .black, width: 15) //Pen settings
canvas.isOpaque = false //Make the background transparent(Okay)
if let window = UIApplication.shared.windows.first {
if let toolPicker = PKToolPicker.shared(for: window) {
toolPicker.addObserver(canvas)
toolPicker.setVisible(true, forFirstResponder: canvas)
canvas.becomeFirstResponder()
}
}
}
}
・ When implementing View As a configuration, put PDFView on the original view controller and put it on it I was thinking of putting a View for drawing, and I was able to implement it exactly as it was. In an article somewhere while wandering the Web, the screen goes black when drawing with a pen There was a thing that I encountered a problem, and I had the exact same symptom on the way.
Probably some processing is moving by the trigger at the time of gesture, I think When I changed the background color, the screen that should be black became the color set as the background color. I decided to make the View itself transparent.
At first, I set the alpha value for the background color of the View for drawing, but I stopped it By setting the isOpaque value of View to false, the problem at the time of gesture has disappeared.
- [Swift] Display PDF with PDFKit - Introducing PencilKit with just 3 lines of code to support Apple Pencil - [Swift] How to use isOpaque to make windows transparent
This time I just combined the above articles! (Don!!!)
Recommended Posts