// // PDFSignatureColorViewController.m // PDFReader // // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved. // // The PDF Reader Sample applications are licensed with a modified BSD license. // Please see License for details. This notice may not be removed from this file. // #import "PDFSignatureColorViewController.h" @interface PDFSignatureColorViewController () @property (nonatomic,assign) IBOutlet UIScrollView *scrollView; @property (nonatomic,assign) IBOutlet UISlider *slider; @property (nonatomic,retain) UIView *selectedView; @end @implementation PDFSignatureColorViewController #pragma mark - Init Methods - (void)dealloc { Block_release(_callback); [_color release]; [_selectedView release]; [super dealloc]; } #pragma mark - UIViewController Methods - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.preferredContentSize = CGSizeMake(300, 130); _selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; _selectedView.backgroundColor = [UIColor clearColor]; _selectedView.layer.masksToBounds = YES; _selectedView.layer.borderColor = [UIColor lightGrayColor].CGColor; _selectedView.layer.borderWidth = 1.0; _selectedView.layer.cornerRadius = 25; [self.scrollView addSubview:_selectedView]; NSArray *colors = @[[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f], [UIColor colorWithRed:19.0f/255.0f green:51.0f/255.0f blue:144.0f/255.0f alpha:1.0f], [UIColor colorWithRed:39.0f/255.0f green:190.0f/255.0f blue:253.0f/255.0f alpha:1.0f], [UIColor colorWithRed:0.0f/255.0f green:255.0f/255.0f blue:54.0f/255.0f alpha:1.0f], [UIColor colorWithRed:252.0f/255.0f green:255.0f/255.0f blue:54.0f/255.0f alpha:1.0f], [UIColor colorWithRed:253.0f/255.0f green:126.0f/255.0f blue:21.0f/255.0f alpha:1.0f], [UIColor colorWithRed:254.0f/255.0f green:134.0f/255.0f blue:106.0f/255.0f alpha:1.0f], [UIColor colorWithRed:166.0f/255.0f green:29.0f/255.0f blue:251.0f/255.0f alpha:1.0f], [UIColor colorWithRed:1.0f green:27.0f/255.0f blue:137.0f/255.0f alpha:1.0f], [UIColor colorWithRed:221.0f/255.0f green:2.0f/255.0f blue:2.0f/255.0f alpha:1.0f], [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.0f]]; NSMutableArray *buttons = [NSMutableArray array]; CGFloat posX = 5; for (int i=0; i <[colors count]; i++){ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(posX, 5, 40, 40); button.backgroundColor = colors[i]; button.layer.masksToBounds = YES; button.layer.cornerRadius = 20; [button addTarget:self action:@selector(buttonItemClicked_Color:) forControlEvents:UIControlEventTouchUpInside]; [self.scrollView addSubview:button]; [buttons addObject:button]; if (self.color && [self isTheSameColor:self.color anotherColor:button.backgroundColor]) { self.selectedView.center = button.center; } posX += button.bounds.size.width+10; } self.scrollView.contentSize = CGSizeMake(posX, self.scrollView.bounds.size.height); self.slider.value = self.lineWidth; } #pragma mark - Button Actions - (IBAction)buttonItemClicked_Color:(UIButton *)button { self.selectedView.center = button.center; self.color = button.backgroundColor; if (self.callback) { self.callback(); } } - (IBAction)sliderValueChanged_LineWidth:(UISlider *)slider { self.lineWidth = slider.value; if (self.callback) { self.callback(); } } #pragma mark - Private Methods - (BOOL)isTheSameColor:(UIColor *)color1 anotherColor:(UIColor *)color2 { const CGFloat* component = CGColorGetComponents(color1.CGColor); const CGFloat* component1 = CGColorGetComponents(color2.CGColor); if (fabs(component[0]-component1[0])>1e-6) return NO; if (fabs(component[1]-component1[1])>1e-6) return NO; if (fabs(component[2]-component1[2])>1e-6) return NO; if (fabs(component[3]-component1[3])>1e-6) return NO; return YES; } #pragma mark - Public Methods - (void)showViewController:(UIViewController *)viewController inBarButtonItem:(UIBarButtonItem *)barButtonItem { self.modalPresentationStyle = UIModalPresentationPopover; UIPopoverPresentationController *popVC = self.popoverPresentationController; popVC.delegate = self; popVC.barButtonItem = barButtonItem; popVC.permittedArrowDirections = UIPopoverArrowDirectionUp; [viewController presentViewController:self animated:YES completion:nil]; } #pragma mark - UIPopoverPresentationControllerDelegate - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection { return UIModalPresentationNone; } @end