瀏覽代碼

PDFEdit(iOS) - 编辑模块高保真处理

chenyu 1 年之前
父節點
當前提交
1cb56e951a

+ 25 - 0
Edit-Ctrl-Demo/Edit-Ctrl-Demo/Assets.xcassets/CPDFThunbnailImageEnter.imageset/Contents.json

@@ -0,0 +1,25 @@
+{
+  "images" : [
+    {
+      "idiom" : "universal",
+      "scale" : "1x"
+    },
+    {
+      "filename" : "thumbnail@2x.png",
+      "idiom" : "universal",
+      "scale" : "2x"
+    },
+    {
+      "filename" : "thumbnail@3x.png",
+      "idiom" : "universal",
+      "scale" : "3x"
+    }
+  ],
+  "info" : {
+    "author" : "xcode",
+    "version" : 1
+  },
+  "properties" : {
+    "template-rendering-intent" : "original"
+  }
+}

二進制
Edit-Ctrl-Demo/Edit-Ctrl-Demo/Assets.xcassets/CPDFThunbnailImageEnter.imageset/thumbnail@2x.png


二進制
Edit-Ctrl-Demo/Edit-Ctrl-Demo/Assets.xcassets/CPDFThunbnailImageEnter.imageset/thumbnail@3x.png


+ 277 - 6
Edit-Ctrl-Demo/Edit-Ctrl-Demo/CPDFViewController.m

@@ -15,7 +15,7 @@
 #import <ComPDFKit/ComPDFKit.h>
 #import <compdfkit_tools/compdfkit_tools.h>
 
-@interface CPDFViewController () <UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate,CPDFEditToolBarDelegate, CPDFToolsViewControllerDelegate>
+@interface CPDFViewController () <UISearchBarDelegate,CPDFViewDelegate,CPDFListViewDelegate, CPDFMoreListViewDelegate, CSearchToolbarDelegate, CPDFDisplayViewDelegate, CPDFBOTAViewControllerDelegate,CPDFSearchResultsDelegate, CPDFThumbnailViewControllerDelegate,CPDFPopMenuViewDelegate,UIDocumentPickerDelegate,CPDFToolsViewControllerDelegate>
 
 @property(nonatomic, strong) NSString *filePath;
 
@@ -33,6 +33,14 @@
 
 @property(nonatomic, strong) CNavigationBarTitleButton * titleButton;
 
+@property(nonatomic, strong) CNavigationRightView *rightView;
+
+@property(nonatomic, strong) CPDFPopMenu *popMenu;
+
+@property(nonatomic, strong) CSearchToolbar *searchToolbar;
+
+@property(nonatomic, strong) UIBarButtonItem * leftBarButtonItem;
+
 @end
 
 @implementation CPDFViewController
@@ -68,11 +76,84 @@
     self.toolBar.delegate = self;
     [self.view addSubview:self.toolBar];
     
-    //default  beginEditingMode
+    
+    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"CPDFThunbnailImageEnter" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] style:UIBarButtonItemStylePlain target:self action:@selector(buttonItemClicked_thumbnail:)];
+    self.leftBarButtonItem = leftItem;
+    self.navigationItem.leftBarButtonItem = self.leftBarButtonItem;
+    
+    __block typeof(self) blockSelf = self;
+    
+    self.rightView = [[CNavigationRightView alloc] initWithDefaultItemsClickBack:^(NSUInteger tag) {
+        switch (tag) {
+            case CNavigationRightTypeSearch:
+                [blockSelf navigationRightItemSearch];
+                break;
+            case CNavigationRightTypeBota:
+                [blockSelf navigationRightItemBota];
+                break;
+            default:
+            case CNavigationRightTypeMore:
+                [blockSelf navigationRightItemMore];
+                break;
+        }
+    }];
+    
+    self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
+    self.searchToolbar.delegate = self;
+    
     [self.pdfListView beginEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
+    [self enterEditMode];
+}
+
+#pragma mark - Action
+
+- (void)navigationRightItemSearch {
+    [self.searchToolbar showInView:self.navigationController.navigationBar];
+    self.title = nil;
+    self.navigationItem.rightBarButtonItem = nil;
+}
+
+- (void)navigationRightItemBota {
+    CPDFBOTAViewController *botaViewController = [[CPDFBOTAViewController alloc] initWithPDFView:self.pdfListView];
+    botaViewController.delegate = self;
+    
+    AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
+   
+    presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:botaViewController presentingViewController:self];
+    botaViewController.transitioningDelegate = presentationController;
+    
+    [self presentViewController:botaViewController animated:YES completion:nil];
     
 }
 
