CPDFShapeArrowViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // CPDFShapeArrowViewController.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 protocol CPDFShapeArrowViewControllerDelegate: AnyObject {
  15. @objc optional func arrowViewController(_ arrowViewController: CPDFShapeArrowViewController, annotStyle: CAnnotStyle)
  16. }
  17. class CPDFShapeArrowViewController: CPDFShapeCircleViewController, CPDFArrowStyleViewDelegate, CShapeSelectViewDelegate {
  18. weak var lineDelegate: CPDFShapeArrowViewControllerDelegate?
  19. private var arrowLabel: UILabel?
  20. private var arrowBtn: UIButton?
  21. private var trialLabel: UILabel?
  22. private var trialBtn: UIButton?
  23. private var startArrowStyleView: CPDFArrowStyleView?
  24. private var endArrowStyleView: CPDFArrowStyleView?
  25. private var startDrawView: CPDFDrawArrowView?
  26. private var endDrawView: CPDFDrawArrowView?
  27. private var dashPattern: [NSNumber] = []
  28. private var shapeSelectView: CShapeSelectView?
  29. // MARK: - Initializers
  30. override init(annotStyle: CAnnotStyle) {
  31. super.init(annotStyle: annotStyle)
  32. self.annotStyle = annotStyle
  33. }
  34. // MARK: - ViewController Methods
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. // Do any additional setup after loading the view.
  38. self.arrowLabel = UILabel()
  39. self.arrowLabel?.text = NSLocalizedString("Start", comment: "")
  40. self.arrowLabel?.textColor = UIColor.gray
  41. self.arrowLabel?.font = UIFont.systemFont(ofSize: 12.0)
  42. if self.arrowLabel != nil {
  43. self.scrcollView?.addSubview(self.arrowLabel!)
  44. }
  45. self.arrowBtn = UIButton()
  46. self.arrowBtn?.setImage(UIImage(named: "CPDFShapeArrowImageStart", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  47. self.arrowBtn?.layer.borderWidth = 1.0
  48. self.arrowBtn?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  49. self.arrowBtn?.autoresizingMask = .flexibleLeftMargin
  50. self.arrowBtn?.addTarget(self, action: #selector(buttonItemClicked_start(_:)), for: .touchUpInside)
  51. if self.arrowBtn != nil {
  52. self.scrcollView?.addSubview(self.arrowBtn!)
  53. }
  54. self.startDrawView = CPDFDrawArrowView(frame: CGRect.zero)
  55. self.startDrawView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  56. if self.startDrawView != nil {
  57. self.arrowBtn?.addSubview(self.startDrawView!)
  58. }
  59. self.trialLabel = UILabel()
  60. self.trialLabel?.text = NSLocalizedString("End", comment: "")
  61. self.trialLabel?.textColor = UIColor.gray
  62. self.trialLabel?.font = UIFont.systemFont(ofSize: 12.0)
  63. self.trialLabel?.autoresizingMask = .flexibleRightMargin
  64. if self.trialLabel != nil {
  65. self.scrcollView?.addSubview(self.trialLabel!)
  66. }
  67. self.trialBtn = UIButton()
  68. self.trialBtn?.setImage(UIImage(named: "CPDFShapeArrowImageEnd", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  69. self.trialBtn?.layer.borderWidth = 1.0
  70. self.trialBtn?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  71. self.trialBtn?.addTarget(self, action: #selector(buttonItemClicked_trial(_:)), for: .touchUpInside)
  72. self.trialBtn?.autoresizingMask = .flexibleLeftMargin
  73. if self.trialLabel != nil {
  74. self.scrcollView?.addSubview(self.trialBtn!)
  75. }
  76. self.endDrawView = CPDFDrawArrowView(frame: CGRect.zero)
  77. self.endDrawView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  78. if self.endDrawView != nil {
  79. self.trialBtn?.addSubview(self.endDrawView!)
  80. }
  81. self.fillColorSelectView?.isHidden = true
  82. self.view.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  83. }
  84. required init?(coder: NSCoder) {
  85. fatalError("init(coder:) has not been implemented")
  86. }
  87. override func viewWillLayoutSubviews() {
  88. super.viewWillLayoutSubviews()
  89. self.scrcollView?.frame = CGRect(x: 0, y: 170, width: self.view.frame.size.width, height: self.view.frame.size.height-170)
  90. self.scrcollView?.contentSize = CGSize(width: self.view.frame.size.width, height: 500)
  91. if #available(iOS 11.0, *) {
  92. var offsetY: CGFloat = 0
  93. 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)
  94. offsetY += self.colorView?.frame.size.height ?? 0
  95. self.opacitySliderView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: offsetY, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  96. offsetY += self.opacitySliderView?.frame.size.height ?? 0
  97. self.thicknessView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: offsetY, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  98. offsetY += self.thicknessView?.frame.size.height ?? 0
  99. self.dottedView?.frame = CGRect(x: self.view.safeAreaInsets.left, y: offsetY, width: self.view.frame.size.width - self.view.safeAreaInsets.left - self.view.safeAreaInsets.right, height: 90)
  100. offsetY += (self.dottedView?.frame.size.height ?? 0)
  101. self.arrowLabel?.frame = CGRect(x: self.view.safeAreaInsets.left+20, y: offsetY, width: 100, height: 45)
  102. self.arrowBtn?.frame = CGRect(x: self.view.frame.size.width - 100 - self.view.safeAreaInsets.right, y: offsetY + 7.5, width: 80, height: 30)
  103. offsetY += self.arrowLabel?.frame.size.height ?? 0
  104. self.startDrawView?.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
  105. self.trialLabel?.frame = CGRect(x: self.view.safeAreaInsets.left+20, y: offsetY, width: 100, height: 45)
  106. self.trialBtn?.frame = CGRect(x: self.view.frame.size.width - 100 - self.view.safeAreaInsets.right, y: offsetY + 7.5, width: 80, height: 30)
  107. self.endDrawView?.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
  108. } else {
  109. var offsetY: CGFloat = 0
  110. self.colorView?.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 90)
  111. offsetY += self.colorView?.frame.size.height ?? 0
  112. self.opacitySliderView?.frame = CGRect(x: 0, y: offsetY, width: self.view.frame.size.width - 0, height: 90)
  113. offsetY += self.opacitySliderView?.frame.size.height ?? 0
  114. self.thicknessView?.frame = CGRect(x: 0, y: offsetY, width: self.view.frame.size.width, height: 90)
  115. offsetY += self.thicknessView?.frame.size.height ?? 0
  116. self.dottedView?.frame = CGRect(x: 0, y: offsetY, width: self.view.frame.size.width, height: 90)
  117. offsetY += self.dottedView?.frame.size.height ?? 0
  118. self.arrowLabel?.frame = CGRect(x: 20, y: offsetY, width: 100, height: 45)
  119. self.arrowBtn?.frame = CGRect(x: self.view.frame.size.width - 100, y: offsetY + 7.5, width: 80, height: 30)
  120. offsetY += self.arrowLabel?.frame.size.height ?? 0
  121. self.startDrawView?.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
  122. self.trialLabel?.frame = CGRect(x: 20, y: offsetY, width: 100, height: 45)
  123. self.trialBtn?.frame = CGRect(x: self.view.frame.size.width - 100, y: offsetY + 7.5, width: 80, height: 30)
  124. self.endDrawView?.frame = CGRect(x: 0, y: 0, width: 40, height: 30)
  125. }
  126. }
  127. // MARK: - Protect Mehthods
  128. override func commomInitFromAnnotStyle() {
  129. self.opacitySliderView?.opacitySlider?.value = Float(self.annotStyle?.opacity ?? 0)
  130. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0)/1)*100))%"
  131. self.thicknessView?.thicknessSlider?.value = Float(self.annotStyle?.lineWidth ?? 0)
  132. self.thicknessView?.startLabel?.text = "\(Int(self.thicknessView?.thicknessSlider?.value ?? 0)) pt"
  133. self.dashPattern = self.annotStyle?.dashPattern ?? []
  134. self.dottedView?.thicknessSlider?.value = Float(truncating: self.dashPattern.first ?? 0)
  135. self.dottedView?.startLabel?.text = "\(Int(self.dottedView?.thicknessSlider?.value ?? 0)) pt"
  136. self.startDrawView?.selectIndex = self.annotStyle?.startLineStyle.rawValue ?? 0
  137. self.endDrawView?.selectIndex = self.annotStyle?.endLineStyle.rawValue ?? 1
  138. startDrawView?.setNeedsDisplay()
  139. endDrawView?.setNeedsDisplay()
  140. self.sampleView?.color = self.annotStyle?.color
  141. self.sampleView?.opcity = self.annotStyle?.opacity ?? 1
  142. self.sampleView?.thickness = self.annotStyle?.lineWidth ?? 1
  143. self.sampleView?.dotted = CGFloat(self.dottedView?.thicknessSlider?.value ?? 0)
  144. self.sampleView?.interiorColor = self.annotStyle?.interiorColor
  145. self.sampleView?.startArrowStyleIndex = CPDFArrowStyle(rawValue: self.annotStyle?.startLineStyle.rawValue ?? 0)!
  146. self.sampleView?.endArrowStyleIndex = CPDFArrowStyle(rawValue: self.annotStyle?.endLineStyle.rawValue ?? 0)!
  147. self.sampleView?.setNeedsDisplay()
  148. }
  149. override func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  150. if self.colorPicker?.superview != nil || self.fillColorPicker?.superview != nil {
  151. let currentDevice = UIDevice.current
  152. if currentDevice.userInterfaceIdiom == .pad {
  153. // This is an iPad
  154. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 520)
  155. } else {
  156. // This is an iPhone or iPod touch
  157. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 320)
  158. }
  159. } else {
  160. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 660)
  161. }
  162. }
  163. // MARK: - Action
  164. @objc func buttonItemClicked_start(_ sender: Any) {
  165. self.startArrowStyleView = CPDFArrowStyleView(title: NSLocalizedString("Arrow Style", comment: ""))
  166. self.startArrowStyleView?.frame = self.view.frame
  167. self.startArrowStyleView?.delegate = self
  168. self.startArrowStyleView?.selectIndex = self.startDrawView?.selectIndex ?? 0
  169. self.startArrowStyleView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  170. if (self.startArrowStyleView != nil) {
  171. self.view.addSubview(self.startArrowStyleView!)
  172. }
  173. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  174. }
  175. @objc func buttonItemClicked_trial(_ sender: Any) {
  176. self.endArrowStyleView = CPDFArrowStyleView(title: NSLocalizedString("Arrowtail style", comment: ""))
  177. self.endArrowStyleView?.frame = self.view.frame
  178. self.endArrowStyleView?.delegate = self
  179. self.endArrowStyleView?.selectIndex = self.endDrawView?.selectIndex ?? 0
  180. self.endArrowStyleView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  181. if (self.endArrowStyleView != nil) {
  182. self.view.addSubview(self.endArrowStyleView!)
  183. }
  184. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  185. }
  186. // MARK: - CPDFArrowStyleViewDelegate
  187. func arrowStyleView(_ arrowStyleView: CPDFArrowStyleView, selectIndex: Int) {
  188. if arrowStyleView == self.startArrowStyleView {
  189. self.sampleView?.startArrowStyleIndex = CPDFArrowStyle(rawValue: selectIndex) ?? .none
  190. self.annotStyle?.setStartLineStyle(CPDFLineStyle(rawValue: selectIndex) ?? .none)
  191. if annotStyle != nil {
  192. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  193. }
  194. self.sampleView?.setNeedsDisplay()
  195. self.startDrawView?.selectIndex = selectIndex
  196. self.startDrawView?.setNeedsDisplay()
  197. } else if arrowStyleView == self.endArrowStyleView {
  198. self.sampleView?.endArrowStyleIndex = CPDFArrowStyle(rawValue: selectIndex) ?? .none
  199. self.annotStyle?.setEndLineStyle(CPDFLineStyle(rawValue: selectIndex) ?? .none)
  200. if annotStyle != nil {
  201. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  202. }
  203. self.sampleView?.setNeedsDisplay()
  204. self.endDrawView?.selectIndex = selectIndex
  205. self.endDrawView?.setNeedsDisplay()
  206. }
  207. }
  208. func arrowStyleRemoveView(_ arrowStyleView: CPDFArrowStyleView) {
  209. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  210. }
  211. // MARK: - CPDFOpacitySliderViewDelegate
  212. override func opacitySliderView(_ opacitySliderView: CPDFOpacitySliderView, opacity: CGFloat) {
  213. sampleView?.opcity = opacity
  214. annotStyle?.setOpacity(opacity)
  215. if annotStyle != nil {
  216. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  217. }
  218. sampleView?.setNeedsDisplay()
  219. }
  220. // MARK: - CPDFThicknessSliderViewDelegate
  221. override func thicknessSliderView(_ opacitySliderView: CPDFThicknessSliderView, thickness: CGFloat) {
  222. if opacitySliderView == thicknessView {
  223. sampleView?.thickness = thickness
  224. annotStyle?.setLineWidth(thickness)
  225. if annotStyle != nil {
  226. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  227. }
  228. sampleView?.setNeedsDisplay()
  229. } else if opacitySliderView == dottedView {
  230. sampleView?.dotted = thickness
  231. annotStyle?.setStyle(.dashed)
  232. annotStyle?.setDashPattern([NSNumber(value: thickness)])
  233. if annotStyle != nil {
  234. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  235. }
  236. sampleView?.setNeedsDisplay()
  237. }
  238. }
  239. // MARK: - CPDFColorSelectViewDelegate
  240. override func selectColorView(_ select: CPDFColorSelectView) {
  241. if select == self.colorView {
  242. if #available(iOS 14.0, *) {
  243. let picker = UIColorPickerViewController()
  244. picker.delegate = self
  245. self.present(picker, animated: true, completion: nil)
  246. isFill = false
  247. } else {
  248. let currentDevice = UIDevice.current
  249. if currentDevice.userInterfaceIdiom == .pad {
  250. // This is an iPad
  251. 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))
  252. } else {
  253. // This is an iPhone or iPod touch
  254. 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))
  255. }
  256. self.colorPicker?.delegate = self
  257. self.colorPicker?.backgroundColor = UIColor.white
  258. if(self.colorPicker != nil) {
  259. self.view.addSubview(self.colorPicker!)
  260. }
  261. self.updatePreferredContentSizeWithTraitCollection(traitCollection: self.traitCollection)
  262. }
  263. }
  264. }
  265. override func selectColorView(_ select: CPDFColorSelectView, color: UIColor) {
  266. if select == colorView {
  267. sampleView?.color = color
  268. annotStyle?.setColor(color)
  269. sampleView?.setNeedsDisplay()
  270. if annotStyle != nil {
  271. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  272. }
  273. } else if select == fillColorSelectView {
  274. sampleView?.interiorColor = color
  275. annotStyle?.setInteriorColor(color)
  276. sampleView?.setNeedsDisplay()
  277. if annotStyle != nil {
  278. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  279. }
  280. }
  281. }
  282. // MARK: - CPDFColorPickerViewDelegate
  283. override func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
  284. if colorPickerView == colorPicker {
  285. sampleView?.color = color
  286. annotStyle?.setColor(color)
  287. sampleView?.setNeedsDisplay()
  288. if annotStyle != nil {
  289. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  290. }
  291. }
  292. var red: CGFloat = 0
  293. var green: CGFloat = 0
  294. var blue: CGFloat = 0
  295. var alpha: CGFloat = 0
  296. color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  297. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  298. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  299. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  300. }
  301. // MARK: - UIColorPickerViewControllerDelegate
  302. @available(iOS 14.0, *)
  303. override func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  304. let color = viewController.selectedColor
  305. sampleView?.color = color
  306. annotStyle?.setColor(color)
  307. sampleView?.setNeedsDisplay()
  308. if annotStyle != nil {
  309. lineDelegate?.arrowViewController?(self, annotStyle: self.annotStyle!)
  310. }
  311. var red: CGFloat = 0
  312. var green: CGFloat = 0
  313. var blue: CGFloat = 0
  314. var alpha: CGFloat = 0
  315. viewController.selectedColor.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
  316. self.opacitySliderView?.opacitySlider?.value = Float(alpha)
  317. self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0) / 1) * 100))%"
  318. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  319. }
  320. }