CPDFBOTAViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // CPDFBOTAViewController.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFBOTAViewController.h"
  13. #import "CPDFThumbnailViewController.h"
  14. #import "CPDFOutlineViewController.h"
  15. #import "CPDFBookmarkViewController.h"
  16. #import "CPDFAnnotationViewController.h"
  17. #import "CPDFColorUtils.h"
  18. #import <ComPDFKit/ComPDFKit.h>
  19. @interface CPDFBOTAViewController () <CPDFOutlineViewControllerDelegate, CPDFBookmarkViewControllerDelegate,CPDFAnnotationViewControllerDelegate>
  20. @property (nonatomic, assign) NSInteger pageIndex;
  21. @property (nonatomic, strong) UISegmentedControl *segmentedControl;
  22. @property (nonatomic, strong) UIViewController *currentViewController;
  23. @property (nonatomic, strong) CPDFOutlineViewController *outlineViewController;
  24. @property (nonatomic, strong) CPDFBookmarkViewController *bookmarkViewController;
  25. @property (nonatomic, strong) CPDFAnnotationViewController *annotationViewController;
  26. @property (nonatomic, strong) UIButton *doneBtn;
  27. @property (nonatomic, strong) NSArray *segmmentArray;
  28. @property (nonatomic, assign) CPDFBOTATypeState type;
  29. @end
  30. @implementation CPDFBOTAViewController
  31. #pragma mark - Initializers
  32. - (instancetype)initWithPDFView:(CPDFView *)pdfView {
  33. if (self = [super init]) {
  34. _pdfView = pdfView;
  35. self.segmmentArray = @[@(CPDFBOTATypeStateOutline),@(CPDFBOTATypeStateBookmark)];
  36. }
  37. return self;
  38. }
  39. - (instancetype)initCustomizeWithPDFView:(CPDFView *)pdfView navArrays:(NSArray *)botaTypes {
  40. if (self = [super init]) {
  41. _pdfView = pdfView;
  42. self.segmmentArray = botaTypes;
  43. }
  44. return self;
  45. }
  46. #pragma mark - UIViewController Methods
  47. - (void)viewDidLoad {
  48. [super viewDidLoad];
  49. // Do any additional setup after loading the view.
  50. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  51. NSMutableArray *segmmentTitleArray = [NSMutableArray array];
  52. for (NSNumber *num in self.segmmentArray) {
  53. if(CPDFBOTATypeStateOutline == num.integerValue) {
  54. [segmmentTitleArray addObject:NSLocalizedString(@"Outlines", nil)];
  55. self.outlineViewController = [[CPDFOutlineViewController alloc] initWithPDFView:self.pdfView];
  56. self.outlineViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
  57. self.outlineViewController.delegate = self;
  58. [self addChildViewController:self.outlineViewController];
  59. } else if(CPDFBOTATypeStateBookmark == num.integerValue) {
  60. [segmmentTitleArray addObject:NSLocalizedString(@"Bookmarks", nil)];
  61. _bookmarkViewController = [[CPDFBookmarkViewController alloc] initWithPDFView:self.pdfView];
  62. _bookmarkViewController.delegate = self;
  63. _bookmarkViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
  64. [self addChildViewController:_bookmarkViewController];
  65. } else if(CPDFBOTATypeStateAnnotation == num.integerValue) {
  66. [segmmentTitleArray addObject:NSLocalizedString(@"Annotations", nil)];
  67. _annotationViewController = [[CPDFAnnotationViewController alloc] initWithPDFView:self.pdfView];
  68. _annotationViewController.delegate = self;
  69. _annotationViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin;
  70. [self addChildViewController:_annotationViewController];
  71. }
  72. }
  73. _segmentedControl = [[UISegmentedControl alloc] initWithItems:segmmentTitleArray];
  74. _segmentedControl.selectedSegmentIndex = 0;
  75. _segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  76. [_segmentedControl addTarget:self action:@selector(segmentedControlValueChanged_BOTA:) forControlEvents:UIControlEventValueChanged];
  77. self.navigationItem.titleView = self.segmentedControl;
  78. [self.view addSubview:self.segmentedControl];
  79. // self.currentViewController = self.outlineViewController;
  80. [self.view addSubview:self.outlineViewController.view];
  81. self.doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  82. self.doneBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  83. [self.doneBtn setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
  84. [self.doneBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  85. [self.view addSubview:self.doneBtn];
  86. self.view.backgroundColor = [CPDFColorUtils CPDFViewControllerBackgroundColor];
  87. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  88. }
  89. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  90. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  91. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  92. }
  93. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection
  94. {
  95. CGFloat width = [UIScreen mainScreen].bounds.size.width;
  96. CGFloat height = [UIScreen mainScreen].bounds.size.height;
  97. CGFloat mWidth = fmin(width, height);
  98. CGFloat mHeight = fmax(width, height);
  99. UIDevice *currentDevice = [UIDevice currentDevice];
  100. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  101. // This is an iPad
  102. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.9 : mHeight*0.7);
  103. } else {
  104. // This is an iPhone or iPod touch
  105. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? mWidth*0.9 : mHeight*0.9);
  106. }
  107. }
  108. - (void)viewWillLayoutSubviews {
  109. _segmentedControl.frame = CGRectMake(15, 44 , self.view.frame.size.width - 30, 30);
  110. self.doneBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  111. if (@available(iOS 11.0, *)) {
  112. self.outlineViewController.view.frame = CGRectMake(0, self.view.safeAreaInsets.top + 80, self.view.bounds.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - 80);
  113. _bookmarkViewController.view.frame = CGRectMake(0, self.view.safeAreaInsets.top + 80, self.view.bounds.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - 80);
  114. _annotationViewController.view.frame = CGRectMake(0, self.view.safeAreaInsets.top + 80, self.view.bounds.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.bounds.size.height - self.view.safeAreaInsets.top - self.view.safeAreaInsets.bottom - 80);
  115. } else {
  116. self.outlineViewController.view.frame = CGRectMake(0, 64 + 44, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 44 - 30);
  117. _bookmarkViewController.view.frame = CGRectMake(0, 64 + 44, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 30 - 44);
  118. _annotationViewController.view.frame = CGRectMake(0, 64 + 44, self.view.bounds.size.width, self.view.bounds.size.height - 64 - 30 - 44);
  119. }
  120. }
  121. - (void)buttonItemClicked_back:(id)sender {
  122. if([self.delegate respondsToSelector:@selector(botaViewControllerDismiss:)]) {
  123. [self.delegate botaViewControllerDismiss:self];
  124. }
  125. }
  126. #pragma mark - Action
  127. - (void)segmentedControlValueChanged_BOTA:(id)sender {
  128. [self.currentViewController.view removeFromSuperview];
  129. if(self.currentViewController == nil) {
  130. if(self.outlineViewController != nil) {
  131. [self.outlineViewController.view removeFromSuperview];
  132. }
  133. if(self.bookmarkViewController.view != nil) {
  134. [self.bookmarkViewController.view removeFromSuperview];
  135. }
  136. if(self.annotationViewController.view != nil) {
  137. [self.annotationViewController.view removeFromSuperview];
  138. }
  139. }
  140. self.type = [[self.segmmentArray objectAtIndex:self.segmentedControl.selectedSegmentIndex] intValue];
  141. switch (self.type) {
  142. case CPDFBOTATypeStateOutline:
  143. self.currentViewController = self.outlineViewController;
  144. [self.view addSubview:self.outlineViewController.view];
  145. break;
  146. case CPDFBOTATypeStateBookmark:
  147. self.currentViewController = self.bookmarkViewController;
  148. [self.view addSubview:self.bookmarkViewController.view];
  149. break;
  150. default:
  151. case CPDFBOTATypeStateAnnotation:
  152. self.currentViewController = self.annotationViewController;
  153. [self.view addSubview:self.annotationViewController.view];
  154. break;
  155. }
  156. }
  157. #pragma mark - CPDFOutlineViewControllerDelegate
  158. - (void)outlineViewController:(CPDFOutlineViewController *)outlineViewController pageIndex:(NSInteger)pageIndex {
  159. [self.pdfView goToPageIndex:pageIndex animated:NO];
  160. if([self.delegate respondsToSelector:@selector(botaViewControllerDismiss:)]) {
  161. [self.delegate botaViewControllerDismiss:self];
  162. }
  163. }
  164. #pragma mark - CPDFBookmarkViewControllerDelegate
  165. - (void)boomarkViewController:(CPDFBookmarkViewController *)bookmarkViewController pageIndex:(NSInteger)pageIndex {
  166. [self.pdfView goToPageIndex:pageIndex animated:NO];
  167. if([self.delegate respondsToSelector:@selector(botaViewControllerDismiss:)]) {
  168. [self.delegate botaViewControllerDismiss:self];
  169. }
  170. }
  171. #pragma mark - CPDFAnnotationViewControllerDelegate
  172. - (void)annotationViewController:(CPDFAnnotationViewController *)annotationViewController jumptoPage:(NSInteger)pageIndex selectAnnot:(CPDFAnnotation *)annot {
  173. [self.pdfView goToPageIndex:pageIndex animated:NO];
  174. if (@available(iOS 12.0, *)) {
  175. CGSize visibleRect = self.pdfView.documentView.visibleSize;
  176. [self.pdfView goToRect:CGRectMake(annot.bounds.origin.x, annot.bounds.origin.y+visibleRect.height/2, annot.bounds.size.width, annot.bounds.size.height) onPage:[self.pdfView.document pageAtIndex:pageIndex] animated:YES];
  177. } else {
  178. [self.pdfView goToRect:CGRectMake(annot.bounds.origin.x, annot.bounds.origin.y+100, annot.bounds.size.width, annot.bounds.size.height) onPage:[self.pdfView.document pageAtIndex:pageIndex] animated:YES];
  179. }
  180. if([self.delegate respondsToSelector:@selector(botaViewControllerDismiss:)]) {
  181. [self.delegate botaViewControllerDismiss:self];
  182. }
  183. }
  184. @end