PDFViewerModeViewController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // PDFViewerModeViewController.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 "PDFViewerModeViewController.h"
  11. #import "PDFSettingsColorViewController.h"
  12. #import <ComPDFKit/ComPDFKit.h>
  13. @interface PDFViewerModeViewController () <UIPopoverPresentationControllerDelegate>
  14. @property (nonatomic,assign) IBOutlet UIScrollView *scrollView;
  15. @property (nonatomic,assign) IBOutlet UISlider *slider;
  16. @property (nonatomic,assign) IBOutlet UISegmentedControl *segmentedControl;
  17. @property (nonatomic,assign) IBOutlet UISwitch *switchButton;
  18. @end
  19. @implementation PDFViewerModeViewController
  20. #pragma mark - Init Methods
  21. - (void)dealloc {
  22. Block_release(_callback);
  23. [_color release];
  24. [super dealloc];
  25. }
  26. #pragma mark - UIViewController Methods
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. // Do any additional setup after loading the view from its nib.
  30. self.preferredContentSize = CGSizeMake(320, 270);
  31. NSArray *colors = @[[UIColor whiteColor],
  32. [UIColor blackColor],
  33. [UIColor colorWithRed:255/255.0 green:239/255.0 blue:190/255.0 alpha:1.0],
  34. [UIColor colorWithRed:153.0/255.0 green:207.0/255.0 blue:161.0/255.0 alpha:1.0],
  35. [UIColor clearColor]];
  36. for (NSInteger i = 0; i < [colors count]; i++) {
  37. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  38. [button addTarget:self action:@selector(buttonItemClicked_Color:) forControlEvents:UIControlEventTouchUpInside];
  39. button.tag = i;
  40. button.frame = CGRectMake(15+i*60, 5, 50.0, 50.0);
  41. button.layer.masksToBounds = YES;
  42. button.layer.cornerRadius = button.frame.size.width * 0.5;
  43. button.layer.borderWidth = 2.0;
  44. button.layer.borderColor = ((UIColor *)colors[i]).CGColor;
  45. button.showsTouchWhenHighlighted = YES;
  46. [button setBackgroundColor:colors[i]];
  47. if (i == self.themesType) {
  48. button.layer.borderColor = [UIColor colorWithRed:73.0/255.0 green:106.0/255.0 blue:239.0/255.0 alpha:1.0].CGColor;
  49. }
  50. if (i == [colors count]-1) {
  51. [button setBackgroundImage:[UIImage imageNamed:@"view_color_wheel_img.png"] forState:UIControlStateNormal];
  52. }
  53. [self.scrollView addSubview:button];
  54. }
  55. self.slider.value = self.brightness;
  56. self.segmentedControl.selectedSegmentIndex = self.scrollType;
  57. self.switchButton.on = self.isCropMode;
  58. }
  59. #pragma mark - Button Action
  60. - (void)buttonItemClicked_Color:(UIButton *)button {
  61. if (![[CPDFKit sharedInstance] allowsFeature:CPDFKitFeatureViewerRender]) {
  62. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  63. style:UIAlertActionStyleCancel
  64. handler:nil];
  65. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
  66. message:NSLocalizedString(@"Your license does not support this feature, please upgrade your license privilege.", nil)
  67. preferredStyle:UIAlertControllerStyleAlert];
  68. [alert addAction:cancelAction];
  69. [self presentViewController:alert animated:YES completion:nil];
  70. return;
  71. }
  72. if (button.tag == 4) {
  73. CGRect rect = [self.view convertRect:button.frame fromView:button.superview];
  74. __block PDFSettingsColorViewController *vc = [[PDFSettingsColorViewController alloc] init];
  75. [vc setColor:self.color];
  76. vc.callback = ^{
  77. self.color = vc.color;
  78. for (UIButton *button in self.scrollView.subviews) {
  79. if ([button isKindOfClass:[UIButton class]]) {
  80. button.layer.borderColor = button.backgroundColor.CGColor;
  81. }
  82. }
  83. button.layer.borderColor = [UIColor colorWithRed:73.0/255.0 green:106.0/255.0 blue:239.0/255.0 alpha:1.0].CGColor;
  84. self.themesType = button.tag;
  85. if (self.callback) {
  86. self.callback(0);
  87. }
  88. };
  89. [vc showViewController:self inRect:rect];
  90. [vc release];
  91. } else {
  92. for (UIButton *button in self.scrollView.subviews) {
  93. if ([button isKindOfClass:[UIButton class]]) {
  94. button.layer.borderColor = button.backgroundColor.CGColor;
  95. }
  96. }
  97. button.layer.borderColor = [UIColor colorWithRed:73.0/255.0 green:106.0/255.0 blue:239.0/255.0 alpha:1.0].CGColor;
  98. self.themesType = button.tag;
  99. if (self.callback) {
  100. self.callback(0);
  101. }
  102. }
  103. }
  104. - (IBAction)sliderValueChanged_Brightness:(id)sender {
  105. self.brightness = self.slider.value;
  106. if (self.callback) {
  107. self.callback(1);
  108. }
  109. }
  110. - (IBAction)segmentedControlValueChanged_Mode:(id)sender {
  111. self.scrollType = self.segmentedControl.selectedSegmentIndex;
  112. if (self.callback) {
  113. self.callback(2);
  114. }
  115. }
  116. - (IBAction)switchValueChanged_CropMode:(id)sender {
  117. self.isCropMode = self.switchButton.isOn;
  118. if (self.callback) {
  119. self.callback(3);
  120. }
  121. }
  122. #pragma mark - Public Methods
  123. - (void)showViewController:(UIViewController *)viewController inBarButtonItem:(UIBarButtonItem *)barButtonItem {
  124. self.modalPresentationStyle = UIModalPresentationPopover;
  125. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  126. popVC.delegate = self;
  127. popVC.barButtonItem = barButtonItem;
  128. popVC.permittedArrowDirections = UIPopoverArrowDirectionUp;
  129. [viewController presentViewController:self animated:YES completion:nil];
  130. }
  131. - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect {
  132. self.modalPresentationStyle = UIModalPresentationPopover;
  133. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  134. popVC.delegate = self;
  135. popVC.sourceRect = rect;
  136. popVC.sourceView = viewController.view;
  137. [viewController presentViewController:self animated:YES completion:nil];
  138. }
  139. #pragma mark - UIPopoverPresentationControllerDelegate
  140. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  141. return UIModalPresentationNone;
  142. }
  143. @end