123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- //
- // PDFSignatureViewController.m
- // PDFReader
- //
- // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
- //
- // The PDF Reader Sample applications are licensed with a modified BSD license.
- // Please see License for details. This notice may not be removed from this file.
- //
- #import "PDFSignatureViewController.h"
- #import "PDFSignatureManager.h"
- #import "PDFSignatureEditViewController.h"
- @interface PDFSignatureViewCell : UITableViewCell
- @property (nonatomic,retain) UIImageView *imgView;
- @end
- @implementation PDFSignatureViewCell
- - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
- _imgView = [[UIImageView alloc] init];
- [self.contentView addSubview:_imgView];
- }
- return self;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- CGFloat height = self.contentView.bounds.size.height;
- CGFloat width = height * self.imgView.image.size.width / self.imgView.image.size.height;
- width = MIN(width, self.contentView.bounds.size.width - 80.0);
- [self.imgView setFrame:CGRectMake((self.bounds.size.width - width)/2.0, 0.0, width, height)];
- self.imgView.center = self.contentView.center;
- }
- @end
- @interface PDFSignatureViewController () <UITableViewDelegate,UITableViewDataSource>
- @property (nonatomic,assign) IBOutlet UITableView *tableView;
- @property (nonatomic,assign) IBOutlet UIView *emptyView;
- @property (nonatomic,assign) IBOutlet UIButton *createButton;
- @end
- @implementation PDFSignatureViewController
- #pragma mark - Init Methods
- - (void)dealloc {
- Block_release(_callback);
- [super dealloc];
- }
- #pragma mark - UIViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- self.navigationItem.rightBarButtonItem = self.editButtonItem;
- self.editing = NO;
-
- self.createButton.layer.borderColor = [UIColor colorWithRed:0.0 green:122.0/255.0 blue:255.0/255.0 alpha:1.0].CGColor;
- self.createButton.layer.borderWidth = 1.0;
- self.createButton.layer.cornerRadius = 5.0;
-
- [self.tableView registerClass:[PDFSignatureViewCell class] forCellReuseIdentifier:@"cell"];
- }
- - (void)setEditing:(BOOL)editing animated:(BOOL)animated {
- [super setEditing:editing animated:animated];
-
- [self.tableView setEditing:editing animated:animated];
- if (editing) {
- UIBarButtonItem *selectAllItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Select All", nil)
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_SelectAll:)];
-
- UIBarButtonItem *deleteItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
- target:self
- action:@selector(buttonItemClicked_Delete:)];
-
- [self.navigationItem setLeftBarButtonItems:@[selectAllItem, deleteItem] animated:animated];
- [selectAllItem release];
- [deleteItem release];
- } else {
- UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
- target:self
- action:@selector(buttonItemClicked_Cancel:)];
- [self.navigationItem setLeftBarButtonItems:@[cancelItem] animated:animated];
- [cancelItem release];
- }
- }
- #pragma mark - Button Actions
- - (IBAction)buttonItemClicked_Create:(id)sender {
- PDFSignatureEditViewController *vc = [[PDFSignatureEditViewController alloc] init];
- vc.callback = ^(UIImage *image) {
- if (image) {
- [[PDFSignatureManager sharedManager] addSignature:image];
- [self.tableView reloadData];
- }
- };
- if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- nav.modalPresentationStyle = UIModalPresentationFormSheet;
- [self presentViewController:nav animated:YES completion:nil];
- [nav release];
- [vc release];
- } else {
- PDFSignatureNavigationController *nav = [[PDFSignatureNavigationController alloc] initWithRootViewController:vc];
- [self presentViewController:nav animated:YES completion:nil];
- [nav release];
- [vc release];
- }
- }
- - (void)buttonItemClicked_Cancel:(id)sender {
- if (self.tableView.editing) {
- self.tableView.editing = NO;
- } else {
- [self dismissViewControllerAnimated:YES completion:^{
- if (self.callback) {
- self.callback(nil);
- self.callback = nil;
- }
- }];
- }
- }
- - (void)buttonItemClicked_SelectAll:(id)sender {
- NSInteger numberOfRowsInSection = [self.tableView numberOfRowsInSection:0];
- if (self.tableView.indexPathsForSelectedRows.count != numberOfRowsInSection) {
- for (NSInteger i=0; i<numberOfRowsInSection; i++) {
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
- [self.tableView selectRowAtIndexPath:indexPath
- animated:YES
- scrollPosition:UITableViewScrollPositionNone];
- }
- } else {
- for (NSInteger i=0; i<numberOfRowsInSection; i++) {
- NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
- [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- }
- }
- }
- - (void)buttonItemClicked_Delete:(id)sender {
- NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet];
- for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
- [indexSet addIndex:indexPath.row];
- }
- [[PDFSignatureManager sharedManager] removeSignaturesAtIndexes:indexSet];
- if ([PDFSignatureManager sharedManager].signatures.count < 1) {
- [self setEditing:NO animated:YES];
- }
- [self.tableView reloadData];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- if ([PDFSignatureManager sharedManager].signatures.count <= 0) {
- self.emptyView.hidden = NO;
- self.tableView.hidden = YES;
- self.navigationItem.rightBarButtonItem = nil;
- } else {
- self.emptyView.hidden = YES;
- self.tableView.hidden = NO;
- self.navigationItem.rightBarButtonItem = self.editButtonItem;
- }
- return [PDFSignatureManager sharedManager].signatures.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- PDFSignatureViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
- cell.imgView.image = [UIImage imageWithContentsOfFile:[PDFSignatureManager sharedManager].signatures[indexPath.row]];
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- if (!tableView.isEditing) {
- [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
- [self dismissViewControllerAnimated:YES completion:^{
- UIImage *image = [UIImage imageWithContentsOfFile:[PDFSignatureManager sharedManager].signatures[indexPath.row]];
- if (self.callback) {
- self.callback(image);
- self.callback = nil;
- }
- }];
- }
- }
- @end
|