- Now it work only for all IPads, because i used UIPopovers. (You can change it, if you want)
- User can open PDFDocuments from native iOS application "Files"
- PDFDocument automatically saved and synchronizes in ICloud.
- User can highlighted annotation for selected area which finger determined.
- If you want delete annotations - turn on "Pencil Mode" and tap to area!
-
I used native PDFKit:
-
For work with documents: UIDocumentPickerViewController:
-
Highlighted text: UITouch and PDFSelection:
- UITouch dont called when PDFDocument is open.
- I solved this problem. (Read the text below)
-
Handle UITouchDelegate:
- Put subview on PDFView when user turn on "Pencil Mode"
- Subiew removed from super view when called method: UITouchEnded
- Use property:
@property (assign, nonatomic) CGPoint startPoint; @property (assign, nonatomic) CGPoint currentMovePoint; @property (assign, nonatomic) CGPoint endPoint; -
In touchesBegan: method we will get the coordinates of the first point.
-
❗️ Dont forget! PDFPage and UIVew - different coordinate systems
- Call this method for convert points from UIView to PDFView page.
[PDFView* view convertPoint:point toPage:self.readerPDF.currentPage] -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.viewForTouch];
self.startPoint = [self.readerPDF convertPoint:point toPage:self.readerPDF.currentPage];
}- In touchesMoved: method we will get the coordinates of the moved point.
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.viewForTouch];
self.endPoint = [self.readerPDF convertPoint:point toPage:self.readerPDF.currentPage];
self.currentMovePoint = [self.readerPDF convertPoint:point toPage:self.readerPDF.currentPage];
PDFSelection* selection = [self.readerPDF.currentPage selectionFromPoint:self.startPoint toPoint:self.currentMovePoint];
NSArray* arraySelections = [selection selectionsByLine];
[self.readerPDF setHighlightedSelections:arraySelections];
[self.readerPDF setCurrentSelection: selection];
}- In touchesEnded: method:
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.viewForTouch];
self.endPoint = [self.readerPDF convertPoint:point toPage:self.readerPDF.currentPage];
PDFSelection* selection = [self.readerPDF.currentPage selectionFromPoint:self.startPoint toPoint:self.endPoint];
NSArray* array = [selection selectionsByLine];
for (PDFSelection* select in array) {
PDFAnnotation* annotation = [[PDFAnnotation alloc] initWithBounds:[select boundsForPage:self.readerPDF.currentPage] forType:PDFAnnotationSubtypeHighlight withProperties:nil];
[self.readerPDF.currentPage addAnnotation:annotation];
}
}
- Well done! After this we get perfect PDFSelection. And add annotation to selections coordinats.

