PDFFreehandViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // PDFFreehandViewController.h
  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 "PDFFreehandViewController.h"
  11. #import "ColorView.h"
  12. @interface PDFFreehandDrawView : UIView
  13. @property (nonatomic,retain) UIColor *lineColor;
  14. @property (nonatomic,assign) CGFloat lineWidth;
  15. @property (nonatomic,assign) CGFloat lineOpacity;
  16. @end
  17. @implementation PDFFreehandDrawView
  18. - (id)initWithFrame:(CGRect)frame {
  19. if (self = [super initWithFrame:frame]) {
  20. _lineColor = [[UIColor redColor] retain];
  21. _lineOpacity = 0.7;
  22. _lineWidth = 3.0;
  23. }
  24. return self;
  25. }
  26. - (void)drawRect:(CGRect)rect {
  27. [super drawRect:rect];
  28. CGContextRef ctx = UIGraphicsGetCurrentContext();
  29. if (!ctx) {
  30. return;
  31. }
  32. float tOffsetX = 20.0;
  33. float tSpaceX = (self.bounds.size.width - 2*tOffsetX) / 3.0;
  34. float tSpaceY = 50.0;
  35. CGContextSetRGBFillColor(ctx,240.0/255.0, 240.0/255.0, 240.0/255.0, 1.0);
  36. CGContextFillRect(ctx, self.bounds);
  37. CGContextSetStrokeColorWithColor(ctx, _lineColor.CGColor);
  38. CGContextSetAlpha(ctx, _lineOpacity);
  39. CGContextSetLineWidth(ctx, _lineWidth/1.2);
  40. CGContextMoveToPoint(ctx, tOffsetX, self.bounds.size.height/2.0);//设置Path的起点
  41. CGContextAddCurveToPoint(ctx, tOffsetX + tSpaceX, self.bounds.size.height/2.0 - tSpaceY,
  42. tOffsetX + tSpaceX*2, self.bounds.size.height/2.0 + tSpaceY,
  43. tOffsetX + tSpaceX*3, self.bounds.size.height/2.0);
  44. CGContextStrokePath(ctx);
  45. }
  46. - (void)dealloc {
  47. [_lineColor release];
  48. [super dealloc];
  49. }
  50. @end
  51. @interface PDFFreehandViewController () <UIPopoverPresentationControllerDelegate>
  52. @property (nonatomic,assign) IBOutlet UIView *contentView;
  53. @property (nonatomic,assign) IBOutlet PDFFreehandDrawView *drawView;
  54. @property (nonatomic,assign) IBOutlet ColorView *colorView;
  55. @property (nonatomic,assign) IBOutlet UISlider *opacitySlider;
  56. @property (nonatomic,assign) IBOutlet UISlider *thicknessSlider;
  57. @property (nonatomic,assign) IBOutlet UILabel *thicknessMinLabel;
  58. @property (nonatomic,assign) IBOutlet UILabel *thicknessMaxLabel;
  59. @property (nonatomic,assign) IBOutlet UIButton *style1Button;
  60. @property (nonatomic,assign) IBOutlet UIButton *style2Button;
  61. @property (nonatomic,retain) UIView *styleSelectedView;
  62. @end
  63. @implementation PDFFreehandViewController
  64. #pragma mark - Init Methods
  65. - (void)dealloc {
  66. Block_release(_callback);
  67. [_styleSelectedView release];
  68. [_color release];
  69. [super dealloc];
  70. }
  71. #pragma mark - UIViewController Methods
  72. - (void)viewDidLoad {
  73. [super viewDidLoad];
  74. // Do any additional setup after loading the view from its nib.
  75. _styleSelectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
  76. _styleSelectedView.backgroundColor = [UIColor lightGrayColor];
  77. _styleSelectedView.layer.masksToBounds = YES;
  78. _styleSelectedView.layer.cornerRadius = 5;
  79. [self.view addSubview:_styleSelectedView];
  80. __block __typeof(self) blockSelf = self;
  81. self.colorView.callback = ^(UIColor *color) {
  82. blockSelf.color = color;
  83. [blockSelf updateView];
  84. if (blockSelf.callback) {
  85. blockSelf.callback();
  86. }
  87. };
  88. self.colorView.color = self.color;
  89. self.opacitySlider.value = self.opacity;
  90. [self updateView];
  91. }
  92. - (void)viewWillLayoutSubviews {
  93. [super viewWillLayoutSubviews];
  94. self.preferredContentSize = CGSizeMake(300, self.contentView.bounds.size.height);
  95. UIButton *button = nil;
  96. if (self.style == 0) {
  97. button = self.style1Button;
  98. } else if (self.style == 1) {
  99. button = self.style2Button;
  100. }
  101. self.styleSelectedView.center = CGPointMake(button.center.x, button.frame.origin.y-self.styleSelectedView.bounds.size.height/2.0);
  102. }
  103. - (void)updateView {
  104. self.drawView.lineColor = self.color;
  105. self.drawView.lineOpacity = self.opacity/100;
  106. self.drawView.lineWidth = self.borderWidth;
  107. [self.drawView setNeedsDisplay];
  108. if (self.style == 0) {
  109. self.thicknessSlider.minimumValue = 0.5;
  110. self.thicknessSlider.maximumValue = 10;
  111. self.thicknessMinLabel.text = @"0.5";
  112. self.thicknessMaxLabel.text = @"10";
  113. } else {
  114. self.thicknessSlider.minimumValue = 10;
  115. self.thicknessSlider.maximumValue = 30;
  116. self.thicknessMinLabel.text = @"10";
  117. self.thicknessMaxLabel.text = @"30";
  118. }
  119. self.thicknessSlider.value = self.borderWidth;
  120. }
  121. #pragma mark - Button Actions
  122. - (IBAction)buttonItemClicked_Style:(UIButton *)button {
  123. self.style = button.tag;
  124. self.styleSelectedView.center = CGPointMake(button.center.x, button.frame.origin.y-self.styleSelectedView.bounds.size.height/2.0);
  125. [self updateView];
  126. if (self.callback) {
  127. self.callback();
  128. }
  129. }
  130. - (IBAction)sliderValueChanged_Opacity:(UISlider *)slider {
  131. self.opacity = slider.value;
  132. [self updateView];
  133. if (self.callback) {
  134. self.callback();
  135. }
  136. }
  137. - (IBAction)sliderValueChanged_BorderWidth:(UISlider *)slider {
  138. self.borderWidth = slider.value;
  139. [self updateView];
  140. if (self.callback) {
  141. self.callback();
  142. }
  143. }
  144. #pragma mark - Public Methods
  145. - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect {
  146. self.modalPresentationStyle = UIModalPresentationPopover;
  147. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  148. popVC.delegate = self;
  149. popVC.sourceRect = rect;
  150. popVC.sourceView = viewController.view;
  151. [viewController presentViewController:self animated:YES completion:nil];
  152. }
  153. #pragma mark - UIPopoverPresentationControllerDelegate
  154. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  155. return UIModalPresentationNone;
  156. }
  157. @end