CPDFInkViewController.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // CPDFInkViewController.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. @objc protocol CPDFInkViewControllerDelegate: AnyObject {
  14. @objc optional func inkViewController(_ inkViewController: CPDFInkViewController, annotStyle: CAnnotStyle)
  15. @objc optional func inkViewControllerDimiss(_ inkViewController: CPDFInkViewController)
  16. }
  17. class CPDFInkViewController: CPDFAnnotationBaseViewController, CPDFThicknessSliderViewDelegate {
  18. weak var delegate: CPDFInkViewControllerDelegate?
  19. private var thicknessView: CPDFThicknessSliderView?
  20. // MARK: - ViewController Methods
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. thicknessView = CPDFThicknessSliderView(frame: CGRect.zero)
  24. thicknessView?.delegate = self
  25. thicknessView?.thicknessSlider?.value = 4.0
  26. thicknessView?.autoresizingMask = .flexibleWidth
  27. if(thicknessView != nil) {
  28. scrcollView?.addSubview(thicknessView!)
  29. }
  30. }
  31. override func viewWillLayoutSubviews() {
  32. super.viewWillLayoutSubviews()
  33. scrcollView?.frame = CGRect(x: 0, y: 170, width: self.view.frame.size.width, height: 310)
  34. scrcollView?.contentSize = CGSize(width: self.view.frame.size.width, height: 400)
  35. if #available(iOS 11.0, *) {
  36. self.thicknessView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 180, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  37. } else {
  38. self.colorView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 90)
  39. self.thicknessView?.frame = CGRect(x: 0, y: 180, width: self.view.frame.size.width, height: 90)
  40. }
  41. }
  42. override func viewDidAppear(_ animated: Bool) {
  43. delegate?.inkViewControllerDimiss?(self)
  44. }
  45. // MARK: - Private Methods
  46. override func commomInitTitle() {
  47. self.titleLabel?.text = NSLocalizedString("Ink", comment: "")
  48. self.thicknessView?.titleLabel?.text = NSLocalizedString("Line Width", comment: "")
  49. sampleView?.selecIndex = .freehand
  50. self.sampleView?.thickness = 4.0
  51. self.colorView?.selectedColor = self.annotStyle?.color
  52. }
  53. override func commomInitFromAnnotStyle() {
  54. self.sampleView?.color = self.annotStyle?.color ?? UIColor.clear
  55. self.sampleView?.opcity = self.annotStyle?.opacity ?? 0
  56. self.sampleView?.thickness = self.annotStyle?.lineWidth ?? 0
  57. self.opacitySliderView?.opacitySlider?.value = Float(self.annotStyle?.opacity ?? 0)
  58. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0)/1)*100))%"
  59. self.thicknessView?.thicknessSlider?.value = Float(self.annotStyle?.lineWidth ?? 0 )
  60. self.thicknessView?.startLabel?.text = "\(Int((self.thicknessView?.thicknessSlider?.value ?? 0))) pt"
  61. self.sampleView?.setNeedsDisplay()
  62. }
  63. override func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  64. if self.colorPicker?.superview != nil {
  65. let currentDevice = UIDevice.current
  66. if currentDevice.userInterfaceIdiom == .pad {
  67. // This is an iPad
  68. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 520)
  69. } else {
  70. // This is an iPhone or iPod touch
  71. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 320)
  72. }
  73. } else {
  74. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 520)
  75. }
  76. }
  77. // MARK: - Action
  78. @objc override func buttonItemClicked_back(_ button: UIButton) {
  79. dismiss(animated: true)
  80. delegate?.inkViewControllerDimiss?(self)
  81. }
  82. // MARK: - CPDFThicknessSliderViewDelegate
  83. func thicknessSliderView(_ opacitySliderView: CPDFThicknessSliderView, thickness: CGFloat) {
  84. sampleView?.thickness = thickness
  85. annotStyle?.setLineWidth(thickness)
  86. if(annotStyle != nil) {
  87. delegate?.inkViewController?(self, annotStyle: annotStyle!)
  88. }
  89. sampleView?.setNeedsDisplay()
  90. }
  91. // MARK: - CPDFColorSelectViewDelegate
  92. override func selectColorView(_ select: CPDFColorSelectView, color: UIColor) {
  93. sampleView?.color = color
  94. annotStyle?.setColor(color)
  95. sampleView?.setNeedsDisplay()
  96. if(annotStyle != nil) {
  97. delegate?.inkViewController?(self, annotStyle: annotStyle!)
  98. }
  99. }
  100. // MARK: - CPDFColorPickerViewDelegate
  101. override func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
  102. sampleView?.color = color
  103. annotStyle?.setColor(color)
  104. if(annotStyle != nil) {
  105. delegate?.inkViewController?(self, annotStyle: annotStyle!)
  106. }
  107. var red: CGFloat = 0
  108. var green: CGFloat = 0
  109. var blue: CGFloat = 0
  110. var alpha: CGFloat = 0
  111. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  112. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  113. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  114. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  115. }
  116. // MARK: - CPDFOpacitySliderViewDelegate
  117. override func opacitySliderView(_ opacitySliderView: CPDFOpacitySliderView, opacity: CGFloat) {
  118. sampleView?.opcity = opacity
  119. annotStyle?.setOpacity(opacity)
  120. if(annotStyle != nil) {
  121. delegate?.inkViewController?(self, annotStyle: annotStyle!)
  122. }
  123. sampleView?.setNeedsDisplay()
  124. }
  125. // MARK: - UIColorPickerViewControllerDelegate
  126. @available(iOS 14.0, *)
  127. func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  128. sampleView?.color = viewController.selectedColor
  129. annotStyle?.setColor(viewController.selectedColor)
  130. if(annotStyle != nil) {
  131. delegate?.inkViewController?(self, annotStyle: annotStyle!)
  132. }
  133. var red: CGFloat = 0
  134. var green: CGFloat = 0
  135. var blue: CGFloat = 0
  136. var alpha: CGFloat = 0
  137. viewController.selectedColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  138. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  139. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  140. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  141. }
  142. }