CPDFSignatureViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // CPDFSignatureViewController.m
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. #import "CPDFSignatureViewController.h"
  13. #import "CAnnotStyle.h"
  14. #import "CPDFSignatureViewCell.h"
  15. #import "CSignatureManager.h"
  16. #import "CPDFSignatureEditViewController.h"
  17. #import "SignatureCustomPresentationController.h"
  18. #if __has_include(<ComPDFKit_Tools/ComPDFKit_Tools.h>)
  19. #import <ComPDFKit_Tools/ComPDFKit_Tools.h>
  20. #else
  21. #import "ComPDFKit_Tools.h"
  22. #endif
  23. @interface CPDFSignatureViewController () <UITableViewDelegate, UITableViewDataSource, CPDFSignatureViewCellDelegate,CPDFSignatureEditViewControllerDelegate>
  24. @property (nonatomic, strong) UIButton *backBtn;
  25. @property (nonatomic, strong) UILabel *titleLabel;
  26. @property (nonatomic, strong) CAnnotStyle *annotStyle;
  27. @property (nonatomic, strong) UILabel *emptyLabel;
  28. @property (nonatomic, strong) UIButton *createButton;
  29. @property (nonatomic, strong) UIView *headerView;
  30. @end
  31. @implementation CPDFSignatureViewController
  32. #pragma mark - Initializers
  33. - (instancetype)initWithStyle:(CAnnotStyle *)annotStyle {
  34. if (self = [super init]) {
  35. self.annotStyle = annotStyle;
  36. }
  37. return self;
  38. }
  39. #pragma mark - ViewController Methods
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Do any additional setup after loading the view.
  43. self.headerView = [[UIView alloc] init];
  44. self.headerView.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.1].CGColor;
  45. self.headerView.layer.borderWidth = 1.0;
  46. self.headerView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  47. [self.view addSubview:self.headerView];
  48. self.titleLabel = [[UILabel alloc] init];
  49. self.titleLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
  50. self.titleLabel.font = [UIFont systemFontOfSize:20];
  51. self.titleLabel.text = NSLocalizedString(@"Signatures", nil);
  52. self.titleLabel.textAlignment = NSTextAlignmentCenter;
  53. self.titleLabel.adjustsFontSizeToFitWidth = YES;
  54. [self.headerView addSubview:self.titleLabel];
  55. self.backBtn = [[UIButton alloc] init];
  56. self.backBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  57. [self.backBtn setImage:[UIImage imageNamed:@"CPDFAnnotationBaseImageBack" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  58. [self.backBtn addTarget:self action:@selector(buttonItemClicked_back:) forControlEvents:UIControlEventTouchUpInside];
  59. [self.headerView addSubview:self.backBtn];
  60. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 70) style:UITableViewStylePlain];
  61. self.tableView.backgroundColor = [CPDFColorUtils CAnnotationPropertyViewControllerBackgoundColor];
  62. self.tableView.delegate = self;
  63. self.tableView.dataSource = self;
  64. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  65. self.tableView.rowHeight = 120;
  66. [self.view addSubview:self.tableView];
  67. self.emptyLabel = [[UILabel alloc] init];
  68. self.emptyLabel.text = NSLocalizedString(@"NO Signature", nil);
  69. self.emptyLabel.textAlignment = NSTextAlignmentCenter;
  70. [self.view addSubview:self.emptyLabel];
  71. self.createButton = [[UIButton alloc] init];
  72. self.createButton.layer.cornerRadius = 25.0;
  73. self.createButton.clipsToBounds = YES;
  74. [self.createButton setImage:[UIImage imageNamed:@"CPDFSignatureImageAdd" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
  75. [self.createButton addTarget:self action:@selector(buttonItemClicked_create:) forControlEvents:UIControlEventTouchUpInside];
  76. [self.view addSubview:self.createButton];
  77. self.view.backgroundColor = [CPDFColorUtils CAnnotationSampleBackgoundColor];
  78. [self updatePreferredContentSizeWithTraitCollection:self.traitCollection];
  79. [self createGestureRecognizer];
  80. }
  81. - (void)viewWillLayoutSubviews {
  82. [super viewWillLayoutSubviews];
  83. self.titleLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, 5, 120, 50);
  84. self.headerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 50);
  85. self.emptyLabel.frame = CGRectMake((self.view.frame.size.width - 120)/2, (self.view.frame.size.height - 50)/2, 120, 50);
  86. if (@available(iOS 11.0, *)) {
  87. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, 5, 50, 50);
  88. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70 - self.view.safeAreaInsets.right, self.view.frame.size.height - 100 - self.view.safeAreaInsets.bottom, 50, 50);
  89. } else {
  90. self.backBtn.frame = CGRectMake(self.view.frame.size.width - 60, 5, 50, 50);
  91. self.createButton.frame = CGRectMake(self.view.frame.size.width - 70, self.view.frame.size.height - 100, 50, 50);
  92. }
  93. }
  94. - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
  95. [super willTransitionToTraitCollection:newCollection withTransitionCoordinator:coordinator];
  96. [self updatePreferredContentSizeWithTraitCollection:newCollection];
  97. }
  98. #pragma mark - Private Methods
  99. - (void)createGestureRecognizer {
  100. [self.createButton setUserInteractionEnabled:YES];
  101. UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panaddBookmarkBtn:)];
  102. [self.createButton addGestureRecognizer:panRecognizer];
  103. }
  104. - (void)panaddBookmarkBtn:(UIPanGestureRecognizer *)gestureRecognizer {
  105. CGPoint point = [gestureRecognizer translationInView:self.view];
  106. CGFloat newX = self.createButton.center.x + point.x;
  107. CGFloat newY = self.createButton.center.y + point.y;
  108. if (CGRectContainsPoint(self.view.frame, CGPointMake(newX, newY))) {
  109. self.createButton.center = CGPointMake(newX, newY);
  110. }
  111. [gestureRecognizer setTranslation:CGPointZero inView:self.view];
  112. }
  113. - (void)updatePreferredContentSizeWithTraitCollection:(UITraitCollection *)traitCollection {
  114. self.preferredContentSize = CGSizeMake(self.view.bounds.size.width, traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact ? 350 : 660);
  115. }
  116. #pragma mark - Action
  117. - (void)buttonItemClicked_back:(id)sender {
  118. [self dismissViewControllerAnimated:YES completion:nil];
  119. if(self.delegate && [self.delegate respondsToSelector:@selector(signatureViewControllerDismiss:)]) {
  120. [self.delegate signatureViewControllerDismiss:self];
  121. }
  122. }
  123. - (void)buttonItemClicked_create:(id)sender {
  124. SignatureCustomPresentationController *presentationController NS_VALID_UNTIL_END_OF_SCOPE;
  125. CPDFSignatureEditViewController *editVC = [[CPDFSignatureEditViewController alloc] init];
  126. editVC.delegate = self;
  127. presentationController = [[SignatureCustomPresentationController alloc] initWithPresentedViewController:editVC presentingViewController:self];
  128. editVC.transitioningDelegate = presentationController;
  129. [self presentViewController:editVC animated:YES completion:nil];
  130. }
  131. #pragma mark - UITableViewDataSource
  132. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  133. if ([CSignatureManager sharedManager].signatures.count <= 0) {
  134. self.emptyLabel.hidden = NO;
  135. self.tableView.hidden = YES;
  136. } else {
  137. self.emptyLabel.hidden = YES;
  138. self.tableView.hidden = NO;
  139. }
  140. return [CSignatureManager sharedManager].signatures.count;
  141. }
  142. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  143. CPDFSignatureViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  144. if (cell == nil) {
  145. cell = [[CPDFSignatureViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
  146. }
  147. cell.signatureImageView.image = [UIImage imageWithContentsOfFile:[CSignatureManager sharedManager].signatures[indexPath.row]];
  148. cell.deleteDelegate = self;
  149. return cell;
  150. }
  151. #pragma mark - UITableViewDelegate
  152. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  153. if (!tableView.isEditing) {
  154. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  155. [self dismissViewControllerAnimated:YES completion:^{
  156. if (self.delegate && [self.delegate respondsToSelector:@selector(signatureViewController:image:)]) {
  157. UIImage *image = [UIImage imageWithContentsOfFile:[CSignatureManager sharedManager].signatures[indexPath.row]];
  158. [self.delegate signatureViewController:self image:image];
  159. }
  160. }];
  161. }
  162. }
  163. #pragma mark - CPDFSignatureViewCellDelegate
  164. - (void)signatureViewCell:(CPDFSignatureViewCell *)signatureViewCell {
  165. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil)
  166. style:UIAlertActionStyleCancel
  167. handler:nil];
  168. UIAlertAction *OKAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", nil)
  169. style:UIAlertActionStyleDefault
  170. handler:^(UIAlertAction * _Nonnull action) {
  171. NSIndexPath *indexSet = [self.tableView indexPathForCell:signatureViewCell];
  172. [[CSignatureManager sharedManager] removeSignaturesAtIndexe:indexSet.row];
  173. if ([CSignatureManager sharedManager].signatures.count < 1) {
  174. [self setEditing:NO animated:YES];
  175. }
  176. [self.tableView reloadData];
  177. }];
  178. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil)
  179. message:NSLocalizedString(@"Are you sure to delete?", nil)
  180. preferredStyle:UIAlertControllerStyleAlert];
  181. [alert addAction:cancelAction];
  182. [alert addAction:OKAction];
  183. [self presentViewController:alert animated:YES completion:nil];
  184. }
  185. #pragma mark - CPDFSignatureEditViewControllerDelegate
  186. - (void)signatureEditViewController:(CPDFSignatureEditViewController *)signatureEditViewController image:(UIImage *)image{
  187. [signatureEditViewController dismissViewControllerAnimated:YES completion:nil];
  188. [[CSignatureManager sharedManager] addImageSignature:image];
  189. [self.tableView reloadData];
  190. }
  191. @end