CPDFSignatureViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // CPDFSignatureViewController.swift
  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 UIKit
  13. import ComPDFKit
  14. @objc public protocol CPDFSignatureViewControllerDelegate: AnyObject {
  15. @objc optional func signatureViewControllerDismiss(_ signatureViewController: CPDFSignatureViewController)
  16. @objc optional func signatureViewController(_ signatureViewController: CPDFSignatureViewController, image: UIImage)
  17. }
  18. public class CPDFSignatureViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CPDFSignatureViewCellDelegate,CPDFSignatureEditViewControllerDelegate {
  19. var annotStyle: CAnnotStyle?
  20. public weak var delegate: CPDFSignatureViewControllerDelegate?
  21. var tableView: UITableView?
  22. private var backBtn: UIButton?
  23. private var titleLabel: UILabel?
  24. private var emptyLabel: UILabel?
  25. private var headerView: UIView?
  26. private var createButton: UIButton?
  27. // MARK: - Initializers
  28. public init(style annotStyle: CAnnotStyle?) {
  29. self.annotStyle = annotStyle
  30. self.tableView = UITableView()
  31. super.init(nibName: nil, bundle: nil)
  32. }
  33. required init?(coder aDecoder: NSCoder) {
  34. fatalError("init(coder:) has not been implemented")
  35. }
  36. // MARK: - ViewController Methods
  37. public override func viewDidLoad() {
  38. super.viewDidLoad()
  39. self.headerView = UIView()
  40. self.headerView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  41. self.headerView?.layer.borderWidth = 1.0
  42. self.headerView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  43. if(self.headerView != nil) {
  44. view.addSubview(self.headerView!)
  45. }
  46. self.titleLabel = UILabel()
  47. self.titleLabel?.autoresizingMask = .flexibleRightMargin
  48. self.titleLabel?.font = UIFont.systemFont(ofSize: 20)
  49. self.titleLabel?.text = NSLocalizedString("Signatures", comment: "")
  50. self.titleLabel?.textAlignment = .center
  51. self.titleLabel?.adjustsFontSizeToFitWidth = true
  52. if(self.titleLabel != nil) {
  53. self.headerView?.addSubview(self.titleLabel!)
  54. }
  55. self.backBtn = UIButton()
  56. self.backBtn?.autoresizingMask = .flexibleLeftMargin
  57. self.backBtn?.setImage(UIImage(named: "CPDFAnnotationBaseImageBack", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  58. self.backBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  59. if(self.backBtn != nil) {
  60. self.headerView?.addSubview(self.backBtn!)
  61. }
  62. self.tableView = UITableView(frame: CGRect(x: 0, y: 50, width: view.frame.size.width, height: view.frame.size.height - 70), style: .plain)
  63. self.tableView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  64. self.tableView?.delegate = self
  65. self.tableView?.dataSource = self
  66. self.tableView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  67. self.tableView?.rowHeight = 120
  68. if(self.tableView != nil) {
  69. view.addSubview(self.tableView!)
  70. }
  71. self.emptyLabel = UILabel()
  72. self.emptyLabel?.text = NSLocalizedString("NO Signature", comment: "")
  73. self.emptyLabel?.textAlignment = .center
  74. if(self.emptyLabel != nil) {
  75. view.addSubview(self.emptyLabel!)
  76. }
  77. self.createButton = UIButton()
  78. self.createButton?.layer.cornerRadius = 25.0
  79. self.createButton?.clipsToBounds = true
  80. self.createButton?.setImage(UIImage(named: "CPDFSignatureImageAdd", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  81. self.createButton?.addTarget(self, action: #selector(buttonItemClicked_create(_:)), for: .touchUpInside)
  82. if(self.createButton != nil) {
  83. view.addSubview(self.createButton!)
  84. }
  85. self.view.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  86. updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  87. createGestureRecognizer()
  88. }
  89. public override func viewWillLayoutSubviews() {
  90. super.viewWillLayoutSubviews()
  91. titleLabel?.frame = CGRect(x: (view.frame.size.width - 120)/2, y: 5, width: 120, height: 50)
  92. headerView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 50)
  93. emptyLabel?.frame = CGRect(x: (view.frame.size.width - 120)/2, y: (view.frame.size.height - 50)/2, width: 120, height: 50)
  94. if #available(iOS 11.0, *) {
  95. backBtn?.frame = CGRect(x: view.frame.size.width - 60 - view.safeAreaInsets.right, y: 5, width: 50, height: 50)
  96. createButton?.frame = CGRect(x: view.frame.size.width - 70 - view.safeAreaInsets.right, y: view.frame.size.height - 100 - view.safeAreaInsets.bottom, width: 50, height: 50)
  97. } else {
  98. backBtn?.frame = CGRect(x: view.frame.size.width - 60, y: 5, width: 50, height: 50)
  99. createButton?.frame = CGRect(x: view.frame.size.width - 70, y: view.frame.size.height - 100, width: 50, height: 50)
  100. }
  101. }
  102. public override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  103. super.willTransition(to: newCollection, with: coordinator)
  104. updatePreferredContentSizeWithTraitCollection(traitCollection: newCollection)
  105. }
  106. // MARK: - Protect Methods
  107. func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  108. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 660)
  109. }
  110. // MARK: - Private Methods
  111. func createGestureRecognizer() {
  112. createButton?.isUserInteractionEnabled = true
  113. let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panaddBookmarkBtn(_:)))
  114. createButton?.addGestureRecognizer(panRecognizer)
  115. }
  116. @objc func panaddBookmarkBtn(_ gestureRecognizer: UIPanGestureRecognizer) {
  117. let point = gestureRecognizer.translation(in: view)
  118. let newX = (createButton?.center.x ?? 0) + point.x
  119. let newY = (createButton?.center.y ?? 0) + point.y
  120. if view.frame.contains(CGPoint(x: newX, y: newY)) {
  121. createButton?.center = CGPoint(x: newX, y: newY)
  122. }
  123. gestureRecognizer.setTranslation(.zero, in: view)
  124. }
  125. // MARK: - Action
  126. @objc func buttonItemClicked_back(_ sender: Any) {
  127. self.dismiss(animated: true, completion: nil)
  128. delegate?.signatureViewControllerDismiss?(self)
  129. }
  130. @objc func buttonItemClicked_create(_ sender: Any) {
  131. let editVC = CPDFSignatureEditViewController.init(nibName: nil, bundle: nil)
  132. editVC.delegate = self
  133. let presentationController = SignatureCustomPresentationController(presentedViewController: editVC, presenting: self)
  134. editVC.transitioningDelegate = presentationController
  135. self.present(editVC, animated: true, completion: nil)
  136. }
  137. // MARK: - UITableViewDataSource
  138. public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  139. if CSignatureManager.sharedManager.signatures.count <= 0 {
  140. emptyLabel?.isHidden = false
  141. tableView.isHidden = true
  142. } else {
  143. emptyLabel?.isHidden = true
  144. tableView.isHidden = false
  145. }
  146. return CSignatureManager.sharedManager.signatures.count
  147. }
  148. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  149. var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CPDFSignatureViewCell
  150. if cell == nil {
  151. cell = CPDFSignatureViewCell(style: .subtitle, reuseIdentifier: "cell")
  152. }
  153. cell?.signatureImageView?.image = UIImage.init(contentsOfFile: CSignatureManager.sharedManager.signatures[indexPath.row])
  154. cell?.deleteDelegate = self
  155. return cell!
  156. }
  157. // MARK: - UITableViewDelegate
  158. public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  159. if !tableView.isEditing {
  160. tableView.deselectRow(at: indexPath, animated: true)
  161. dismiss(animated: true)
  162. let image = UIImage.init(contentsOfFile: CSignatureManager.sharedManager.signatures[indexPath.row])
  163. if(image != nil) {
  164. delegate?.signatureViewController?(self, image: image!)
  165. }
  166. }
  167. }
  168. // MARK: - CPDFSignatureViewCellDelegate
  169. func signatureViewCell(_ signatureViewCell: CPDFSignatureViewCell) {
  170. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
  171. let OKAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { (action) in
  172. if let indexSet = self.tableView?.indexPath(for: signatureViewCell) {
  173. CSignatureManager.sharedManager.removeSignatures(at: IndexSet(integer: indexSet.row))
  174. if CSignatureManager.sharedManager.signatures.count < 1 {
  175. self.setEditing(false, animated: true)
  176. }
  177. self.tableView?.reloadData()
  178. }
  179. }
  180. let alert = UIAlertController(title: NSLocalizedString("Warning", comment: ""), message: NSLocalizedString("Are you sure to delete?", comment: ""), preferredStyle: .alert)
  181. alert.addAction(cancelAction)
  182. alert.addAction(OKAction)
  183. self.present(alert, animated: true, completion: nil)
  184. }
  185. // MARK: - CPDFSignatureEditViewControllerDelegate
  186. func signatureEditViewController(_ signatureEditViewController: CPDFSignatureEditViewController, image: UIImage) {
  187. signatureEditViewController.dismiss(animated: true)
  188. CSignatureManager.sharedManager.addImageSignature(image)
  189. tableView?.reloadData()
  190. }
  191. }