-
Notifications
You must be signed in to change notification settings - Fork 103
/
PDFViewController.swift
374 lines (298 loc) · 13.1 KB
/
PDFViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// PDFViewController.swift
// Pods
//
// Created by Chris Anderson on 5/7/16.
//
//
import UIKit
import SafariServices
open class PDFViewController: UIViewController {
/// A boolean value that determines whether the navigation bar and scrubber bar hide on screen tap
open var hidesBarsOnTap: Bool = true
/// A boolean value that determines if the scrubber bar should be visible
open var showsScrubber: Bool = true {
didSet {
guard isViewLoaded else {
return
}
pageScrubber.isHidden = !showsScrubber
}
}
/// A boolean value that determines if a PDF should have fillable form elements
open var allowsFormFilling: Bool = true
/// A boolean value that determines if annotations are allowed
open var allowsAnnotations: Bool = true
/// A boolean value that determines if sharing should be allowed
open var allowsSharing: Bool = true
/// A boolean value that determines if view controller is displayed as modal
open var isPresentingInModal: Bool = false
/// The scroll direction of the reader
open var scrollDirection: UICollectionViewScrollDirection = .horizontal
/// A reference to the document that is being displayed
var document: PDFDocument!
/// A reference to the share button
var shareBarButtonItem: UIBarButtonItem?
/// A closure that defines an action to take upon selecting the share button.
/// The default action brings up a UIActivityViewController
open lazy var shareBarButtonAction: () -> () = { self.showActivitySheet() }
/// A reference to the collection view handling page presentation
var collectionView: PDFSinglePageViewer!
/// A reference to the page scrubber bar
var pageScrubber: PDFPageScrubber!
private(set) open lazy var formController: PDFFormViewController = PDFFormViewController(document: self.document)
private(set) open lazy var annotationController: PDFAnnotationController = PDFAnnotationController(document: self.document, delegate: self)
fileprivate var showingAnnotations = false
fileprivate var showingFormFilling = true
/**
Initializes a new reader with a given document
- Parameters:
- document: The document to display
- Returns: An instance of the PDFViewController
*/
public init(document: PDFDocument) {
super.init(nibName: nil, bundle: nil)
self.document = document
}
/**
Initializes a new reader with a given document and annotation controller
- Parameters:
- document: The document to display
- annotationController: The controller to supervise annotations
- Returns: An instance of the PDFViewController
*/
public convenience init(document: PDFDocument, annotationController: PDFAnnotationController) {
self.init(document: document)
self.annotationController = PDFAnnotationController(document: self.document, delegate: self)
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override open func viewDidLoad() {
super.viewDidLoad()
pageScrubber = PDFPageScrubber(frame: CGRect(x: 0, y: view.frame.size.height - bottomLayoutGuide.length, width: view.frame.size.width, height: 44.0), document: document)
pageScrubber.scrubberDelegate = self
pageScrubber.translatesAutoresizingMaskIntoConstraints = false
collectionView = PDFSinglePageViewer(frame: view.bounds, document: document)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.singlePageDelegate = self
let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
flowLayout.scrollDirection = scrollDirection
switch scrollDirection {
case .horizontal:
collectionView.isPagingEnabled = true
pageScrubber.isHidden = !showsScrubber
case .vertical:
collectionView.isPagingEnabled = false
pageScrubber.isHidden = true
}
self.setupUI()
collectionView.reloadItems(at: [IndexPath(row: 0, section: 0)])
}
fileprivate func setupUI() {
view.addSubview(collectionView)
view.addSubview(pageScrubber)
view.addSubview(annotationController.view)
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
pageScrubber.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
pageScrubber.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
pageScrubber.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
pageScrubber.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
pageScrubber.sizeToFit()
reloadBarButtons()
}
open override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
collectionView.collectionViewLayout.invalidateLayout()
}
open override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
view.layoutSubviews()
}
open override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.annotationController.finishAnnotation()
self.document.annotations = self.annotationController.annotations
self.document.save()
}
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: { (context) in
self.collectionView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.bottomLayoutGuide.length, 0)
self.collectionView.collectionViewLayout.invalidateLayout()
self.pageScrubber.sizeToFit()
}, completion: { (context) in
self.collectionView.displayPage(self.document.currentPage, animated: false)
})
}
//MARK: - Private helpers
fileprivate func scrollTo(page: Int) {
document.currentPage = page
collectionView.displayPage(page, animated: false)
if showsScrubber {
pageScrubber.updateScrubber()
}
}
fileprivate func reloadBarButtons() {
navigationItem.rightBarButtonItems = rightBarButtons()
if isPresentingInModal {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Done",
style: .plain,
target: self,
action: #selector(PDFViewController.dismissModal))
}
}
open func rightBarButtons() -> [UIBarButtonItem] {
var buttons: [UIBarButtonItem] = []
if allowsSharing {
let shareFormBarButtonItem = PDFBarButton(
image: UIImage.bundledImage("share"),
toggled: false,
target: self,
action: #selector(PDFViewController.shareDocument)
)
buttons.append(shareFormBarButtonItem)
self.shareBarButtonItem = shareFormBarButtonItem
}
buttons.append(PDFBarButton(
image: UIImage.bundledImage("thumbs"),
toggled: false,
target: self,
action: #selector(PDFViewController.showThumbnailView)
)
)
if allowsAnnotations {
if showingAnnotations {
buttons.append(annotationController.undoButton)
for button in annotationController.buttons.reversed() {
buttons.append(button)
}
}
buttons.append(PDFBarButton(
image: UIImage.bundledImage("annot"),
toggled: showingAnnotations,
target: self,
action: #selector(PDFViewController.toggleAnnotations(_:))
)
)
}
return buttons
}
func toggleAnnotations(_ button: PDFBarButton) {
showingAnnotations = !showingAnnotations
reloadBarButtons()
}
func showActivitySheet() {
let renderer = PDFRenderController(document: document, controllers: [
annotationController,
formController
])
let pdf = renderer.renderOntoPDF()
let items = [pdf]
let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityVC.modalPresentationStyle = .popover
let popController = activityVC.popoverPresentationController
popController?.barButtonItem = shareBarButtonItem
popController?.permittedArrowDirections = .up
}
present(activityVC, animated: true, completion: nil)
}
func showThumbnailView() {
let vc = PDFThumbnailViewController(document: document)
vc.delegate = self
let nvc = UINavigationController(rootViewController: vc)
nvc.modalTransitionStyle = .crossDissolve
present(nvc, animated: true, completion: nil)
}
func hideBars(state: Bool) {
navigationController?.setNavigationBarHidden(state, animated: true)
switch scrollDirection {
case .horizontal:
if showsScrubber {
pageScrubber.isHidden = state
}
else {
pageScrubber.isHidden = true
}
case .vertical:
pageScrubber.isHidden = true
}
}
/// Toggles the display of the navigation bar and scrubber bar
func toggleBars() {
collectionView.collectionViewLayout.invalidateLayout()
hideBars(state: !(navigationController?.isNavigationBarHidden ?? false))
}
//MARK: - IBActions
func handleTap(_ gestureRecognizer: UIGestureRecognizer) {
self.toggleBars()
}
func shareDocument() {
self.shareBarButtonAction()
}
func dismissModal() {
dismiss(animated: true, completion: nil)
}
}
extension PDFViewController: PDFAnnotationControllerProtocol {
public func annotationWillStart(touch: UITouch) -> Int? {
let tapPoint = touch.location(in: collectionView)
guard let pageIndex = collectionView.indexPathForItem(at: tapPoint)?.row else { return nil }
return pageIndex + 1
}
}
extension PDFViewController: PDFPageScrubberDelegate {
public func scrubber(_ scrubber: PDFPageScrubber, selectedPage: Int) {
self.scrollTo(page: selectedPage)
}
}
extension PDFViewController: PDFSinglePageViewerDelegate {
public func singlePageViewer(_ collectionView: PDFSinglePageViewer, didDisplayPage page: Int) {
document.currentPage = page
if showsScrubber {
pageScrubber.updateScrubber()
}
}
public func singlePageViewer(_ collectionView: PDFSinglePageViewer, loadedContent content: PDFPageContentView) {
// if allowsFormFilling {
// formController.showForm(content)
// }
if allowsAnnotations {
annotationController.showAnnotations(content)
}
}
public func singlePageViewer(_ collectionView: PDFSinglePageViewer, selected action: PDFAction) {
if let action = action as? PDFActionURL {
let svc = SFSafariViewController(url: action.url as URL)
present(svc, animated: true, completion: nil)
} else if let action = action as? PDFActionGoTo {
collectionView.displayPage(action.pageIndex, animated: true)
}
}
public func singlePageViewer(_ collectionView: PDFSinglePageViewer, selected annotation: PDFAnnotationView) {
if let annotation = annotation.parent {
annotationController.select(annotation: annotation)
}
}
public func singlePageViewer(_ collectionView: PDFSinglePageViewer, tapped recognizer: UITapGestureRecognizer) {
if hidesBarsOnTap {
handleTap(recognizer)
}
annotationController.select(annotation: nil)
}
public func singlePageViewerDidBeginDragging() {
self.hideBars(state: true)
}
public func singlePageViewerDidEndDragging() { }
}
extension PDFViewController: PDFThumbnailViewControllerDelegate {
public func thumbnailCollection(_ collection: PDFThumbnailViewController, didSelect page: Int) {
self.scrollTo(page: page)
self.dismiss(animated: true, completion: nil)
}
}