SignatureViewController.m 11 KB

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