SignatureViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // SignatureViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 14-8-7.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #define INTNUMBERS @"0123456789\n"
  9. #import "SignatureViewController.h"
  10. #import "RAUtils.h"
  11. //#import "const.h"
  12. @interface SignatureViewController ()
  13. @end
  14. @implementation SignatureViewController
  15. + (instancetype)ra_signatureViewControllerWithCompletion:(void (^)(UIImage *))blk {
  16. SignatureViewController * vc =[ [UIStoryboard storyboardWithName:@"signature"
  17. bundle:[NSBundle mainBundle]]
  18. instantiateViewControllerWithIdentifier:@"SignatureViewController"];
  19. vc.onReturnImg = blk;
  20. return vc;
  21. }
  22. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  23. {
  24. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  25. if (self) {
  26. // Custom initialization
  27. }
  28. return self;
  29. }
  30. -(BOOL) shouldAutorotate
  31. {
  32. return false;
  33. }
  34. - (void)onBackClick:(UIButton *)sender {
  35. [self.navigationController popViewControllerAnimated:FALSE];
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. // self.view.backgroundColor = [UIColor redColor];
  41. self.navigationItem.title = self.title;
  42. if(self.linewidth==0)
  43. self.linewidth=5;
  44. EAGLContext *context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
  45. if(true||self.signature_ratio.width<=0||self.signature_ratio.height<=0)
  46. {
  47. self.signatureView = [[SignatureView alloc] initWithFrame:self.view.frame context:context];
  48. // self.signatureView= [[SignatureView alloc] initWithFrame:self.view.frame];
  49. }
  50. else
  51. {
  52. CGRect rect=CGRectMake(5, 90, self.view.frame.size.width-10, self.view.frame.size.height-95);
  53. CGSize signatureView_size=CGSizeMake(rect.size.width, rect.size.width*self.signature_ratio.height/self.signature_ratio.width);
  54. if(signatureView_size.height>rect.size.height)
  55. signatureView_size=CGSizeMake(rect.size.height*self.signature_ratio.width/self.signature_ratio.height,rect.size.height);
  56. 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)];
  57. }
  58. // self.signatureView.backgroundColor = [UIColor clearColor];
  59. self.signatureView.layer.borderColor = [UIColor darkGrayColor].CGColor;
  60. self.signatureView.layer.borderWidth = 1.0;
  61. [self.view addSubview:self.signatureView];
  62. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  63. style:UIBarButtonItemStylePlain
  64. target:self
  65. action:@selector( onBackClick:)];
  66. // closeButton.tintColor = UIColorFromRGB(0x996633);
  67. self.navigationItem.leftBarButtonItem=closeButton;
  68. UIBarButtonItem *clearBtn=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"clear"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  69. style:UIBarButtonItemStylePlain
  70. target:self
  71. action:@selector( onClear:)];
  72. // clearBtn.tintColor = UIColorFromRGB(0x996633);
  73. UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"save"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  74. style:UIBarButtonItemStylePlain
  75. target:self
  76. action:@selector( onDone:)];
  77. // doneBtn.tintColor = UIColorFromRGB(0x996633);
  78. // id aa = self.navigationItem;
  79. // self.navi_item.title = @"ttttt";
  80. // self.navigationItem.rightBarButtonItem = searchButton;
  81. UIBarButtonItem * settingBtn =[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"signature_setting"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  82. style:UIBarButtonItemStylePlain
  83. target:self
  84. action:@selector( onSetting:)];
  85. // UIBarButtonItem * settingBtn =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
  86. // target:self
  87. // action:@selector( onSetting:)];
  88. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  89. self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn , nil];
  90. } else {
  91. self.navigationItem.rightBarButtonItems=[NSArray arrayWithObjects:doneBtn,clearBtn,settingBtn , nil];
  92. }
  93. [self.navigationController.navigationBar layoutIfNeeded];
  94. [self.signatureView setLineWidth:self.linewidth];
  95. self.signatureView.foregroundLineColor = [UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000];
  96. // UIImage* img = [UIImage alloc]initw
  97. // cgimagecre
  98. //self.existSignature没有调用,已去掉
  99. // if(self.existSignature!= nil)
  100. // {
  101. // CGSize size =self.signatureView.bounds.size;
  102. // self.signatureView.image = [self addImage:self.existSignature size:size] ;
  103. // 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);
  104. // }
  105. // Do any additional setup after loading the view.
  106. }
  107. - (UIImage *)addImage:(UIImage *)image size:(CGSize )size {
  108. UIGraphicsBeginImageContext(size);
  109. UIImage *cleanImage = UIGraphicsGetImageFromCurrentImageContext();
  110. // Draw image1
  111. [image drawInRect:CGRectMake((size.width-image.size.width)/2,(size.height-image.size.height)/2, image.size.width, image.size.height)];
  112. // Draw image2
  113. [cleanImage drawInRect:CGRectMake(0, 0, cleanImage.size.width, cleanImage.size.height)];
  114. UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
  115. UIGraphicsEndImageContext();
  116. return resultingImage;
  117. }
  118. - (void)onSetting:(UIButton *)sender {
  119. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Change line width" message:nil preferredStyle:UIAlertControllerStyleAlert];
  120. //block代码块取代了delegate
  121. [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  122. textField.text = [NSString stringWithFormat:@"%d",self.linewidth];
  123. textField.keyboardType=UIKeyboardTypeNumberPad;
  124. textField.delegate = self;
  125. }];
  126. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Save" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  127. if(self.linewidth>16||self.linewidth<1)
  128. {
  129. int newlinewidth = 0;
  130. if(self.linewidth>16)
  131. {
  132. self.linewidth = 16;
  133. newlinewidth=16;
  134. }
  135. else if(self.linewidth<1)
  136. {
  137. self.linewidth = 1;
  138. newlinewidth=1;
  139. }
  140. [RAUtils message_alert:[NSString stringWithFormat:@"Line width has been set to %d.",newlinewidth] title:@"Line width should between 1 and 16" controller:self ];
  141. }
  142. [self.signatureView setLineWidth:self.linewidth];
  143. // UITextField *name = alertControl.textFields.firstObject;
  144. // if(name.text.length==0)
  145. // {
  146. // [RAUtils message_alert:@"Cannot save without name. Save canceled." title:@"Name is missing" controller:self];
  147. // }
  148. // else
  149. // {
  150. // self.save_name = name.text;
  151. // self.btnsave.enabled =false;
  152. // // NSMutableDictionary * info =
  153. // if(self.onSavePDF)
  154. // self.onSavePDF(self.save_name,self.filename);
  155. // }
  156. }];
  157. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  158. // DebugLog(@"Cancel");
  159. }];
  160. [alertControl addAction:actionOne];
  161. [alertControl addAction:alertthree];
  162. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  163. [self presentViewController:alertControl animated:YES completion:nil];
  164. }
  165. - (void)onDone:(UIButton *)sender {
  166. NSLog(@"onDone");
  167. UIImage* img = [self.signatureView signatureImage];
  168. // return ;
  169. // CGSize imgsize = img.size;
  170. NSData* data = [self.signatureView signatureData];
  171. if (self.delegate && [self.delegate respondsToSelector:@selector(SignatureVCReturnImage:indexPath:)]) {
  172. [self.delegate SignatureVCReturnImage:img indexPath:self.indexPath];
  173. }
  174. if (self.delegate && [self.delegate respondsToSelector:@selector(SignatureVCReturnData:indexPath:)]) {
  175. [self.delegate SignatureVCReturnData:data indexPath:self.indexPath];
  176. }
  177. if(self.onReturnImg)
  178. self.onReturnImg(img);
  179. [self.navigationController popViewControllerAnimated:true];
  180. // [self.aBNewPersonNav dismissViewControllerAnimated:true completion:^{
  181. // ;
  182. // }];
  183. }
  184. - (void)onClear:(UIButton *)sender {
  185. [self.signatureView clear];
  186. }
  187. - (void)didReceiveMemoryWarning
  188. {
  189. [super didReceiveMemoryWarning];
  190. // Dispose of any resources that can be recreated.
  191. }
  192. #pragma mark textField delegate
  193. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  194. NSCharacterSet *cs;
  195. cs = [[NSCharacterSet characterSetWithCharactersInString:INTNUMBERS]invertedSet];
  196. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];
  197. BOOL canChange = [string isEqualToString:filtered];
  198. return canChange;
  199. }
  200. - (void)textFieldDidEndEditing:(UITextField *)textField
  201. {
  202. self.linewidth = [textField.text intValue];
  203. }
  204. //- (void)textFieldDidBeginEditing:(UITextField *)textField
  205. //{
  206. // DebugLog(@"textField shouldChangeCharactersInRange");
  207. //
  208. // self.lastedit = textField;
  209. // UITableViewCell *cell = (UITableViewCell *) textField.superview.superview;
  210. // self.lastedit_from = [self.editorTable indexPathForCell:cell];
  211. //}
  212. /*
  213. #pragma mark - Navigation
  214. // In a storyboard-based application, you will often want to do a little preparation before navigation
  215. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  216. {
  217. // Get the new view controller using [segue destinationViewController].
  218. // Pass the selected object to the new view controller.
  219. }
  220. */
  221. - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  222. self.signatureView.frame = self.view.bounds;
  223. }
  224. @end