+- (void)navigationRightItemMore {
+    CPDFPopMenuView * menuView = [[CPDFPopMenuView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
+    menuView.delegate = self;
+    self.popMenu = [CPDFPopMenu popMenuWithContentView:menuView];
+    self.popMenu.dimCoverLayer = YES;
+    if (@available(iOS 11.0, *)) {
+        [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - self.view.safeAreaInsets.right - 250, self.view.safeAreaInsets.top - 10, 250, 200)];
+    } else {
+        // Fallback on earlier versions
+        [self.popMenu showMenuInRect:CGRectMake(self.view.frame.size.width - 250, -10, 250, 200)];
+    }
+}
+
+- (void)buttonItemClicked_thumbnail:(id)sender {
+    CPDFThumbnailViewController *thumbnailViewController = [[CPDFThumbnailViewController alloc] initWithPDFView:self.pdfListView];
+    thumbnailViewController.delegate = self;
+    [self.navigationController pushViewController:thumbnailViewController animated:NO];
+}
+
+- (void)menuItemClick_CopyAction:(id)sender {
+    if (self.pdfListView.currentSelection.string)
+        [[UIPasteboard generalPasteboard] setString:self.pdfListView.currentSelection.string];
+    
+    [self.pdfListView clearSelection];
+}
+
+
+
 - (void) titleButtonClickd:(UIButton *) button {
     CPDFToolsViewController * toolsVc = [[CPDFToolsViewController alloc] init];
     toolsVc.delegate = self;
@@ -88,11 +169,11 @@
     if (@available(iOS 11.0, *)) {
         self.pdfListView.frame = CGRectMake(self.view.safeAreaInsets.left, self.view.safeAreaInsets.top, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height - self.view.safeAreaInsets.bottom- self.view.safeAreaInsets.top);
         
-        self.toolBar.frame = CGRectMake(0, self.view.frame.size.height - self.view.safeAreaInsets.bottom - 60, self.view.frame.size.width, 60);
+        self.toolBar.frame = CGRectMake(0, self.view.frame.size.height - self.view.safeAreaInsets.bottom - 44, self.view.frame.size.width, 44);
         
     } else {
         self.pdfListView.frame = self.view.bounds;
-        self.toolBar.frame = CGRectMake(0, self.view.frame.size.height  - 84, self.view.frame.size.width, 60);
+        self.toolBar.frame = CGRectMake(0, self.view.frame.size.height  - 68, self.view.frame.size.width, 44);
     }
 }
 
@@ -154,11 +235,11 @@
     });
 }
 
-#pragma mark - Action
-
 #pragma mark - CPDFViewDelegate
 
 - (void)PDFViewDocumentDidLoaded:(CPDFView *)pdfView {
+    UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
+    self.navigationItem.rightBarButtonItem = rightItem;
 }
 
 - (void)PDFViewCurrentPageDidChanged:(CPDFView *)pdfView {
@@ -191,8 +272,10 @@
 - (void)CPDFToolsViewControllerDismiss:(CPDFToolsViewController *)viewController selectItemAtIndex:(NSUInteger)selectIndex {
     if(selectIndex == 0) {
         //viewwer
+        [self enterViewerMode];
     }else{
         //edit
+        [self enterEditMode];
     }
 }
 
@@ -248,4 +331,192 @@
     }
 }
 
