CPDFDrawPencilKitFuncView.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // CPDFDrawPencilKitFuncView.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. public enum CPDFDrawPencilKitFuncType: Int {
  14. case eraser
  15. case cancel
  16. case done
  17. }
  18. public protocol CPDFDrawPencilViewDelegate: AnyObject {
  19. func drawPencilFuncView(_ view: CPDFDrawPencilKitFuncView, eraserBtn btn: UIButton)
  20. func drawPencilFuncView(_ view: CPDFDrawPencilKitFuncView, saveBtn btn: UIButton)
  21. func drawPencilFuncView(_ view: CPDFDrawPencilKitFuncView, cancelBtn btn: UIButton)
  22. }
  23. public class CPDFDrawPencilKitFuncView: UIView {
  24. public weak var delegate: CPDFDrawPencilViewDelegate?
  25. private var eraseButton: UIButton?
  26. // MARK: - Initializers
  27. override init(frame: CGRect) {
  28. super.init(frame: frame)
  29. // Initialization code
  30. self.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  31. self.layer.borderColor = UIColor.lightGray.cgColor
  32. self.layer.borderWidth = 1.0
  33. self.layer.cornerRadius = 5.0
  34. initSubViews()
  35. }
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. public override func layoutSubviews() {
  40. super.layoutSubviews()
  41. let tWidth: CGFloat = 182.0
  42. let tHeight: CGFloat = 54.0
  43. self.transform = CGAffineTransform.identity
  44. self.frame = CGRect(x: 0, y: 0, width: tWidth, height: tHeight)
  45. let width = (self.superview?.bounds.size.width ?? 0) - 30
  46. let scale = width / self.bounds.size.width
  47. if self.frame.size.width > width {
  48. self.transform = CGAffineTransform(scaleX: scale, y: scale)
  49. self.center = CGPoint(x: (self.superview?.frame.size.width ?? 0) / 2.0,
  50. y: self.frame.size.height / 2.0 + 22)
  51. } else {
  52. if #available(iOS 11.0, *) {
  53. if self.window?.safeAreaInsets.bottom ?? 0 > 0 {
  54. self.center = CGPoint(x: (self.superview?.frame.size.width ?? 0) - self.frame.size.width / 2.0 - 40,
  55. y: self.frame.size.height / 2.0 + 42)
  56. } else {
  57. self.center = CGPoint(x: (self.superview?.frame.size.width ?? 0) - self.frame.size.width / 2.0 - 30,
  58. y: self.frame.size.height / 2.0 + 22)
  59. }
  60. }
  61. }
  62. }
  63. func initSubViews() {
  64. let tSpace: CGFloat = 10.0
  65. var tOffsetX: CGFloat = tSpace
  66. let tOffsetY: CGFloat = 5.0
  67. let tWidth: CGFloat = 44.0
  68. let eraseBtn = UIButton(type: .custom)
  69. eraseBtn.tag = CPDFDrawPencilKitFuncType.eraser.rawValue
  70. eraseBtn.layer.cornerRadius = 2.0
  71. eraseBtn.frame = CGRect(x: tOffsetX, y: tOffsetY, width: tWidth, height: self.m_height - tOffsetY * 2.0)
  72. eraseBtn.setImage(UIImage(named: "CImageNamePencilEraserOff", in: Bundle(for: Self.self), compatibleWith: nil), for: .normal)
  73. eraseBtn.setImage(UIImage(named: "CImageNamePencilEraserOn", in: Bundle(for: Self.self), compatibleWith: nil), for: .selected)
  74. eraseBtn.addTarget(self, action: #selector(eraserBtnClicked(_:)), for: .touchUpInside)
  75. self.addSubview(eraseBtn)
  76. self.eraseButton = eraseBtn
  77. tOffsetX = tOffsetX + eraseBtn.frame.size.width + tSpace
  78. let clearBtn = UIButton(type: .custom)
  79. clearBtn.tag = CPDFDrawPencilKitFuncType.cancel.rawValue
  80. clearBtn.layer.cornerRadius = 2.0
  81. clearBtn.frame = CGRect(x: tOffsetX, y: tOffsetY, width: tWidth + 10, height: self.m_height - tOffsetY * 2.0)
  82. if #available(iOS 13.0, *) {
  83. clearBtn.setTitleColor(.label, for: .normal)
  84. } else {
  85. clearBtn.setTitleColor(.black, for: .normal)
  86. }
  87. clearBtn.setTitle(NSLocalizedString("Discard", comment: ""), for: .normal)
  88. clearBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13.0)
  89. clearBtn.titleLabel?.adjustsFontSizeToFitWidth = true
  90. clearBtn.addTarget(self, action: #selector(clearBtnClicked(_:)), for: .touchUpInside)
  91. self.addSubview(clearBtn)
  92. tOffsetX = tOffsetX + clearBtn.frame.size.width + tSpace
  93. let saveBtn = UIButton(type: .custom)
  94. saveBtn.tag = CPDFDrawPencilKitFuncType.done.rawValue
  95. saveBtn.layer.cornerRadius = 2.0
  96. saveBtn.frame = CGRect(x: tOffsetX, y: tOffsetY, width: tWidth, height: self.frame.size.height - tOffsetY * 2.0)
  97. if #available(iOS 13.0, *) {
  98. saveBtn.setTitleColor(.label, for: .normal)
  99. } else {
  100. saveBtn.setTitleColor(.black, for: .normal)
  101. }
  102. saveBtn.setTitle(NSLocalizedString("Save", comment: ""), for: .normal)
  103. saveBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13.0)
  104. saveBtn.addTarget(self, action: #selector(saveBtnClicked(_:)), for: .touchUpInside)
  105. self.addSubview(saveBtn)
  106. }
  107. func resetAllSubviews() {
  108. // Reset all subviews
  109. for tSubview in subviews {
  110. if tSubview is UIButton {
  111. var tBtn = tSubview as? UIButton
  112. tBtn?.isSelected = false
  113. tBtn?.backgroundColor = UIColor.clear
  114. }
  115. }
  116. }
  117. // MARK: - Action
  118. @objc func clearBtnClicked(_ sender: UIButton) {
  119. resetAllSubviews()
  120. DispatchQueue.main.async {
  121. self.delegate?.drawPencilFuncView(self, cancelBtn: sender)
  122. }
  123. }
  124. @objc func saveBtnClicked(_ sender: UIButton) {
  125. resetAllSubviews()
  126. DispatchQueue.main.async {
  127. self.delegate?.drawPencilFuncView(self, saveBtn: sender)
  128. }
  129. }
  130. @objc func eraserBtnClicked(_ sender: UIButton) {
  131. let isSelected = !sender.isSelected
  132. resetAllSubviews()
  133. sender.isSelected = isSelected
  134. DispatchQueue.main.async {
  135. self.delegate?.drawPencilFuncView(self, eraserBtn: sender)
  136. }
  137. }
  138. var m_width: CGFloat {
  139. get {
  140. return frame.width
  141. }
  142. set {
  143. var frame = self.frame
  144. frame.size.width = newValue
  145. self.frame = frame
  146. }
  147. }
  148. var m_height: CGFloat {
  149. get {
  150. return frame.height
  151. }
  152. set {
  153. var frame = self.frame
  154. frame.size.height = newValue
  155. self.frame = frame
  156. }
  157. }
  158. }