CPDFShapeCircleViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // CPDFShapeCircleViewController.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 "CPDFShapeCircleViewController.h"
  13. #import "CPDFShareCircleViewController_Header.h"
  14. @interface CPDFShapeCircleViewController () <UIColorPickerViewControllerDelegate, CPDFThicknessSliderViewDelegate, CPDFColorSelectViewDelegate, CPDFColorPickerViewDelegate>
  15. @property (nonatomic, strong) NSMutableArray *dashPattern;
  16. @property (nonatomic, strong) UIColorPickerViewController *picker API_AVAILABLE(ios(14.0));
  17. @property (nonatomic, strong) UIColorPickerViewController *fillPicker API_AVAILABLE(ios(14.0));
  18. @end
  19. @implementation CPDFShapeCircleViewController
  20. #pragma mark - Initializers
  21. - (instancetype)initWithStyle:(CAnnotStyle *)annotStyle selectIndex:(NSInteger)index title:(NSString *)title {
  22. if (self = [super init]) {
  23. self.annotStyle = annotStyle;
  24. self.index = index;
  25. self.titles = title;
  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. _thicknessView = [[CPDFThicknessSliderView alloc] init];
  34. _thicknessView.delegate = self;
  35. _thicknessView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  36. [self.scrcollView addSubview:self.thicknessView];
  37. self.dottedView = [[CPDFThicknessSliderView alloc] init];
  38. self.dottedView.delegate = self;
  39. self.dottedView.thicknessSlider.minimumValue = 0.0;
  40. self.dottedView.thicknessSlider.maximumValue = 10.0;
  41. self.dottedView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  42. [self.scrcollView addSubview:self.dottedView];
  43. self.fillColorSelectView = [[CPDFColorSelectView alloc] init];
  44. self.fillColorSelectView.delegate = self;
  45. self.fillColorSelectView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  46. [self.scrcollView addSubview:self.fillColorSelectView];
  47. }
  48. - (void)viewWillLayoutSubviews {
  49. [super viewWillLayoutSubviews];
  50. self.scrcollView.frame = CGRectMake(0, 170, self.view.frame.size.width, self.view.frame.size.height-170);
  51. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 550);
  52. if (@available(iOS 11.0, *)) {
  53. self.colorView.frame = CGRectMake(self.view.safeAreaInsets.left, 0,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  54. self.fillColorSelectView.frame = CGRectMake(self.view.safeAreaInsets.left, 90, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  55. self.opacitySliderView.frame = CGRectMake(self.view.safeAreaInsets.left, 180, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  56. self.thicknessView.frame = CGRectMake(self.view.safeAreaInsets.left, 270, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  57. self.dottedView.frame = CGRectMake(self.view.safeAreaInsets.left, 360, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  58. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  59. } else {
  60. self.colorView.frame = CGRectMake(0, 0, self.view.frame.size.width, 90);
  61. self.opacitySliderView.frame = CGRectMake(0, 180, self.view.frame.size.width, 90);
  62. self.fillColorSelectView.frame = CGRectMake(0, 90, self.view.frame.size.width, 90);
  63. self.thicknessView.frame = CGRectMake(0, 270, self.view.frame.size.width, 90);
  64. self.dottedView.frame = CGRectMake(0, 360, self.view.frame.size.width, 90);
  65. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  66. }
  67. }
  68. #pragma mark - Protect Mehthods
  69. - (void)commomInitFromAnnotStyle {
  70. self.opacitySliderView.opacitySlider.value = self.annotStyle.opacity;
  71. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  72. self.thicknessView.thicknessSlider.value = self.annotStyle.lineWidth;
  73. self.thicknessView.startLabel.text = [NSString stringWithFormat:@"%d pt", (int)self.thicknessView.thicknessSlider.value];
  74. self.dashPattern = (NSMutableArray *)self.annotStyle.dashPattern;
  75. self.dottedView.thicknessSlider.value = [self.dashPattern.firstObject floatValue];
  76. self.dottedView.startLabel.text = [NSString stringWithFormat:@"%d pt", (int)self.dottedView.thicknessSlider.value];
  77. self.sampleView.color = self.annotStyle.color;
  78. self.sampleView.opcity = self.annotStyle.opacity;
  79. self.sampleView.thickness = self.annotStyle.lineWidth;
  80. self.sampleView.dotted = self.dottedView.thicknessSlider.value;
  81. self.sampleView.interiorColor = self.annotStyle.interiorColor?:[UIColor clearColor];
  82. [self.sampleView setNeedsDisplay];
  83. }
  84. - (void)commomInitTitle {
  85. self.sampleView.color = self.annotStyle.color;
  86. self.sampleView.interiorColor = [UIColor whiteColor];
  87. self.sampleView.thickness = 4.0;
  88. self.sampleView.selecIndex = (NSInteger)self.annotStyle.annotMode;
  89. switch (self.annotStyle.annotMode) {
  90. case CPDFViewAnnotationModeCircle:
  91. {
  92. self.titleLabel.text = NSLocalizedString(@"Circle", nil);
  93. }
  94. break;
  95. case CPDFViewAnnotationModeSquare:
  96. {
  97. self.titleLabel.text = NSLocalizedString(@"Square", nil);
  98. }
  99. break;
  100. case CPDFViewAnnotationModeArrow:
  101. {
  102. self.titleLabel.text = NSLocalizedString(@"Arrow", nil);
  103. }
  104. break;
  105. case CPDFViewAnnotationModeLine:
  106. {
  107. self.titleLabel.text = NSLocalizedString(@"Line", nil);
  108. }
  109. break;
  110. default:
  111. break;
  112. }
  113. self.fillColorSelectView.colorLabel.text = NSLocalizedString(@"Fill Color", nil);
  114. self.thicknessView.titleLabel.text = NSLocalizedString(@"Line Width", nil);
  115. self.colorView.colorLabel.text = NSLocalizedString(@"Stroke Color", nil);
  116. self.dottedView.titleLabel.text = NSLocalizedString(@"Line and Border Style", nil);
  117. self.colorView.selectedColor = self.annotStyle.color;
  118. self.fillColorSelectView.selectedColor = self.annotStyle.interiorColor;
  119. }
  120. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  121. if ([self.colorPicker superview] || [self.fillColorPicker superview]) {
  122. UIDevice *currentDevice = [UIDevice currentDevice];
  123. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  124. // This is an iPad
  125. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 520);
  126. } else {
  127. // This is an iPhone or iPod touch
  128. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 320);
  129. }
  130. } else {
  131. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 660);
  132. }
  133. }
  134. - (void)updateBordColor:(UIColor *)color {
  135. if(color) {
  136. CGFloat red, green, blue, alpha;
  137. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  138. self.sampleView.color = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
  139. self.sampleView.opcity = alpha;
  140. self.annotStyle.color = self.sampleView.color;
  141. self.annotStyle.opacity = self.sampleView.opcity;
  142. self.annotStyle.interiorOpacity = self.sampleView.opcity;
  143. } else {
  144. self.sampleView.color = color;
  145. self.sampleView.opcity = 0;
  146. self.annotStyle.color = color;
  147. }
  148. [self.sampleView setNeedsDisplay];
  149. if (self.delegate && [self.delegate respondsToSelector:@selector(circleViewController:annotStyle:)]) {
  150. [self.delegate circleViewController:self annotStyle:self.annotStyle];
  151. }
  152. self.opacitySliderView.opacitySlider.value = self.annotStyle.opacity;
  153. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];;
  154. }
  155. - (void)updateFillColor:(UIColor *)color {
  156. if(color) {
  157. CGFloat red, green, blue, alpha;
  158. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  159. self.sampleView.interiorColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
  160. self.sampleView.opcity = alpha;
  161. self.annotStyle.interiorColor = self.sampleView.interiorColor;
  162. self.annotStyle.opacity = self.sampleView.opcity;
  163. self.annotStyle.interiorOpacity = self.sampleView.opcity;
  164. } else {
  165. self.sampleView.interiorColor = color;
  166. self.sampleView.opcity = 0;
  167. self.annotStyle.color = color;
  168. }
  169. [self.sampleView setNeedsDisplay];
  170. if (self.delegate && [self.delegate respondsToSelector:@selector(circleViewController:annotStyle:)]) {
  171. [self.delegate circleViewController:self annotStyle:self.annotStyle];
  172. }
  173. self.opacitySliderView.opacitySlider.value = self.annotStyle.opacity;
  174. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];;
  175. }
  176. #pragma mark - CPDFOpacitySliderViewDelegate
  177. - (void)opacitySliderView:(CPDFOpacitySliderView *)opacitySliderView opacity:(CGFloat)opacity {
  178. self.sampleView.opcity = opacity;
  179. self.annotStyle.opacity = opacity;
  180. self.annotStyle.interiorOpacity = opacity;
  181. if (self.delegate && [self.delegate respondsToSelector:@selector(circleViewController:annotStyle:)]) {
  182. [self.delegate circleViewController:self annotStyle:self.annotStyle];
  183. }
  184. [self.sampleView setNeedsDisplay];
  185. }
  186. #pragma mark - CPDFThicknessSliderViewDelegate
  187. - (void)thicknessSliderView:(CPDFThicknessSliderView *)thicknessSliderView thickness:(CGFloat)thickness {
  188. if (thicknessSliderView == self.thicknessView) {
  189. self.sampleView.thickness = thickness;
  190. self.annotStyle.lineWidth = thickness;
  191. [self.sampleView setNeedsDisplay];
  192. if (self.delegate && [self.delegate respondsToSelector:@selector(circleViewController:annotStyle:)]) {
  193. [self.delegate circleViewController:self annotStyle:self.annotStyle];
  194. }
  195. } else if (thicknessSliderView == self.dottedView) {
  196. self.sampleView.dotted = thickness;
  197. self.annotStyle.style = CPDFBorderStyleDashed;
  198. self.annotStyle.dashPattern = @[[NSNumber numberWithFloat:(float)thickness]];
  199. if (self.delegate && [self.delegate respondsToSelector:@selector(circleViewController:annotStyle:)]) {
  200. [self.delegate circleViewController:self annotStyle:self.annotStyle];
  201. }
  202. [self.sampleView setNeedsDisplay];
  203. }
  204. }
  205. #pragma mark - CPDFColorSelectViewDelegate
  206. - (void)selectColorView:(CPDFColorSelectView *)select {
  207. if (select == self.colorView) {
  208. if (@available(iOS 14.0, *)) {
  209. self.picker = [[UIColorPickerViewController alloc] init];
  210. self.picker.delegate = self;
  211. [self presentViewController:self.picker animated:YES completion:nil];
  212. } else {
  213. UIDevice *currentDevice = [UIDevice currentDevice];
  214. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  215. // This is an iPad
  216. self.colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 520)];
  217. } else {
  218. // This is an iPhone or iPod touch
  219. self.colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 320)];
  220. }
  221. self.colorPicker.delegate = self;
  222. self.colorPicker.backgroundColor = [UIColor whiteColor];
  223. [self.view addSubview:self.colorPicker];
  224. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  225. }
  226. } else if (select == self.fillColorSelectView) {
  227. if (@available(iOS 14.0, *)) {
  228. self.fillPicker = [[UIColorPickerViewController alloc] init];
  229. self.fillPicker.delegate = self;
  230. [self presentViewController:self.fillPicker animated:YES completion:nil];
  231. } else {
  232. UIDevice *currentDevice = [UIDevice currentDevice];
  233. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  234. // This is an iPad
  235. self.fillColorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 520)];
  236. } else {
  237. // This is an iPhone or iPod touch
  238. self.fillColorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 320)];
  239. }
  240. self.fillColorPicker.delegate = self;
  241. self.fillColorPicker.backgroundColor = [UIColor whiteColor];
  242. [self.view addSubview:self.fillColorPicker];
  243. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  244. }
  245. }
  246. }
  247. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  248. if (select == self.colorView) {
  249. [self updateBordColor:color];
  250. } else if (select == self.fillColorSelectView) {
  251. [self updateFillColor:color];
  252. }
  253. }
  254. #pragma mark - CPDFColorPickerViewDelegate
  255. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  256. if (colorPickerView == self.colorPicker) {
  257. [self updateBordColor:color];
  258. } else if (colorPickerView == self.fillColorPicker) {
  259. [self updateFillColor:color];
  260. }
  261. CGFloat red, green, blue, alpha;
  262. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  263. self.opacitySliderView.opacitySlider.value = alpha;
  264. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  265. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  266. }
  267. #pragma mark - UIColorPickerViewControllerDelegate
  268. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  269. UIColor *color = viewController.selectedColor;
  270. if (viewController == self.picker) {
  271. [self updateBordColor:color];
  272. } else if (viewController == self.fillPicker) {
  273. [self updateFillColor:color];
  274. }
  275. CGFloat red, green, blue, alpha;
  276. [viewController.selectedColor getRed:&red green:&green blue:&blue alpha:&alpha];
  277. self.opacitySliderView.opacitySlider.value = alpha;
  278. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  279. }
  280. @end