+#pragma mark - CSearchToolbarDelegate
+
+- (void)searchToolbar:(CSearchToolbar *)searchToolbar onSearchQueryResults:(NSArray *)results {
+    if ([results count] < 1) {
+        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
+                                                               style:UIAlertActionStyleCancel
+                                                             handler:nil];
+        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
+                                                                       message:NSLocalizedString(@"your have‘t search result", nil)
+                                                                preferredStyle:UIAlertControllerStyleAlert];
+        [alert addAction:cancelAction];
+        [self presentViewController:alert animated:YES completion:nil];
+        return;
+    }
+    [self searchToolbarOnExitSearch:searchToolbar];
+    
+    CPDFSearchResultsViewController* searchResultController = [[CPDFSearchResultsViewController alloc] initWithResultArray:results keyword:searchToolbar.searchKeyString document:self.pdfListView.document];
+    searchResultController.delegate = self;
+    [self.navigationController pushViewController:searchResultController animated:YES];
+}
+
+- (void)searchToolbarOnExitSearch:(CSearchToolbar *)searchToolbar {
+    if([searchToolbar superview]) {
+        [searchToolbar removeFromSuperview];
+        UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightView];
+        self.navigationItem.rightBarButtonItem = rightItem;
+        self.title = self.navigationTitle;
+    }
+}
+
+#pragma mark - CPDFDisplayViewDelegate
+
+- (void)displayViewControllerDismiss:(CPDFDisplayViewController *)displayViewController {
+    [self.navigationController popToRootViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFBOTAViewControllerDelegate
+
+- (void)botaViewControllerDismiss:(CPDFBOTAViewController *)botaViewController {
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFSearchResultsDelegate
+
+- (void)searchResultsView:(CPDFSearchResultsViewController *)resultVC forSelection:(CPDFSelection *)selection indexPath:(NSIndexPath *)indexPath {
+    [self.navigationController popViewControllerAnimated:YES];
+    
+    NSInteger pageIndex = [self.pdfListView.document indexForPage:selection.page];
+    [self.pdfListView goToPageIndex:pageIndex animated:NO];
+    [self.pdfListView setHighlightedSelection:selection animated:YES];
+}
+
+-(void)searchResultsViewControllerDismiss:(CPDFSearchResultsViewController *)searchResultsViewController {
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFThumbnailViewControllerDelegate
+
+- (void)thumbnailViewController:(CPDFThumbnailViewController *)thumbnailViewController pageIndex:(NSInteger)pageIndex {
+    [self.pdfListView goToPageIndex:pageIndex animated:NO];
+    [self.navigationController popViewControllerAnimated:YES];
+}
+
+#pragma mark - CPDFMenuViewdelegate
+- (void)menuDidClickAtView:(CPDFPopMenuView *)view clickType:(CPDFPopMenuViewType)viewType {
+    switch (viewType) {
+        case CPDFPopMenuViewTypeSetting:
+            //display controller
+        {
+            [self.popMenu hideMenu];
+            CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
+            displayVc.delegate = self;
+            
+            
+            AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
+            presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:displayVc presentingViewController:self];
+            displayVc.transitioningDelegate = presentationController;
+            [self presentViewController:displayVc animated:YES completion:nil];
+        }
+            
+            break;
+            
+        case CPDFPopMenuViewTypeInfo:
+        {
+            [self.popMenu hideMenu];
+            CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
+            
+            AAPLCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
+            presentationController = [[AAPLCustomPresentationController alloc] initWithPresentedViewController:infoVc presentingViewController:self];
+            infoVc.transitioningDelegate = presentationController;
+            [self presentViewController:infoVc animated:YES completion:nil];
+            
+        }
+         
+            break;
+            
+        case CPDFPopMenuViewTypeShare:
+        {
+            [self.popMenu hideMenu];
+            if (self.pdfListView.isEdited) {
+                dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+                    [self.pdfListView commitEditing];
+                    dispatch_async(dispatch_get_main_queue(), ^{
+                        [self.pdfListView endOfEditing];
+                        [self shareAction];
+                    });
+                });
+            } else {
+                [self.pdfListView endOfEditing];
+                [self shareAction];
+            }
+            
+        }
+            break;
+            
+        case CPDFPopMenuViewTypeAddFile:
+        {
+            [self.popMenu hideMenu];
+            NSArray *documentTypes = @[@"com.adobe.pdf"];
+            UIDocumentPickerViewController *documentPickerViewController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:documentTypes inMode:UIDocumentPickerModeOpen];
+                    documentPickerViewController.delegate = self;
+            [self presentViewController:documentPickerViewController animated:YES completion:nil];
+        }
+            
+        default:
+            break;
+    }
+}
+
+- (void)shareAction {
+    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:@[self.pdfListView.document.documentURL] applicationActivities:nil];
+        activityVC.definesPresentationContext = YES;
+        [self presentViewController:activityVC animated:YES completion:nil];
+        activityVC.completionWithItemsHandler = ^(UIActivityType  _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) {
+
+            if (completed) {
+                NSLog(@"Success!");
+            } else {
+                NSLog(@"Failed Or Canceled!");
+            }
+        };
+}
+
+#pragma mark - UIDocument Picker
+- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
+    
+    __weak typeof(self) weakSelf = self;
+    BOOL canAccessingResource = [url startAccessingSecurityScopedResource];
+       if(canAccessingResource) {
+           NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
+           NSError *error;
+           [fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
+               [weakSelf reloadDocumentWithFilePath:newURL.path completion:^(BOOL result) {
+                   
+               }];
+           }];
+           if (error) {
+               
+           }
+       } else {
+           // startAccessingSecurityScopedResource fail
+       }
+       [url stopAccessingSecurityScopedResource];
+    
+    
+}
+
+- (void)enterEditMode {
+    self.rightView.hidden = YES;
+    self.navigationItem.leftBarButtonItem = nil;
+    [self.pdfListView changeEditingLoadType:CEditingLoadTypeText | CEditingLoadTypeImage];
+}
+
+- (void)enterViewerMode {
+    self.rightView.hidden = NO;
+    self.navigationItem.leftBarButtonItem = self.leftBarButtonItem;
+    if (self.pdfListView.isEdited) {
+        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+            [self.pdfListView commitEditing];
+            dispatch_async(dispatch_get_main_queue(), ^{
+                [self.pdfListView endOfEditing];
+            });
+        });
+    } else {
+        [self.pdfListView endOfEditing];
+    }
+}
+
 @end

