PDFPageEditAddViewController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // PDFPageEditAddViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFPageEditAddViewController.h"
  11. @interface PDFPageEditAddViewController () <UICollectionViewDelegate,UICollectionViewDataSource,UIPopoverPresentationControllerDelegate>
  12. @property (nonatomic,assign) IBOutlet UICollectionView *collectionView;
  13. @property (nonatomic,retain) NSMutableArray *pages;
  14. @end
  15. @implementation PDFPageEditAddViewController
  16. #pragma mark - Init Methods
  17. - (instancetype)init {
  18. if (self = [super init]) {
  19. }
  20. return self;
  21. }
  22. - (void)dealloc {
  23. Block_release(_callback);
  24. [_pages release];
  25. [super dealloc];
  26. }
  27. #pragma mark - UIViewController Methods
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. // Do any additional setup after loading the view from its nib.
  31. self.title = NSLocalizedString(@"New Page", nil);
  32. [self.collectionView registerClass:[PDFPageEditCell class] forCellWithReuseIdentifier:@"PDFPageEditCell"];
  33. self.collectionView.alwaysBounceVertical = YES;
  34. self.pages = [NSMutableArray array];
  35. NSArray *images = @[@"", @"paper1@2x", @"paper2@2x", @"paper3@2x", @"paper4@2x", @"paper5@2x", @"paper6@2x",];
  36. for (int i=0; i<images.count; i++) {
  37. NSString *image = images[i];
  38. PDFEditPage *page = [[PDFEditPage alloc] init];
  39. if (image.length > 0) {
  40. NSString *imagePath = [[NSBundle mainBundle] pathForResource:image ofType:@"jpg"];
  41. page.imagePath = imagePath;
  42. }
  43. if (i > 3) {
  44. page.size = CGSizeMake(842, 595);
  45. }
  46. [self.pages addObject:page];
  47. [page release];
  48. }
  49. }
  50. #pragma mark - Public Methods
  51. - (void)showViewController:(UIViewController *)viewController inBarButtonItem:(UIBarButtonItem *)barButtonItem {
  52. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  53. self.preferredContentSize = CGSizeMake(580, 580);
  54. } else {
  55. self.preferredContentSize = CGSizeMake(280, 280);
  56. }
  57. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
  58. nav.modalPresentationStyle = UIModalPresentationPopover;
  59. UIPopoverPresentationController *popVC = nav.popoverPresentationController;
  60. popVC.delegate = self;
  61. popVC.barButtonItem = barButtonItem;
  62. popVC.permittedArrowDirections = UIPopoverArrowDirectionUp;
  63. [viewController presentViewController:nav animated:YES completion:nil];
  64. [nav release];
  65. }
  66. #pragma mark - UIPopoverPresentationControllerDelegate
  67. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  68. return UIModalPresentationNone;
  69. }
  70. #pragma mark - UICollectionViewDelegateFlowLayout
  71. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  72. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  73. return CGSizeMake(180, 180);
  74. } else {
  75. return CGSizeMake(80, 80);
  76. }
  77. }
  78. #pragma mark - UICollectionViewDataSource
  79. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  80. return self.pages.count;
  81. }
  82. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  83. PDFPageEditCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PDFPageEditCell" forIndexPath:indexPath];
  84. cell.backgroundColor = [UIColor clearColor];
  85. cell.page = self.pages[indexPath.item];
  86. return cell;
  87. }
  88. #pragma mark - UICollectionViewDelegate
  89. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  90. [self dismissViewControllerAnimated:YES completion:^{
  91. if (self.callback) {
  92. self.callback(self.pages[indexPath.item]);
  93. self.callback = nil;
  94. }
  95. }];
  96. }
  97. @end