CPDFAnnotationBaseViewController.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // CPDFAnnotationBaseViewController.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 "CPDFAnnotationBaseViewController.h"
  13. #import "CPDFAnnotationBaseViewController_Header.h"
  14. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  15. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  16. #else
  17. #import "ComPDFKit_Tools.h"
  18. #endif
  19. @interface CPDFAnnotationBaseViewController () <UIColorPickerViewControllerDelegate, CPDFColorSelectViewDelegate, CPDFColorPickerViewDelegate, CPDFOpacitySliderViewDelegate>
  20. @end
  21. @implementation CPDFAnnotationBaseViewController
  22. #pragma mark - Initializers
  23. - (instancetype)initWithStyle:(CAnnotStyle *)annotStyle {
  24. if (self = [super init]) {
  25. self.annotStyle = annotStyle;
  26. }
  27. return self;
  28. }
  29. #pragma mark - ViewController Methods
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. // Do any additional setup after loading the view.
  33. self.headerView = [[UIView alloc] init];
  34. self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  35. self.headerView.layer.borderWidth = 1.0;
  36. self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  37. [self.view addSubview:self.headerView];
  38. self.titleLabel = [[UILabel alloc] init];
  39. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  40. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  41. self.titleLabel.font = [UIFont systemFontOfSize:20];
  42. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  43. [self.headerView addSubview:self.titleLabel];
  44. self.scrcollView = [[UIScrollView alloc] init];
  45. self.scrcollView.scrollEnabled = YES;
  46. [self.view addSubview:self.scrcollView];
  47. self.backBtn = [[UIButton alloc] init];
  48. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  49. [self.backBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBaseImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  50. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  51. [self.headerView addSubview:self.backBtn];
  52. self.sampleBackgoundView = [[UIView alloc] init];
  53. self.sampleBackgoundView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  54. self.sampleBackgoundView.layer.borderWidth = 1.0;
  55. self.sampleBackgoundView.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  56. [self.headerView addSubview:self.sampleBackgoundView];
  57. self.sampleView = [[CPDFSampleView alloc] init];
  58. self.sampleView.backgroundColor = [UIColor whiteColor];
  59. self.sampleView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  60. self.sampleView.layer.borderWidth = 1.0;
  61. self.sampleView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  62. [self.sampleBackgoundView addSubview:self.sampleView];
  63. self.colorView = [[CPDFColorSelectView alloc] init];
  64. self.colorView.delegate = self;
  65. self.colorView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  66. [self.scrcollView addSubview:self.colorView];
  67. self.opacitySliderView = [[CPDFOpacitySliderView alloc] init];
  68. self.opacitySliderView.delegate = self;
  69. self.opacitySliderView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  70. [self.scrcollView addSubview:self.opacitySliderView];
  71. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  72. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  73. }
  74. - (void)viewWillLayoutSubviews {
  75. [super viewWillLayoutSubviews];
  76. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  77. self.scrcollView.frame = CGRectMake(0, 170, self.view.frame.size.width, 210);
  78. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 170);
  79. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 330);
  80. self.sampleBackgoundView.frame = CGRectMake(0, 50, self.view.bounds.size.width, 120);
  81. self.sampleView.frame = CGRectMake((self.view.frame.size.width - 300)/2, 15, 300, self.sampleBackgoundView.bounds.size.height - 30);
  82. if (@available(iOS 11.0, *)) {
  83. self.colorPicker.frame = CGRectMake(self.view.safeAreaInsets.left, 0, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, self.view.frame.size.height);
  84. self.colorView.frame = CGRectMake(self.view.safeAreaInsets.left, 0,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  85. self.opacitySliderView.frame = CGRectMake(self.view.safeAreaInsets.left, 90, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  86. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  87. } else {
  88. self.colorView.frame = CGRectMake(0, 0, self.view.frame.size.width, 90);
  89. self.opacitySliderView.frame = CGRectMake(0, 90, self.view.frame.size.width, 90);
  90. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  91. }
  92. }
  93. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  94. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  95. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  96. }
  97. - (void)viewWillAppear:(BOOL)animated {
  98. [super viewWillAppear:animated];
  99. [self commomInitTitle];
  100. [self commomInitFromAnnotStyle];
  101. }
  102. #pragma mark - Protect Methods
  103. - (void)commomInitTitle {
  104. self.titleLabel.text = NSLocalizedString(@"Note", nil);
  105. self.sampleView.selecIndex = CPDFSamplesHighlight;
  106. self.colorView.colorLabel.text = NSLocalizedString(@"Color", nil);
  107. self.colorView.selectedColor = self.annotStyle.color;
  108. [self.colorView setNeedsLayout];
  109. }
  110. - (void)commomInitFromAnnotStyle {
  111. self.sampleView.color = self.annotStyle.color;
  112. self.sampleView.opcity = self.annotStyle.opacity;
  113. self.opacitySliderView.opacitySlider.value = self.annotStyle.opacity;
  114. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  115. }
  116. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  117. if ([self.colorPicker superview]) {
  118. UIDevice *currentDevice = [UIDevice currentDevice];
  119. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  120. // This is an iPad
  121. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 520);
  122. } else {
  123. // This is an iPhone or iPod touch
  124. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 320);
  125. }
  126. } else {
  127. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 420);
  128. }
  129. }
  130. #pragma mark - Action
  131. - (void)buttonItemClicked_back:(id)sender {
  132. [self dismissViewControllerAnimated:YES completion:nil];
  133. }
  134. #pragma mark - CPDFColorSelectViewDelegate
  135. - (void)selectColorView:(CPDFColorSelectView *)select {
  136. if (@available(iOS 14.0, *)) {
  137. UIColorPickerViewController *picker = [[UIColorPickerViewController alloc] init];
  138. picker.delegate = self;
  139. [self presentViewController:picker animated:YES completion:nil];
  140. } else {
  141. UIDevice *currentDevice = [UIDevice currentDevice];
  142. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  143. // This is an iPad
  144. _colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 520)];
  145. } else {
  146. // This is an iPhone or iPod touch
  147. _colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 320)];
  148. }
  149. _colorPicker.delegate = self;
  150. _colorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  151. _colorPicker.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  152. [self.view addSubview:self.colorPicker];
  153. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  154. }
  155. }
  156. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  157. self.sampleView.color = color;
  158. [self.sampleView setNeedsDisplay];
  159. }
  160. #pragma mark - CPDFColorPickerViewDelegate
  161. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  162. self.sampleView.color = color;
  163. [self.sampleView setNeedsDisplay];
  164. CGFloat red, green, blue, alpha;
  165. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  166. self.opacitySliderView.opacitySlider.value = alpha;
  167. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  168. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  169. }
  170. #pragma mark - CPDFOpacitySliderViewDelegate
  171. - (void)opacitySliderView:(CPDFOpacitySliderView *)opacitySliderView opacity:(CGFloat)opacity {
  172. self.sampleView.opcity = opacity;
  173. [self.sampleView setNeedsDisplay];
  174. }
  175. @end