PDFSignatureColorViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // PDFSignatureColorViewController.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 "PDFSignatureColorViewController.h"
  11. @interface PDFSignatureColorViewController () <UIPopoverPresentationControllerDelegate>
  12. @property (nonatomic,assign) IBOutlet UIScrollView *scrollView;
  13. @property (nonatomic,assign) IBOutlet UISlider *slider;
  14. @property (nonatomic,retain) UIView *selectedView;
  15. @end
  16. @implementation PDFSignatureColorViewController
  17. #pragma mark - Init Methods
  18. - (void)dealloc {
  19. Block_release(_callback);
  20. [_color release];
  21. [_selectedView release];
  22. [super dealloc];
  23. }
  24. #pragma mark - UIViewController Methods
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view from its nib.
  28. self.preferredContentSize = CGSizeMake(300, 130);
  29. _selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
  30. _selectedView.backgroundColor = [UIColor clearColor];
  31. _selectedView.layer.masksToBounds = YES;
  32. _selectedView.layer.borderColor = [UIColor lightGrayColor].CGColor;
  33. _selectedView.layer.borderWidth = 1.0;
  34. _selectedView.layer.cornerRadius = 25;
  35. [self.scrollView addSubview:_selectedView];
  36. NSArray *colors = @[[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f],
  37. [UIColor colorWithRed:19.0f/255.0f green:51.0f/255.0f blue:144.0f/255.0f alpha:1.0f],
  38. [UIColor colorWithRed:39.0f/255.0f green:190.0f/255.0f blue:253.0f/255.0f alpha:1.0f],
  39. [UIColor colorWithRed:0.0f/255.0f green:255.0f/255.0f blue:54.0f/255.0f alpha:1.0f],
  40. [UIColor colorWithRed:252.0f/255.0f green:255.0f/255.0f blue:54.0f/255.0f alpha:1.0f],
  41. [UIColor colorWithRed:253.0f/255.0f green:126.0f/255.0f blue:21.0f/255.0f alpha:1.0f],
  42. [UIColor colorWithRed:254.0f/255.0f green:134.0f/255.0f blue:106.0f/255.0f alpha:1.0f],
  43. [UIColor colorWithRed:166.0f/255.0f green:29.0f/255.0f blue:251.0f/255.0f alpha:1.0f],
  44. [UIColor colorWithRed:1.0f green:27.0f/255.0f blue:137.0f/255.0f alpha:1.0f],
  45. [UIColor colorWithRed:221.0f/255.0f green:2.0f/255.0f blue:2.0f/255.0f alpha:1.0f],
  46. [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f]];
  47. NSMutableArray *buttons = [NSMutableArray array];
  48. CGFloat posX = 5;
  49. for (int i=0; i <[colors count]; i++){
  50. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  51. button.frame = CGRectMake(posX, 5, 40, 40);
  52. button.backgroundColor = colors[i];
  53. button.layer.masksToBounds = YES;
  54. button.layer.cornerRadius = 20;
  55. [button addTarget:self action:@selector(buttonItemClicked_Color:) forControlEvents:UIControlEventTouchUpInside];
  56. [self.scrollView addSubview:button];
  57. [buttons addObject:button];
  58. if (self.color && [self isTheSameColor:self.color anotherColor:button.backgroundColor]) {
  59. self.selectedView.center = button.center;
  60. }
  61. posX += button.bounds.size.width+10;
  62. }
  63. self.scrollView.contentSize = CGSizeMake(posX, self.scrollView.bounds.size.height);
  64. self.slider.value = self.lineWidth;
  65. }
  66. #pragma mark - Button Actions
  67. - (IBAction)buttonItemClicked_Color:(UIButton *)button {
  68. self.selectedView.center = button.center;
  69. self.color = button.backgroundColor;
  70. if (self.callback) {
  71. self.callback();
  72. }
  73. }
  74. - (IBAction)sliderValueChanged_LineWidth:(UISlider *)slider {
  75. self.lineWidth = slider.value;
  76. if (self.callback) {
  77. self.callback();
  78. }
  79. }
  80. #pragma mark - Private Methods
  81. - (BOOL)isTheSameColor:(UIColor *)color1 anotherColor:(UIColor *)color2 {
  82. const CGFloat* component = CGColorGetComponents(color1.CGColor);
  83. const CGFloat* component1 = CGColorGetComponents(color2.CGColor);
  84. if (fabs(component[0]-component1[0])>1e-6) return NO;
  85. if (fabs(component[1]-component1[1])>1e-6) return NO;
  86. if (fabs(component[2]-component1[2])>1e-6) return NO;
  87. if (fabs(component[3]-component1[3])>1e-6) return NO;
  88. return YES;
  89. }
  90. #pragma mark - Public Methods
  91. - (void)showViewController:(UIViewController *)viewController inBarButtonItem:(UIBarButtonItem *)barButtonItem {
  92. self.modalPresentationStyle = UIModalPresentationPopover;
  93. UIPopoverPresentationController *popVC = self.popoverPresentationController;
  94. popVC.delegate = self;
  95. popVC.barButtonItem = barButtonItem;
  96. popVC.permittedArrowDirections = UIPopoverArrowDirectionUp;
  97. [viewController presentViewController:self animated:YES completion:nil];
  98. }
  99. #pragma mark - UIPopoverPresentationControllerDelegate
  100. - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
  101. return UIModalPresentationNone;
  102. }
  103. @end