PDFSignatureViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // PDFSignatureViewController.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFSignatureViewController.h"
  11. #import "PDFSignatureManager.h"
  12. #import "PDFSignatureEditViewController.h"
  13. @interface PDFSignatureViewCell : UITableViewCell
  14. @property (nonatomic,retain) UIImageView *imgView;
  15. @end
  16. @implementation PDFSignatureViewCell
  17. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  18. if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
  19. _imgView = [[UIImageView alloc] init];
  20. [self.contentView addSubview:_imgView];
  21. }
  22. return self;
  23. }
  24. - (void)layoutSubviews {
  25. [super layoutSubviews];
  26. CGFloat height = self.contentView.bounds.size.height;
  27. CGFloat width = height * self.imgView.image.size.width / self.imgView.image.size.height;
  28. width = MIN(width, self.contentView.bounds.size.width - 80.0);
  29. [self.imgView setFrame:CGRectMake((self.bounds.size.width - width)/2.0, 0.0, width, height)];
  30. self.imgView.center = self.contentView.center;
  31. }
  32. @end
  33. @interface PDFSignatureViewController () <UITableViewDelegate,UITableViewDataSource>
  34. @property (nonatomic,assign) IBOutlet UITableView *tableView;
  35. @property (nonatomic,assign) IBOutlet UIView *emptyView;
  36. @property (nonatomic,assign) IBOutlet UIButton *createButton;
  37. @end
  38. @implementation PDFSignatureViewController
  39. #pragma mark - Init Methods
  40. - (void)dealloc {
  41. Block_release(_callback);
  42. [super dealloc];
  43. }
  44. #pragma mark - UIViewController Methods
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. // Do any additional setup after loading the view from its nib.
  48. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  49. self.editing = NO;
  50. self.createButton.layer.borderColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:255.0/255.0 alpha:1.0].CGColor;
  51. self.createButton.layer.borderWidth = 1.0;
  52. self.createButton.layer.cornerRadius = 5.0;
  53. [self.tableView registerClass:[PDFSignatureViewCell class] forCellReuseIdentifier:@"cell"];
  54. }
  55. - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
  56. [super setEditing:editing animated:animated];
  57. [self.tableView setEditing:editing animated:animated];
  58. if (editing) {
  59. UIBarButtonItem *selectAllItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Select All", nil)
  60. style:UIBarButtonItemStylePlain
  61. target:self
  62. action:@selector(buttonItemClicked_SelectAll:)];
  63. UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
  64. target:self
  65. action:@selector(buttonItemClicked_Delete:)];
  66. [self.navigationItem setLeftBarButtonItems:@[selectAllItem, deleteItem] animated:animated];
  67. [selectAllItem release];
  68. [deleteItem release];
  69. } else {
  70. UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
  71. target:self
  72. action:@selector(buttonItemClicked_Cancel:)];
  73. [self.navigationItem setLeftBarButtonItems:@[cancelItem] animated:animated];
  74. [cancelItem release];
  75. }
  76. }
  77. #pragma mark - Button Actions
  78. - (IBAction)buttonItemClicked_Create:(id)sender {
  79. PDFSignatureEditViewController *vc = [[PDFSignatureEditViewController alloc] init];
  80. vc.callback = ^(UIImage *image) {
  81. if (image) {
  82. [[PDFSignatureManager sharedManager] addSignature:image];
  83. [self.tableView reloadData];
  84. }
  85. };
  86. if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
  87. UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
  88. nav.modalPresentationStyle = UIModalPresentationFormSheet;
  89. [self presentViewController:nav animated:YES completion:nil];
  90. [nav release];
  91. [vc release];
  92. } else {
  93. PDFSignatureNavigationController *nav = [[PDFSignatureNavigationController alloc] initWithRootViewController:vc];
  94. [self presentViewController:nav animated:YES completion:nil];
  95. [nav release];
  96. [vc release];
  97. }
  98. }
  99. - (void)buttonItemClicked_Cancel:(id)sender {
  100. if (self.tableView.editing) {
  101. self.tableView.editing = NO;
  102. } else {
  103. [self dismissViewControllerAnimated:YES completion:^{
  104. if (self.callback) {
  105. self.callback(nil);
  106. self.callback = nil;
  107. }
  108. }];
  109. }
  110. }
  111. - (void)buttonItemClicked_SelectAll:(id)sender {
  112. NSInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:0];
  113. if (self.tableView.indexPathsForSelectedRows.count != numberOfRowsInSection) {
  114. for (NSInteger i=0; i<numberOfRowsInSection; i++) {
  115. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  116. [self.tableView selectRowAtIndexPath:indexPath
  117. animated:YES
  118. scrollPosition:UITableViewScrollPositionNone];
  119. }
  120. } else {
  121. for (NSInteger i=0; i<numberOfRowsInSection; i++) {
  122. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
  123. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  124. }
  125. }
  126. }
  127. - (void)buttonItemClicked_Delete:(id)sender {
  128. NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
  129. for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
  130. [indexSet addIndex:indexPath.row];
  131. }
  132. [[PDFSignatureManager sharedManager] removeSignaturesAtIndexes:indexSet];
  133. if ([PDFSignatureManager sharedManager].signatures.count < 1) {
  134. [self setEditing:NO animated:YES];
  135. }
  136. [self.tableView reloadData];
  137. }
  138. #pragma mark - UITableViewDataSource
  139. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  140. if ([PDFSignatureManager sharedManager].signatures.count <= 0) {
  141. self.emptyView.hidden = NO;
  142. self.tableView.hidden = YES;
  143. self.navigationItem.rightBarButtonItem = nil;
  144. } else {
  145. self.emptyView.hidden = YES;
  146. self.tableView.hidden = NO;
  147. self.navigationItem.rightBarButtonItem = self.editButtonItem;
  148. }
  149. return [PDFSignatureManager sharedManager].signatures.count;
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  152. PDFSignatureViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
  153. cell.imgView.image = [UIImage imageWithContentsOfFile:[PDFSignatureManager sharedManager].signatures[indexPath.row]];
  154. return cell;
  155. }
  156. #pragma mark - UITableViewDelegate
  157. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  158. if (!tableView.isEditing) {
  159. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  160. [self dismissViewControllerAnimated:YES completion:^{
  161. UIImage *image = [UIImage imageWithContentsOfFile:[PDFSignatureManager sharedManager].signatures[indexPath.row]];
  162. if (self.callback) {
  163. self.callback(image);
  164. self.callback = nil;
  165. }
  166. }];
  167. }
  168. }
  169. @end