PDFShapeViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // PDFShapeViewController.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 "PDFShapeViewController.h"
  11. #import "ColorView.h"
  12. @interface PDFShapeViewController () <UIPopoverPresentationControllerDelegate>
  13. @property (nonatomic,assign) IBOutlet UIView *contentView;
  14. @property (nonatomic,assign) IBOutlet ColorView *colorView;
  15. @property (nonatomic,assign) IBOutlet UISegmentedControl *segmentedControl;
  16. @property (nonatomic,assign) IBOutlet UISlider *opacitySlider;
  17. @property (nonatomic,assign) IBOutlet UISlider *thicknessSlider;
  18. @property (nonatomic,assign) IBOutlet UILabel *opacityMinLabel;
  19. @property (nonatomic,assign) IBOutlet UIButton *style1Button;
  20. @property (nonatomic,assign) IBOutlet UIButton *style2Button;
  21. @property (nonatomic,assign) IBOutlet UIButton *style3Button;
  22. @property (nonatomic,assign) IBOutlet UIButton *style4Button;
  23. @property (nonatomic,retain) UIView *styleSelectedView;
  24. @end
  25. @implementation PDFShapeViewController
  26. #pragma mark Init Methods
  27. - (void)dealloc {
  28. Block_release(_callback);
  29. [_styleSelectedView release];
  30. [_color release];
  31. [_fillColor release];
  32. [super dealloc];
  33. }
  34. #pragma mark View Methods
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view from its nib.
  38. _styleSelectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
  39. _styleSelectedView.backgroundColor = [UIColor lightGrayColor];
  40. _styleSelectedView.layer.masksToBounds = YES;
  41. _styleSelectedView.layer.cornerRadius = 5;
  42. [self.view addSubview:_styleSelectedView];
  43. __block __typeof(self) blockSelf = self;
  44. self.colorView.callback = ^(UIColor *color) {
  45. if (blockSelf.segmentedControl.selectedSegmentIndex == 0) {
  46. blockSelf.color = color;
  47. } else {
  48. blockSelf.fillColor = color;
  49. }
  50. if (blockSelf.callback) {
  51. blockSelf.callback();
  52. }
  53. };
  54. self.colorView.allowsClearColor = YES;
  55. self.thicknessSlider.value = self.borderWidth;
  56. [self updateSelectedView];
  57. }
  58. - (void)viewWillLayoutSubviews {
  59. [super viewWillLayoutSubviews];
  60. if (self.allowsShowStyle) {
  61. self.preferredContentSize = CGSizeMake(300, self.contentView.bounds.size.height);
  62. } else {
  63. self.preferredContentSize = CGSizeMake(300, self.contentView.bounds.size.height-78);
  64. }
  65. UIButton *button = nil;
  66. if (self.style == 0) {
  67. button = self.style1Button;
  68. } else if (self.style == 1) {
  69. button = self.style2Button;
  70. } else if (self.style == 2) {
  71. button = self.style3Button;
  72. } else if (self.style == 3) {
  73. button = self.style4Button;
  74. }
  75. self.styleSelectedView.center = CGPointMake(button.center.x, button.frame.origin.y-self.styleSelectedView.bounds.size.height/2.0);
  76. }
  77. - (void)updateSelectedView {
  78. if (self.segmentedControl.selectedSegmentIndex == 0) {
  79. self.colorView.color = self.color;
  80. } else {
  81. self.colorView.color = self.fillColor;
  82. }
  83. if (self.segmentedControl.selectedSegmentIndex == 0) {
  84. self.opacitySlider.minimumValue = 25;
  85. self.opacitySlider.maximumValue = 100;
  86. self.opacitySlider.value = self.opacity;
  87. self.opacityMinLabel.text = @"25%";
  88. } else {
  89. self.opacitySlider.minimumValue = 0;
  90. self.opacitySlider.maximumValue = 100;
  91. self.opacitySlider.value = self.fillOpacity;
  92. self.opacityMinLabel.text = @"0%";
  93. }
  94. }
  95. #pragma mark - Button Actions
  96. - (IBAction)segmentedControlValueChanged_Mode:(id)sender {
  97. [self updateSelectedView];
  98. }
  99. - (IBAction)buttonItemClicked_Style:(UIButton *)button {
  100. self.style = button.tag;
  101. self.styleSelectedView.center = CGPointMake(button.center.x, button.frame.origin.y-self.styleSelectedView.bounds.size.height/2.0);
  102. if (self.callback) {
  103. self.callback();
  104. }
  105. }
  106. - (IBAction)sliderValueChanged_Opacity:(UISlider *)slider {
  107. if (self.segmentedControl.selectedSegmentIndex == 0) {
  108. self.opacity = slider.value;
  109. } else {
  110. self.fillOpacity = slider.value;
  111. }
  112. if (self.callback) {
  113. self.callback();
  114. }
  115. }
  116. - (IBAction)sliderValueChanged_BorderWidth:(UISlider *)slider {
  117. self.borderWidth = slider.value;
  118. if (self.callback) {
  119. self.callback();
  120. }
  121. }
  122. #pragma mark - Public Methods
  123. - (void)showViewController:(UIViewController *)viewController inRect:(CGRect)rect {
  124. self.modalPresentationStyle = UIModalPresentationPopover;
  125. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  126. popVC.delegate = self;
  127. popVC.sourceRect = rect;
  128. popVC.sourceView = viewController.view;
  129. [viewController presentViewController:self animated:YES completion:nil];
  130. }
  131. #pragma mark - UIPopoverPresentationControllerDelegate
  132. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  133. return UIModalPresentationNone;
  134. }
  135. @end