CPDFShapeCircleViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // CPDFShapeCircleViewController.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 Foundation
  14. @objc protocol CPDFShapeCircleViewControllerDelegate: AnyObject {
  15. @objc optional func circleViewController(_ circleViewController: CPDFShapeCircleViewController, annotStyle: CAnnotStyle)
  16. }
  17. class CPDFShapeCircleViewController: CPDFAnnotationBaseViewController, CPDFThicknessSliderViewDelegate {
  18. weak var delegate: CPDFShapeCircleViewControllerDelegate?
  19. var fillColorSelectView: CPDFColorSelectView?
  20. var thicknessView: CPDFThicknessSliderView?
  21. var dottedView: CPDFThicknessSliderView?
  22. var fillColorPicker: CPDFColorPickerView?
  23. var index: Int = 0
  24. var titles = ""
  25. var isFill: Bool = false
  26. private var dashPattern: [NSNumber] = [NSNumber]()
  27. // MARK: - Initializers
  28. override init(annotStyle: CAnnotStyle) {
  29. super.init(annotStyle: annotStyle)
  30. // self.annotStyle = annotStyle
  31. }
  32. required init?(coder: NSCoder) {
  33. fatalError("init(coder:) has not been implemented")
  34. }
  35. // MARK: - ViewController Methods
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. // Do any additional setup after loading the view.
  39. self.thicknessView = CPDFThicknessSliderView(frame: CGRect.zero)
  40. self.thicknessView?.delegate = self
  41. self.thicknessView?.autoresizingMask = [.flexibleWidth]
  42. if thicknessView != nil {
  43. self.scrcollView?.addSubview(self.thicknessView!)
  44. }
  45. self.dottedView = CPDFThicknessSliderView(frame: CGRect.zero)
  46. self.dottedView?.delegate = self
  47. self.dottedView?.thicknessSlider?.minimumValue = 0.0
  48. self.dottedView?.thicknessSlider?.maximumValue = 10.0
  49. self.dottedView?.autoresizingMask = [.flexibleWidth]
  50. if dottedView != nil {
  51. self.scrcollView?.addSubview(self.dottedView!)
  52. }
  53. self.fillColorSelectView = CPDFColorSelectView(frame: CGRect.zero)
  54. self.fillColorSelectView?.delegate = self
  55. self.fillColorSelectView?.autoresizingMask = [.flexibleWidth]
  56. if fillColorSelectView != nil {
  57. self.scrcollView?.addSubview(self.fillColorSelectView!)
  58. }
  59. }
  60. override func viewWillLayoutSubviews() {
  61. super.viewWillLayoutSubviews()
  62. self.scrcollView?.frame = CGRect(x: 0, y: 170, width: self.view.frame.size.width, height: self.view.frame.size.height - 170)
  63. self.scrcollView?.contentSize = CGSize(width: self.view.frame.size.width, height: 550)
  64. if #available(iOS 11.0, *) {
  65. self.colorView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 0, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  66. self.fillColorSelectView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 90, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  67. self.opacitySliderView?.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)
  68. self.thicknessView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 270, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  69. self.dottedView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: 360, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  70. self.backBtn?.frame = CGRect(x: self.view.frame.size.width - 60 - self.view.safeAreaInsets.right, y: 5, width: 50, height: 50)
  71. } else {
  72. self.colorView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 90)
  73. self.opacitySliderView?.frame = CGRect(x: 0, y: 180, width: self.view.frame.size.width, height: 90)
  74. self.fillColorSelectView?.frame = CGRect(x: 0, y: 90, width: self.view.frame.size.width, height: 90)
  75. self.thicknessView?.frame = CGRect(x: 0, y: 270, width: self.view.frame.size.width, height: 90)
  76. self.dottedView?.frame = CGRect(x: 0, y: 360, width: self.view.frame.size.width, height: 90)
  77. self.backBtn?.frame = CGRect(x: self.view.frame.size.width - 60, y: 5, width: 50, height: 50)
  78. }
  79. }
  80. // MARK: - Protect Mehthods
  81. override func commomInitTitle() {
  82. self.sampleView?.color = self.annotStyle?.color
  83. self.sampleView?.interiorColor = UIColor.white
  84. self.sampleView?.thickness = 4.0
  85. self.sampleView?.selecIndex = CPDFSamplesSelectedIndex(rawValue: self.annotStyle?.annotMode.rawValue ?? 0)!
  86. switch self.annotStyle?.annotMode {
  87. case .circle:
  88. self.titleLabel?.text = NSLocalizedString("Circle", comment: "")
  89. case .square:
  90. self.titleLabel?.text = NSLocalizedString("Square", comment: "")
  91. case .arrow:
  92. self.titleLabel?.text = NSLocalizedString("Arrow", comment: "")
  93. case .line:
  94. self.titleLabel?.text = NSLocalizedString("Line", comment: "")
  95. default:
  96. break
  97. }
  98. self.fillColorSelectView?.colorLabel?.text = NSLocalizedString("Fill Color", comment: "")
  99. self.thicknessView?.titleLabel?.text = NSLocalizedString("Line Width", comment: "")
  100. self.colorView?.colorLabel?.text = NSLocalizedString("Stroke Color", comment: "")
  101. self.dottedView?.titleLabel?.text = NSLocalizedString("Line and Border Style", comment: "")
  102. self.colorView?.selectedColor = self.annotStyle?.color
  103. self.fillColorSelectView?.selectedColor = self.annotStyle?.interiorColor
  104. }
  105. override func commomInitFromAnnotStyle() {
  106. self.opacitySliderView?.opacitySlider?.value = Float(self.annotStyle?.opacity ?? 1)
  107. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0)/1)*100))%"
  108. self.thicknessView?.thicknessSlider?.value = Float(self.annotStyle?.lineWidth ?? 1)
  109. self.thicknessView?.startLabel?.text = "\(Int(self.thicknessView?.thicknessSlider?.value ?? 0)) pt"
  110. self.dashPattern = self.annotStyle?.dashPattern ?? []
  111. self.dottedView?.thicknessSlider?.value = Float(truncating: self.dashPattern.first ?? 0)
  112. self.dottedView?.startLabel?.text = "\(Int(self.dottedView?.thicknessSlider?.value ?? 0)) pt"
  113. self.sampleView?.color = self.annotStyle?.color
  114. self.sampleView?.opcity = self.annotStyle?.opacity ?? 1
  115. self.sampleView?.thickness = self.annotStyle?.lineWidth ?? 1
  116. self.sampleView?.dotted = CGFloat(self.dottedView?.thicknessSlider?.value ?? 0)
  117. self.sampleView?.interiorColor = self.annotStyle?.interiorColor
  118. self.sampleView?.setNeedsDisplay()
  119. }
  120. override func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  121. if self.colorPicker?.superview != nil || self.fillColorPicker?.superview != nil {
  122. let currentDevice = UIDevice.current
  123. if currentDevice.userInterfaceIdiom == .pad {
  124. // This is an iPad
  125. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 520)
  126. } else {
  127. // This is an iPhone or iPod touch
  128. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 320)
  129. }
  130. } else {
  131. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 660)
  132. }
  133. }
  134. func updateBorderColor(_ color: UIColor?) {
  135. if let color = color {
  136. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  137. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  138. sampleView?.color = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
  139. annotStyle?.setColor(sampleView?.color)
  140. annotStyle?.setOpacity(sampleView?.opcity ?? 0)
  141. annotStyle?.setInteriorOpacity(sampleView?.opcity ?? 0)
  142. } else {
  143. sampleView?.color = color
  144. sampleView?.opcity = 0
  145. annotStyle?.setColor(color)
  146. }
  147. sampleView?.setNeedsDisplay()
  148. if annotStyle != nil {
  149. delegate?.circleViewController?(self, annotStyle: annotStyle!)
  150. }
  151. opacitySliderView?.opacitySlider?.value = Float(annotStyle?.opacity ?? 1)
  152. opacitySliderView?.startLabel?.text = "\(Int(((opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  153. }
  154. func updateFillColor(_ color: UIColor?) {
  155. if let color = color {
  156. var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0, alpha: CGFloat = 0
  157. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  158. sampleView?.interiorColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
  159. annotStyle?.setInteriorColor(sampleView?.interiorColor)
  160. annotStyle?.setOpacity(sampleView?.opcity ?? 0)
  161. annotStyle?.setInteriorOpacity(sampleView?.opcity ?? 0)
  162. } else {
  163. sampleView?.interiorColor = color
  164. sampleView?.opcity = 0
  165. annotStyle?.setColor(color)
  166. }
  167. sampleView?.setNeedsDisplay()
  168. if annotStyle != nil {
  169. delegate?.circleViewController?(self, annotStyle: annotStyle!)
  170. }
  171. opacitySliderView?.opacitySlider?.value = Float(annotStyle?.opacity ?? 1)
  172. opacitySliderView?.startLabel?.text = "\(Int(((opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  173. }
  174. // MARK: - CPDFColorPickerViewDelegate
  175. override func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
  176. if colorPickerView == colorPicker {
  177. updateBorderColor(color)
  178. } else if colorPicker == fillColorPicker {
  179. updateFillColor(color)
  180. }
  181. var red: CGFloat = 0
  182. var green: CGFloat = 0
  183. var blue: CGFloat = 0
  184. var alpha: CGFloat = 0
  185. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  186. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  187. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  188. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  189. }
  190. // MARK: - CPDFOpacitySliderViewDelegate
  191. override func opacitySliderView(_ opacitySliderView: CPDFOpacitySliderView, opacity: CGFloat) {
  192. sampleView?.opcity = opacity
  193. annotStyle?.setOpacity(opacity)
  194. annotStyle?.setInteriorOpacity(opacity)
  195. if annotStyle != nil {
  196. delegate?.circleViewController?(self, annotStyle: annotStyle!)
  197. }
  198. sampleView?.setNeedsDisplay()
  199. }
  200. // MARK: - CPDFThicknessSliderViewDelegate
  201. func thicknessSliderView(_ opacitySliderView: CPDFThicknessSliderView, thickness: CGFloat) {
  202. if opacitySliderView == thicknessView {
  203. sampleView?.thickness = thickness
  204. annotStyle?.setLineWidth(thickness)
  205. if annotStyle != nil {
  206. delegate?.circleViewController?(self, annotStyle: annotStyle!)
  207. }
  208. sampleView?.setNeedsDisplay()
  209. } else if opacitySliderView == dottedView {
  210. sampleView?.dotted = thickness
  211. annotStyle?.setStyle(.dashed)
  212. annotStyle?.setDashPattern([NSNumber(value: thickness)])
  213. if annotStyle != nil {
  214. delegate?.circleViewController?(self, annotStyle: annotStyle!)
  215. }
  216. sampleView?.setNeedsDisplay()
  217. }
  218. }
  219. // MARK: - CPDFColorSelectViewDelegate
  220. override func selectColorView(_ select: CPDFColorSelectView) {
  221. if select == self.colorView {
  222. if #available(iOS 14.0, *) {
  223. let picker = UIColorPickerViewController()
  224. picker.delegate = self
  225. self.present(picker, animated: true, completion: nil)
  226. isFill = false
  227. } else {
  228. let currentDevice = UIDevice.current
  229. if currentDevice.userInterfaceIdiom == .pad {
  230. // This is an iPad
  231. self.colorPicker = CPDFColorPickerView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 520))
  232. } else {
  233. // This is an iPhone or iPod touch
  234. self.colorPicker = CPDFColorPickerView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 320))
  235. }
  236. self.colorPicker?.delegate = self
  237. self.colorPicker?.backgroundColor = UIColor.white
  238. if colorPicker != nil {
  239. self.view.addSubview(self.colorPicker!)
  240. }
  241. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  242. }
  243. } else if select == self.fillColorSelectView {
  244. if #available(iOS 14.0, *) {
  245. let fillPicker = UIColorPickerViewController()
  246. fillPicker.delegate = self
  247. self.present(fillPicker, animated: true, completion: nil)
  248. isFill = true
  249. } else {
  250. let currentDevice = UIDevice.current
  251. if currentDevice.userInterfaceIdiom == .pad {
  252. // This is an iPad
  253. self.fillColorPicker = CPDFColorPickerView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 520))
  254. } else {
  255. // This is an iPhone or iPod touch
  256. self.fillColorPicker = CPDFColorPickerView(frame: CGRect(x: self.view.frame.origin.x, y: self.view.frame.origin.y, width: self.view.frame.size.width, height: 320))
  257. }
  258. self.fillColorPicker?.delegate = self
  259. self.fillColorPicker?.backgroundColor = UIColor.white
  260. if fillColorPicker != nil {
  261. self.view.addSubview(self.fillColorPicker!)
  262. }
  263. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  264. }
  265. }
  266. }
  267. override func selectColorView(_ select: CPDFColorSelectView, color: UIColor) {
  268. if select == colorView {
  269. updateBorderColor(color)
  270. } else if select == fillColorSelectView {
  271. updateFillColor(color)
  272. }
  273. }
  274. // MARK: - UIColorPickerViewControllerDelegate
  275. @available(iOS 14.0, *)
  276. func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  277. let color = viewController.selectedColor
  278. if isFill {
  279. updateFillColor(color)
  280. } else {
  281. updateBorderColor(color)
  282. }
  283. var red: CGFloat = 0
  284. var green: CGFloat = 0
  285. var blue: CGFloat = 0
  286. var alpha: CGFloat = 0
  287. viewController.selectedColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  288. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  289. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  290. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  291. }
  292. }