CPDFShapeArrowViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // CPDFShapeArrowViewController.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 "CPDFShapeArrowViewController.h"
  13. #import "CPDFShareCircleViewController_Header.h"
  14. #import "CPDFArrowStyleView.h"
  15. #import "CShapeSelectView.h"
  16. #import "CPDFDrawArrowView.h"
  17. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  18. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  19. #else
  20. #import "ComPDFKit_Tools.h"
  21. #endif
  22. @interface CPDFShapeArrowViewController () <UIColorPickerViewControllerDelegate, CPDFArrowStyleViewDelegate, CShapeSelectViewDelegate, CPDFColorPickerViewDelegate>
  23. @property (nonatomic, strong) UILabel *arrowLabel;
  24. @property (nonatomic, strong) UIButton *arrowBtn;
  25. @property (nonatomic, strong) UILabel *trialLabel;
  26. @property (nonatomic, strong) UIButton *trialBtn;
  27. @property (nonatomic, strong) CPDFArrowStyleView *startArrowStyleView;
  28. @property (nonatomic, strong) CPDFArrowStyleView *endArrowStyleView;
  29. @property (nonatomic, strong) CPDFDrawArrowView *startDrawView;
  30. @property (nonatomic, strong) CPDFDrawArrowView *endDrawView;
  31. @property (nonatomic, strong) NSMutableArray *dashPattern;
  32. @property (nonatomic, strong) CShapeSelectView *shapeSelectView;
  33. @property (nonatomic, strong) UIColorPickerViewController *picker API_AVAILABLE(ios(14.0));
  34. @property (nonatomic, strong) UIColorPickerViewController *fillPicker API_AVAILABLE(ios(14.0));
  35. @end
  36. @implementation CPDFShapeArrowViewController
  37. #pragma mark - ViewController Methods
  38. - (void)viewDidLoad {
  39. [super viewDidLoad];
  40. // Do any additional setup after loading the view.
  41. self.arrowLabel = [[UILabel alloc] init];
  42. self.arrowLabel.text = NSLocalizedString(@"Start", nil);
  43. self.arrowLabel.textColor = [UIColor grayColor];
  44. self.arrowLabel.font = [UIFont systemFontOfSize:12.0];
  45. [self.scrcollView addSubview:self.arrowLabel];
  46. self.arrowBtn = [[UIButton alloc] init];
  47. [self.arrowBtn setImage:[UIImage imageNamed:@"CPDFShapeArrowImageStart" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  48. self.arrowBtn.layer.borderWidth = 1.0;
  49. self.arrowBtn.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  50. self.arrowBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  51. [self.arrowBtn addTarget:self action:@selector(buttonItemClicked_start:) forControlEvents:UIControlEventTouchUpInside];
  52. [self.scrcollView addSubview:self.arrowBtn];
  53. self.startDrawView = [[CPDFDrawArrowView alloc] init];
  54. self.startDrawView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  55. [self.arrowBtn addSubview:self.startDrawView];
  56. self.trialLabel = [[UILabel alloc] init];
  57. self.trialLabel.text = NSLocalizedString(@"End", nil);
  58. self.trialLabel.textColor = [UIColor grayColor];
  59. self.trialLabel.font = [UIFont systemFontOfSize:12.0];
  60. self.trialLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  61. [self.scrcollView addSubview:self.trialLabel];
  62. self.trialBtn = [[UIButton alloc] init];
  63. [self.trialBtn setImage:[UIImage imageNamed:@"CPDFShapeArrowImageEnd" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  64. self.trialBtn.layer.borderWidth = 1.0;
  65. self.trialBtn.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  66. [self.trialBtn addTarget:self action:@selector(buttonItemClicked_trial:) forControlEvents:UIControlEventTouchUpInside];
  67. self.trialBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  68. [self.scrcollView addSubview:self.trialBtn];
  69. self.endDrawView = [[CPDFDrawArrowView alloc]init];
  70. self.endDrawView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  71. [self.trialBtn addSubview:self.endDrawView];
  72. self.fillColorSelectView.hidden = YES;
  73. self.view.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  74. }
  75. - (void)viewWillLayoutSubviews {
  76. [super viewWillLayoutSubviews];
  77. self.scrcollView.frame = CGRectMake(0, 170, self.view.frame.size.width, self.view.frame.size.height-170);
  78. self.scrcollView.contentSize = CGSizeMake(self.view.frame.size.width, 500);
  79. if (@available(iOS 11.0, *)) {
  80. CGFloat offsetY = 0;
  81. self.colorView.frame = CGRectMake(self.view.safeAreaInsets.left, 0,self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  82. offsetY+= self.colorView.frame.size.height;
  83. self.opacitySliderView.frame = CGRectMake(self.view.safeAreaInsets.left, offsetY, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  84. offsetY+= self.opacitySliderView.frame.size.height;
  85. self.thicknessView.frame = CGRectMake(self.view.safeAreaInsets.left, offsetY, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  86. offsetY+= self.thicknessView.frame.size.height;
  87. self.dottedView.frame = CGRectMake(self.view.safeAreaInsets.left, offsetY, self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, 90);
  88. offsetY+= self.dottedView.frame.size.height;
  89. self.arrowLabel.frame = CGRectMake(self.view.safeAreaInsets.left+20, offsetY, 100, 45);
  90. self.arrowBtn.frame = CGRectMake(self.view.frame.size.width - 100 - self.view.safeAreaInsets.right, offsetY + 7.5, 80, 30);
  91. offsetY+= self.arrowLabel.frame.size.height;
  92. self.startDrawView.frame = CGRectMake(0, 0, 40, 30);
  93. self.trialLabel.frame = CGRectMake(self.view.safeAreaInsets.left+20, offsetY, 100, 45);
  94. self.trialBtn.frame = CGRectMake(self.view.frame.size.width - 100- self.view.safeAreaInsets.right, offsetY + 7.5, 80, 30);
  95. self.endDrawView.frame = CGRectMake(0, 0, 40, 30);
  96. } else {
  97. CGFloat offsetY = 0;
  98. self.colorView.frame = CGRectMake(0, 0,self.view.frame.size.width, 90);
  99. offsetY+= self.colorView.frame.size.height;
  100. self.opacitySliderView.frame = CGRectMake(0, offsetY, self.view.frame.size.width - 0, 90);
  101. offsetY+= self.opacitySliderView.frame.size.height;
  102. self.thicknessView.frame = CGRectMake(0, offsetY, self.view.frame.size.width, 90);
  103. offsetY+= self.thicknessView.frame.size.height;
  104. self.dottedView.frame = CGRectMake(0, offsetY, self.view.frame.size.width, 90);
  105. offsetY+= self.dottedView.frame.size.height;
  106. self.arrowLabel.frame = CGRectMake(20, offsetY, 100, 45);
  107. self.arrowBtn.frame = CGRectMake(self.view.frame.size.width - 100, offsetY + 7.5, 80, 30);
  108. offsetY+= self.arrowLabel.frame.size.height;
  109. self.startDrawView.frame = CGRectMake(0, 0, 40, 30);
  110. self.trialLabel.frame = CGRectMake(20, offsetY, 100, 45);
  111. self.trialBtn.frame = CGRectMake(self.view.frame.size.width - 100, offsetY + 7.5, 80, 30);
  112. self.endDrawView.frame = CGRectMake(0, 0, 40, 30);
  113. }
  114. }
  115. #pragma mark - Protect Mehthods
  116. - (void)commomInitFromAnnotStyle {
  117. self.opacitySliderView.opacitySlider.value = self.annotStyle.opacity;
  118. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  119. self.thicknessView.thicknessSlider.value = self.annotStyle.lineWidth;
  120. self.thicknessView.startLabel.text = [NSString stringWithFormat:@"%d pt", (int)self.thicknessView.thicknessSlider.value];
  121. self.dashPattern = (NSMutableArray *)self.annotStyle.dashPattern;
  122. self.dottedView.thicknessSlider.value = [self.dashPattern.firstObject floatValue];
  123. self.dottedView.startLabel.text = [NSString stringWithFormat:@"%d pt", (int)self.dottedView.thicknessSlider.value];
  124. self.sampleView.color = self.annotStyle.color;
  125. self.sampleView.opcity = self.annotStyle.opacity;
  126. self.sampleView.dotted = self.dottedView.thicknessSlider.value;
  127. self.sampleView.interiorColor = self.annotStyle.interiorColor;
  128. self.sampleView.startArrowStyleIndex = (NSInteger)self.annotStyle.startLineStyle;
  129. self.sampleView.endArrowStyleIndex = (NSInteger)self.annotStyle.endLineStyle;
  130. [self.sampleView setNeedsDisplay];
  131. self.startDrawView.selectIndex = (NSInteger)self.annotStyle.startLineStyle;
  132. [self.startDrawView setNeedsDisplay];
  133. self.endDrawView.selectIndex = (NSInteger)self.annotStyle.endLineStyle;
  134. [self.endDrawView setNeedsDisplay];
  135. }
  136. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  137. if([self.startArrowStyleView superview] || [self.endArrowStyleView superview]) {
  138. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 120);
  139. } else if ([self.colorPicker superview]) {
  140. UIDevice *currentDevice = [UIDevice currentDevice];
  141. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  142. // This is an iPad
  143. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 520);
  144. } else {
  145. // This is an iPhone or iPod touch
  146. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, 320);
  147. }
  148. } else {
  149. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 660);
  150. }
  151. }
  152. #pragma mark - Action
  153. - (void)buttonItemClicked_start:(id)sender {
  154. self.startArrowStyleView = [[CPDFArrowStyleView alloc] initWirhTitle:NSLocalizedString(@"Arrow Style",nil)];
  155. self.startArrowStyleView.frame = self.view.frame;
  156. self.startArrowStyleView.delegate = self;
  157. self.startArrowStyleView.selectIndex = self.startDrawView.selectIndex;
  158. self.startArrowStyleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  159. [self.view addSubview:self.startArrowStyleView];
  160. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  161. }
  162. - (void)buttonItemClicked_trial:(id)sender {
  163. self.endArrowStyleView = [[CPDFArrowStyleView alloc] initWirhTitle:NSLocalizedString(@"Arrowtail style",nil)];
  164. self.endArrowStyleView.frame = self.view.frame;
  165. self.endArrowStyleView.delegate = self;
  166. self.endArrowStyleView.selectIndex = self.endDrawView.selectIndex;
  167. self.endArrowStyleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  168. [self.view addSubview:self.endArrowStyleView];
  169. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  170. }
  171. #pragma mark - CPDFArrowStyleViewDelegate
  172. - (void)arrowStyleView:(CPDFArrowStyleView *)arrowStyleView selectIndex:(NSInteger)selectIndex {
  173. if (arrowStyleView == self.startArrowStyleView) {
  174. self.sampleView.startArrowStyleIndex = selectIndex;
  175. self.annotStyle.startLineStyle = selectIndex;
  176. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  177. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  178. }
  179. [self.sampleView setNeedsDisplay];
  180. self.startDrawView.selectIndex = selectIndex;
  181. [self.startDrawView setNeedsDisplay];
  182. } else if (arrowStyleView == self.endArrowStyleView) {
  183. self.sampleView.endArrowStyleIndex = selectIndex;
  184. self.annotStyle.endLineStyle = selectIndex;
  185. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  186. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  187. }
  188. [self.sampleView setNeedsDisplay];
  189. self.endDrawView.selectIndex = selectIndex;
  190. [self.endDrawView setNeedsDisplay];
  191. }
  192. }
  193. - (void)arrowStyleRemoveView:(CPDFArrowStyleView *)arrowStyleView {
  194. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  195. }
  196. #pragma mark - CPDFOpacitySliderViewDelegate
  197. - (void)opacitySliderView:(CPDFOpacitySliderView *)opacitySliderView opacity:(CGFloat)opacity {
  198. self.sampleView.opcity = opacity;
  199. self.annotStyle.opacity = opacity;
  200. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  201. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  202. }
  203. [self.sampleView setNeedsDisplay];
  204. }
  205. #pragma mark - CPDFThicknessSliderViewDelegate
  206. - (void)thicknessSliderView:(CPDFThicknessSliderView *)thicknessSliderView thickness:(CGFloat)thickness {
  207. if (thicknessSliderView == self.thicknessView) {
  208. self.sampleView.thickness = thickness;
  209. self.annotStyle.lineWidth = thickness;
  210. [self.sampleView setNeedsDisplay];
  211. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  212. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  213. }
  214. } else if (thicknessSliderView == self.dottedView) {
  215. self.sampleView.dotted = thickness;
  216. self.annotStyle.style = CPDFBorderStyleDashed;
  217. self.annotStyle.dashPattern = @[[NSNumber numberWithFloat:(float)thickness]];
  218. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  219. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  220. }
  221. [self.sampleView setNeedsDisplay];
  222. }
  223. }
  224. #pragma mark - CPDFColorSelectViewDelegate
  225. - (void)selectColorView:(CPDFColorSelectView *)select {
  226. if (select == self.colorView) {
  227. if (@available(iOS 14.0, *)) {
  228. self.picker = [[UIColorPickerViewController alloc] init];
  229. self.picker.delegate = self;
  230. [self presentViewController:self.picker animated:YES completion:nil];
  231. } else {
  232. UIDevice *currentDevice = [UIDevice currentDevice];
  233. if (currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
  234. // This is an iPad
  235. self.colorPicker = [[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.colorPicker = [[CPDFColorPickerView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, 320)];
  239. }
  240. self.colorPicker.delegate = self;
  241. self.colorPicker.backgroundColor = [UIColor whiteColor];
  242. [self.view addSubview:self.colorPicker];
  243. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  244. }
  245. }
  246. }
  247. - (void)selectColorView:(CPDFColorSelectView *)select color:(UIColor *)color {
  248. if (select == self.colorView) {
  249. self.sampleView.color = color;
  250. self.annotStyle.color = color;
  251. [self.sampleView setNeedsDisplay];
  252. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  253. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  254. }
  255. } else if (select == self.fillColorSelectView) {
  256. self.sampleView.interiorColor = color;
  257. self.annotStyle.interiorColor = color;
  258. [self.sampleView setNeedsDisplay];
  259. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  260. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  261. }
  262. }
  263. }
  264. #pragma mark - CPDFColorPickerViewDelegate
  265. - (void)pickerView:(CPDFColorPickerView *)colorPickerView color:(UIColor *)color {
  266. if (colorPickerView == self.colorPicker) {
  267. self.sampleView.color = color;
  268. self.annotStyle.color = color;
  269. [self.sampleView setNeedsDisplay];
  270. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  271. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  272. }
  273. }
  274. CGFloat red, green, blue, alpha;
  275. [color getRed:&red green:&green blue:&blue alpha:&alpha];
  276. self.opacitySliderView.opacitySlider.value = alpha;
  277. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  278. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  279. }
  280. #pragma mark - UIColorPickerViewControllerDelegate
  281. - (void)colorPickerViewControllerDidFinish:(UIColorPickerViewController *)viewController API_AVAILABLE(ios(14.0)) {
  282. if (viewController == self.picker) {
  283. self.sampleView.color = viewController.selectedColor;
  284. self.annotStyle.color = self.sampleView.color;
  285. if (self.lineDelegate && [self.lineDelegate respondsToSelector:@selector(arrowViewController:annotStyle:)]) {
  286. [self.lineDelegate arrowViewController:self annotStyle:self.annotStyle];
  287. }
  288. [self.sampleView setNeedsDisplay];
  289. }
  290. CGFloat red, green, blue, alpha;
  291. [viewController.selectedColor getRed:&red green:&green blue:&blue alpha:&alpha];
  292. self.opacitySliderView.opacitySlider.value = alpha;
  293. self.opacitySliderView.startLabel.text = [NSString stringWithFormat:@"%d%%", (int)((self.opacitySliderView.opacitySlider.value/1)*100)];
  294. }
  295. @end