// // SignatureViewController.m // RedAnt ERP Mobile // // Created by Ray on 14-8-7. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved. // #define INTNUMBERS @"0123456789\n" #import "SignatureViewController.h" #import "RAUtils.h" //#import "const.h" @interface SignatureViewController () @end @implementation SignatureViewController + (instancetype)ra_signatureViewControllerWithCompletion:(void (^)(UIImage *))blk { SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SignatureViewController"]; vc.onReturnImg = blk; return vc; } - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } -(BOOL) shouldAutorotate { return false; } - (void)onBackClick:(UIButton *)sender { [self.navigationController popViewControllerAnimated:FALSE]; } - (void)viewDidLoad { [super viewDidLoad]; // self.view.backgroundColor = [UIColor redColor]; self.navigationItem.title = self.title; if(self.linewidth==0) self.linewidth=5; EAGLContext *context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2]; if(true/*||self.signature_ratio.width<=0||self.signature_ratio.height<=0*/) { self.signatureView = [[SignatureView alloc] initWithFrame:self.view.frame context:context]; // self.signatureView= [[SignatureView alloc] initWithFrame:self.view.frame]; } // else // { // CGRect rect=CGRectMake(5, 90, self.view.frame.size.width-10, self.view.frame.size.height-95); // // CGSize signatureView_size=CGSizeMake(rect.size.width, rect.size.width*self.signature_ratio.height/self.signature_ratio.width); // // if(signatureView_size.height>rect.size.height) // signatureView_size=CGSizeMake(rect.size.height*self.signature_ratio.width/self.signature_ratio.height,rect.size.height); // // self.signatureView= [[SignatureView alloc] initWithFrame:CGRectMake((rect.size.width-signatureView_size.width)/2+5, (rect.size.height-signatureView_size.height)/2+90, signatureView_size.width, signatureView_size.height)]; // } // self.signatureView.backgroundColor = [UIColor clearColor]; self.signatureView.layer.borderColor = [UIColor darkGrayColor].CGColor; self.signatureView.layer.borderWidth = 1.0; [self.view addSubview:self.signatureView]; UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onBackClick:)]; // closeButton.tintColor = UIColorFromRGB(0x996633); self.navigationItem.leftBarButtonItem=closeButton; UIBarButtonItem *clearBtn=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"clear"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onClear:)]; // clearBtn.tintColor = UIColorFromRGB(0x996633); UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onDone:)]; // doneBtn.tintColor = UIColorFromRGB(0x996633); // id aa = self.navigationItem; // self.navi_item.title = @"ttttt"; // self.navigationItem.rightBarButtonItem = searchButton; UIBarButtonItem * settingBtn =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"signature_setting"] imageWithRenderingMode:UIImageRenderingModeAutomatic] style:UIBarButtonItemStylePlain target:self action:@selector( onSetting:)]; // UIBarButtonItem * settingBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose // target:self // action:@selector( onSetting:)]; if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPhone) { self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn , nil]; } else { self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn,settingBtn , nil]; } [self.navigationController.navigationBar layoutIfNeeded]; [self.signatureView setLineWidth:self.linewidth]; self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000]; // UIImage* img = [UIImage alloc]initw // cgimagecre //self.existSignature没有调用,已去掉 // if(self.existSignature!= nil) // { // CGSize size =self.signatureView.bounds.size; // self.signatureView.image = [self addImage:self.existSignature size:size] ; // self.signatureView.boundary =CGRectMake((size.width-self.existSignature.size.width)/2,(size.height-self.existSignature.size.height)/2, self.existSignature.size.width, self.existSignature.size.height); // } // Do any additional setup after loading the view. } - (UIImage *)addImage:(UIImage *)image size:(CGSize )size { UIGraphicsBeginImageContext(size); UIImage *cleanImage = UIGraphicsGetImageFromCurrentImageContext(); // Draw image1 [image drawInRect:CGRectMake((size.width-image.size.width)/2,(size.height-image.size.height)/2, image.size.width, image.size.height)]; // Draw image2 [cleanImage drawInRect:CGRectMake(0, 0, cleanImage.size.width, cleanImage.size.height)]; UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage; } - (void)onSetting:(UIButton *)sender { UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change line width" message:nil preferredStyle:UIAlertControllerStyleAlert]; //block代码块取代了delegate [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.text = [NSString stringWithFormat:@"%d",self.linewidth]; textField.keyboardType=UIKeyboardTypeNumberPad; textField.delegate = self; }]; UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { if(self.linewidth>16||self.linewidth<1) { int newlinewidth = 0; if(self.linewidth>16) { self.linewidth = 16; newlinewidth=16; } else if(self.linewidth<1) { self.linewidth = 1; newlinewidth=1; } [RAUtils message_box:@"Line width should between 1 and 16" message:[NSString stringWithFormat:@"Line width has been set to %d.",newlinewidth] completion:nil]; } [self.signatureView setLineWidth:self.linewidth]; }]; UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { // DebugLog(@"Cancel"); }]; [alertControl addAction:actionOne]; [alertControl addAction:alertthree]; //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField) [self presentViewController:alertControl animated:YES completion:nil]; } - (void)onDone:(UIButton *)sender { DebugLog(@"onDone"); UIImage* img = [self.signatureView signatureImage]; // return ; // CGSize imgsize = img.size; NSData* data = [self.signatureView signatureData]; if (self.Signaturedelegate && [self.Signaturedelegate respondsToSelector:@selector(SignatureVCReturnImage:indexPath:)]) { [self.Signaturedelegate SignatureVCReturnImage:img indexPath:self.indexPath]; } if (self.Signaturedelegate && [self.Signaturedelegate respondsToSelector:@selector(SignatureVCReturnData:indexPath:)]) { [self.Signaturedelegate SignatureVCReturnData:data indexPath:self.indexPath]; } [self.navigationController popViewControllerAnimated:true]; if(self.onReturnImg) self.onReturnImg(img); // [self.aBNewPersonNav dismissViewControllerAnimated:true completion:^{ // ; // }]; } - (void)onClear:(UIButton *)sender { [self.signatureView clear]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark textField delegate -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:INTNUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:filtered]; return canChange; } - (void)textFieldDidEndEditing:(UITextField *)textField { self.linewidth = [textField.text intValue]; } //- (void)textFieldDidBeginEditing:(UITextField *)textField //{ // DebugLog(@"textField shouldChangeCharactersInRange"); // // self.lastedit = textField; // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview; // self.lastedit_from = [self.editorTable indexPathForCell:cell]; //} /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ //- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { // self.signatureView.frame = self.view.bounds; //} - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id _Nonnull context) { // what ever you want to prepare } completion:^(id _Nonnull context) { self.signatureView.frame = self.view.bounds; }]; } @end