CPDFInkViewController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // CPDFInkViewController.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 "CPDFInkViewController.h"
  13. #import "CPDFAnnotationBaseViewController_Header.h"
  14. #import "CPDFThicknessSliderView.h"
  15. #import "CPDFInkTopToolBar.h"
  16. @interface CPDFInkViewController () <CPDFThicknessSliderViewDelegate>
  17. @property (nonatomic, strong) CPDFThicknessSliderView *thicknessView;
  18. @end
  19. @implementation CPDFInkViewController
  20. #pragma mark - ViewController Methods
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. _thicknessView = [[CPDFThicknessSliderView alloc] init];
  24. _thicknessView.delegate = self;
  25. _thicknessView.thicknessSlider.value = 4.0;
  26. _thicknessView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  27. [self.scrcollView addSubview:self.thicknessView];
  28. }
  29. - (void)viewWillLayoutSubviews {
  30. [super viewWillLayoutSubviews];
  31. self.scrcollView.frame = CGRectMake(0, 170, self.view.frame.size.width, 310);
  32. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 400);
  33. if (@available(iOS 11.0, *)) {
  34. self.thicknessView.frame = CGRectMake(self.view.safeAreaInsets.left, 180, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  35. } else {
  36. self.colorView.frame = CGRectMake(0, 0, self.view.frame.size.width, 90);
  37. self.thicknessView.frame = CGRectMake(0, 180, self.view.frame.size.width, 90);
  38. }
  39. }
  40. - (void)viewWillDisappear:(BOOL)animated {
  41. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewControllerDimiss:)]) {
  42. [self.delegate inkViewControllerDimiss:self];
  43. }
  44. }
  45. #pragma mark - Private Methods
  46. - (void)commomInitTitle {
  47. self.titleLabel.text = NSLocalizedString(@"Ink", nil);
  48. self.thicknessView.titleLabel.text = NSLocalizedString(@"Line Width", nil);
  49. self.sampleView.selecIndex = CPDFSamplesFreehand;
  50. self.sampleView.thickness = 4.0;
  51. self.colorView.selectedColor = self.annotStyle.color;
  52. }
  53. - (void)commomInitFromAnnotStyle {
  54. self.sampleView.color = self.annotStyle.color;
  55. self.sampleView.opcity = self.annotStyle.opacity;
  56. self.sampleView.thickness = self.annotStyle.lineWidth;
  57. self.opacitySliderView.opacitySlider.value = self.annotStyle.opacity;
  58. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  59. self.thicknessView.thicknessSlider.value = self.annotStyle.lineWidth;
  60. self.thicknessView.startLabel.text = [NSString stringWithFormat:@"%d pt", (int)self.thicknessView.thicknessSlider.value];
  61. [self.sampleView setNeedsDisplay];
  62. }
  63. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  64. if ([self.colorPicker superview]) {
  65. UIDevice *currentDevice = [UIDevice currentDevice];
  66. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  67. // This is an iPad
  68. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 520);
  69. } else {
  70. // This is an iPhone or iPod touch
  71. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 320);
  72. }
  73. } else {
  74. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 520);
  75. }
  76. }
  77. #pragma mark - Action
  78. - (void)buttonItemClicked_back:(id)sender {
  79. [self dismissViewControllerAnimated:YES completion:nil];
  80. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewControllerDimiss:)]) {
  81. [self.delegate inkViewControllerDimiss:self];
  82. }
  83. }
  84. #pragma mark - CPDFThicknessSliderViewDelegate
  85. - (void)thicknessSliderView:(CPDFThicknessSliderView *)thicknessSliderView thickness:(CGFloat)thickness {
  86. self.sampleView.thickness = thickness;
  87. self.annotStyle.lineWidth = thickness;
  88. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewController:annotStyle:)]) {
  89. [self.delegate inkViewController:self annotStyle:self.annotStyle];
  90. }
  91. [self.sampleView setNeedsDisplay];
  92. }
  93. #pragma mark - CPDFColorSelectViewDelegate
  94. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  95. self.sampleView.color = color;
  96. self.annotStyle.color = color;
  97. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewController:annotStyle:)]) {
  98. [self.delegate inkViewController:self annotStyle:self.annotStyle];
  99. }
  100. [self.sampleView setNeedsDisplay];
  101. }
  102. #pragma mark - CPDFColorPickerViewDelegate
  103. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  104. self.sampleView.color = color;
  105. self.annotStyle.color = color;
  106. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewController:annotStyle:)]) {
  107. [self.delegate inkViewController:self annotStyle:self.annotStyle];
  108. }
  109. [self.sampleView setNeedsDisplay];
  110. CGFloat red, green, blue, alpha;
  111. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  112. self.opacitySliderView.opacitySlider.value = alpha;
  113. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  114. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  115. }
  116. #pragma mark - CPDFOpacitySliderViewDelegate
  117. - (void)opacitySliderView:(CPDFOpacitySliderView *)opacitySliderView opacity:(CGFloat)opacity {
  118. self.sampleView.opcity = opacity;
  119. self.annotStyle.opacity = opacity;
  120. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewController:annotStyle:)]) {
  121. [self.delegate inkViewController:self annotStyle:self.annotStyle];
  122. }
  123. [self.sampleView setNeedsDisplay];
  124. }
  125. #pragma mark - UIColorPickerViewControllerDelegate
  126. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  127. self.sampleView.color = viewController.selectedColor;
  128. self.annotStyle.color = self.sampleView.color;
  129. if (self.delegate && [self.delegate respondsToSelector:@selector(inkViewController:annotStyle:)]) {
  130. [self.delegate inkViewController:self annotStyle:self.annotStyle];
  131. }
  132. [self.sampleView setNeedsDisplay];
  133. CGFloat red, green, blue, alpha;
  134. [viewController.selectedColor getRed:&red green:&green blue:&blue alpha:&alpha];
  135. self.opacitySliderView.opacitySlider.value = alpha;
  136. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  137. }
  138. @end