PDFFreehandView.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // PDFFreehandView.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 "PDFFreehandView.h"
  11. #import <ComPDFKit/ComPDFKit.h>
  12. #import "PDFFreehandViewController.h"
  13. @interface PDFFreehandView ()
  14. @property (nonatomic,retain) UIView *colorView;
  15. @end
  16. @implementation PDFFreehandView
  17. #pragma mark - Super Method
  18. - (instancetype)initWithFrame:(CGRect)frame {
  19. if (self = [super initWithFrame:frame]) {
  20. self.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
  21. self.layer.borderColor = [UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1.0].CGColor;
  22. self.layer.borderWidth = 1.0;
  23. self.layer.cornerRadius = 5.0;
  24. CGFloat width = frame.size.width/6;
  25. CGFloat height = frame.size.height;
  26. UIButton *undoButton = [UIButton buttonWithType:UIButtonTypeCustom];
  27. undoButton.tag = 0;
  28. undoButton.frame = CGRectMake(0, 0, width, height);
  29. [undoButton setImage:[UIImage imageNamed:@"btn_undo.png"] forState:UIControlStateNormal];
  30. [undoButton addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  31. [self addSubview:undoButton];
  32. UIButton *redoButton = [UIButton buttonWithType:UIButtonTypeCustom];
  33. redoButton.tag = 1;
  34. redoButton.frame = CGRectMake(width, 0, width, height);
  35. [redoButton setImage:[UIImage imageNamed:@"btn_redo.png"] forState:UIControlStateNormal];
  36. [redoButton addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  37. [self addSubview:redoButton];
  38. UIButton *eraseButton = [UIButton buttonWithType:UIButtonTypeCustom];
  39. eraseButton.tag = 2;
  40. eraseButton.frame = CGRectMake(2*width, 0, width, height);
  41. [eraseButton setImage:[UIImage imageNamed:@"btn_eraser_00.png"] forState:UIControlStateNormal];
  42. [eraseButton setImage:[UIImage imageNamed:@"btn_eraser_01.png"] forState:UIControlStateSelected];
  43. [eraseButton addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  44. [self addSubview:eraseButton];
  45. UIButton *propertyButton = [UIButton buttonWithType:UIButtonTypeCustom];
  46. propertyButton.tag = 3;
  47. propertyButton.frame = CGRectMake(3*width, 0, width, height);
  48. [propertyButton setImage:[UIImage imageNamed:@"btn_brush_a00.png"] forState:UIControlStateNormal];
  49. [propertyButton addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  50. [self addSubview:propertyButton];
  51. _colorView = [[UIView alloc] initWithFrame:CGRectMake((width-8)/2.0, 6, 8, 8)];
  52. _colorView.layer.cornerRadius = _colorView.bounds.size.width/2.0;
  53. _colorView.backgroundColor = CPDFKitShareConfig.freehandAnnotationColor;
  54. [propertyButton addSubview:_colorView];
  55. UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeSystem];
  56. clearButton.tag = 4;
  57. clearButton.frame = CGRectMake(4*width, 0, width, height);
  58. [clearButton setTitle:NSLocalizedString(@"Clear", nil) forState:UIControlStateNormal];
  59. [clearButton setTitleColor:[UIColor colorWithRed:243.0/255.0 green:0 blue:8.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  60. clearButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
  61. [clearButton addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  62. [self addSubview:clearButton];
  63. UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeSystem];
  64. saveButton.tag = 5;
  65. saveButton.frame = CGRectMake(5*width, 0, width, height);
  66. [saveButton setTitle:NSLocalizedString(@"Done", nil) forState:UIControlStateNormal];
  67. [saveButton setTitleColor:[UIColor colorWithRed:41.0/255.0 green:111.0/255.0 blue:173.0/255.0 alpha:1.0] forState:UIControlStateNormal];
  68. saveButton.titleLabel.font = [UIFont systemFontOfSize:15.0];
  69. [saveButton addTarget:self action:@selector(buttonItemClicked_Switch:) forControlEvents:UIControlEventTouchUpInside];
  70. [self addSubview:saveButton];
  71. }
  72. return self;
  73. }
  74. - (void)dealloc {
  75. Block_release(_callback);
  76. [_colorView release];
  77. [super dealloc];
  78. }
  79. #pragma mark - Button Actions
  80. - (void)buttonItemClicked_Switch:(UIButton *)button {
  81. if (button.tag == 3) {
  82. __block PDFFreehandViewController *vc = [[PDFFreehandViewController alloc] init];
  83. vc.color = CPDFKitShareConfig.freehandAnnotationColor;
  84. vc.opacity = CPDFKitShareConfig.freehandAnnotationOpacity;
  85. vc.borderWidth = CPDFKitShareConfig.freehandAnnotationBorderWidth;
  86. vc.callback = ^{
  87. if (self.callback) {
  88. self.callback(button.tag, button.isSelected);
  89. }
  90. self.colorView.backgroundColor = vc.color;
  91. CPDFKitShareConfig.freehandAnnotationColor = vc.color;
  92. CPDFKitShareConfig.freehandAnnotationOpacity = vc.opacity;
  93. CPDFKitShareConfig.freehandAnnotationBorderWidth = vc.borderWidth;
  94. };
  95. UIViewController *viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  96. CGRect rect = [button.superview convertRect:button.frame toView:viewController.view];
  97. [vc showViewController:viewController inRect:rect];
  98. [vc release];
  99. } else {
  100. if (button.tag == 2) {
  101. button.selected = !button.isSelected;
  102. if (button.isSelected) {
  103. button.backgroundColor = [UIColor lightGrayColor];
  104. } else {
  105. button.backgroundColor = [UIColor clearColor];
  106. }
  107. }
  108. if (self.callback) {
  109. self.callback(button.tag, button.isSelected);
  110. }
  111. }
  112. }
  113. @end