CShapeSelectView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // CShapeSelectView.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. enum CShapeSelectType: Int {
  14. case square = 0
  15. case circle
  16. case arrow
  17. case line
  18. }
  19. @objc protocol CShapeSelectViewDelegate: AnyObject {
  20. @objc optional func shapeSelectView(_ shapeSelectView: CShapeSelectView, tag: Int)
  21. }
  22. class CShapeSelectView: UIView {
  23. weak var delegate: CShapeSelectViewDelegate?
  24. private var buttonArray: [UIButton] = []
  25. // MARK: - Initializers
  26. override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. let squareButton = UIButton()
  29. squareButton.setImage(UIImage(named: "CPDFShapeArrowImageSquare", in: Bundle(for: Self.self), compatibleWith: nil), for: .normal)
  30. squareButton.addTarget(self, action: #selector(buttonItemClicked(_:)), for: .touchUpInside)
  31. squareButton.tag = CShapeSelectType.square.rawValue
  32. addSubview(squareButton)
  33. buttonArray.append(squareButton)
  34. let circleButton = UIButton()
  35. circleButton.setImage(UIImage(named: "CPDFShapeArrowImageCircle", in: Bundle(for: Self.self), compatibleWith: nil), for: .normal)
  36. circleButton.addTarget(self, action: #selector(buttonItemClicked(_:)), for: .touchUpInside)
  37. circleButton.tag = CShapeSelectType.circle.rawValue
  38. addSubview(circleButton)
  39. buttonArray.append(circleButton)
  40. let arrowButton = UIButton()
  41. arrowButton.setImage(UIImage(named: "CPDFShapeArrowImageArrow", in: Bundle(for: Self.self), compatibleWith: nil), for: .normal)
  42. arrowButton.addTarget(self, action: #selector(buttonItemClicked(_:)), for: .touchUpInside)
  43. arrowButton.tag = CShapeSelectType.arrow.rawValue
  44. addSubview(arrowButton)
  45. buttonArray.append(arrowButton)
  46. let lineButton = UIButton()
  47. lineButton.setImage(UIImage(named: "CPDFShapeArrowImageLine", in: Bundle(for: Self.self), compatibleWith: nil), for: .normal)
  48. lineButton.addTarget(self, action: #selector(buttonItemClicked(_:)), for: .touchUpInside)
  49. lineButton.tag = CShapeSelectType.line.rawValue
  50. addSubview(lineButton)
  51. buttonArray.append(lineButton)
  52. }
  53. required init?(coder: NSCoder) {
  54. fatalError("init(coder:) has not been implemented")
  55. }
  56. override func layoutSubviews() {
  57. super.layoutSubviews()
  58. for (i, button) in buttonArray.enumerated() {
  59. button.frame = CGRect(x: (bounds.size.width - (bounds.size.height * 4))/5 * CGFloat(i+1) + bounds.size.height * CGFloat(i), y: 0, width: bounds.size.height, height: bounds.size.height)
  60. }
  61. }
  62. // MARK: - Action
  63. @objc func buttonItemClicked(_ button: UIButton) {
  64. for btn in buttonArray {
  65. btn.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  66. }
  67. buttonArray[button.tag].backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  68. delegate?.shapeSelectView?(self, tag: button.tag)
  69. }
  70. }