// // 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 - (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.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 yellowColor]; 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:)]; self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn,settingBtn , nil]; [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_alert:[NSString stringWithFormat:@"Line width has been set to %d.",newlinewidth] title:@"Line width should between 1 and 16" controller:self ]; } [self.signatureView setLineWidth:self.linewidth]; // UITextField *name = alertControl.textFields.firstObject; // if(name.text.length==0) // { // [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self]; // } // else // { // self.save_name = name.text; // self.btnsave.enabled =false; // // NSMutableDictionary * info = // if(self.onSavePDF) // self.onSavePDF(self.save_name,self.filename); // } }]; 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 { NSLog(@"onDone"); UIImage* img = [self.signatureView signatureImage]; // return ; // CGSize imgsize = img.size; NSData* data = [self.signatureView signatureData]; if (self.delegate && [self.delegate respondsToSelector:@selector(SignatureVCReturnImage:indexPath:)]) { [self.delegate SignatureVCReturnImage:img indexPath:self.indexPath]; } if (self.delegate && [self.delegate respondsToSelector:@selector(SignatureVCReturnData:indexPath:)]) { [self.delegate SignatureVCReturnData:data indexPath:self.indexPath]; } if(self.onReturnImg) self.onReturnImg(img); [self.navigationController popViewControllerAnimated:true]; // [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; } @end