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