+ 0 - 30
viewer-ctrl-demo/viewer-ctrl-demo/CPDFViewController.m

@@ -77,17 +77,6 @@
         }
     }];
     
-    //titleButton
-    CNavigationBarTitleButton * navTitleButton = [[CNavigationBarTitleButton alloc] init];
-    CGRect frame = navTitleButton.frame;
-    frame.size.width = 35;
-    navTitleButton.frame = frame;
-    self.titleButton = navTitleButton;
-    self.navigationItem.titleView = navTitleButton;
-    
-    [navTitleButton setImage:[UIImage imageNamed:@"syasarrow"] forState:UIControlStateNormal];
-    [navTitleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];
-    
     self.searchToolbar = [[CSearchToolbar alloc] initWithPDFView:self.pdfListView];
     self.searchToolbar.delegate = self;
     
@@ -96,10 +85,6 @@
     }];
     
     
-}
-
-- (void) titleButtonClickd:(UIButton *) button {
-  
 }
 
 - (void)viewWillLayoutSubviews {
@@ -256,21 +241,6 @@
     }
 }
 
-
-#pragma mark - CPDFMorelistViewDelegate
-
-- (void)PDFMoreListViewController:(CPDFMoreListViewController *)moreVC didSelectRow:(CPDFMoreListViewType)row {
-    if(CPDFMoreListViewTypeInfo == row){
-        //File Info
-        CPDFInfoViewController * infoVc = [[CPDFInfoViewController alloc] initWithPDFView:self.pdfListView];
-        [self.navigationController pushViewController:infoVc animated:YES];
-    } else {
-        CPDFDisplayViewController *displayVc = [[CPDFDisplayViewController alloc] initWithPDFView:self.pdfListView];
-        displayVc.delegate = self;
-        [self.navigationController pushViewController:displayVc animated:NO];
-    }
-}
-
 #pragma mark - CSearchToolbarDelegate
 
 - (void)searchToolbar:(CSearchToolbar *)searchToolbar onSearchQueryResults:(NSArray *)results {