SignatureViewController.m 11 KB

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