CImageWatermarkPreView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // CImageWatermarkPreView.swift
  3. // PDFViewer-Swift
  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. class CImageWatermarkPreView: UIView {
  14. var documentSize: CGSize?
  15. var documentView: UIImageView?
  16. var preImageView: UIImageView?
  17. var rotationBtn: UIButton?
  18. var preLayer: CALayer?
  19. var imageTileView: CTileWatermarkPreView?
  20. init(frame: CGRect, Image image: UIImage?) {
  21. super.init(frame: frame)
  22. documentView = UIImageView(image: image)
  23. if documentView != nil {
  24. addSubview(documentView!)
  25. }
  26. imageTileView = CTileWatermarkPreView(frame: .zero)
  27. imageTileView?.backgroundColor = .clear
  28. imageTileView?.isUserInteractionEnabled = false
  29. imageTileView?.isHidden = true
  30. if imageTileView != nil {
  31. documentView?.addSubview(imageTileView!)
  32. }
  33. rotationBtn = UIButton(type: .custom)
  34. rotationBtn?.translatesAutoresizingMaskIntoConstraints = false
  35. rotationBtn?.setImage(UIImage(named: "CWatermarkPreViewRatoteIamge", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  36. rotationBtn?.addTarget(self, action: #selector(buttonItemClicked_rotate), for: .touchUpInside)
  37. preImageView = UIImageView()
  38. preImageView?.layer.borderColor = UIColor.blue.cgColor
  39. preImageView?.layer.borderWidth = 1.0
  40. preImageView?.sizeToFit()
  41. preImageView?.backgroundColor = UIColor.white
  42. if preImageView != nil {
  43. documentView?.addSubview(preImageView!)
  44. }
  45. if rotationBtn != nil {
  46. documentView?.addSubview(rotationBtn!)
  47. }
  48. backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  49. }
  50. required init?(coder: NSCoder) {
  51. fatalError("init(coder:) has not been implemented")
  52. }
  53. override func layoutSubviews() {
  54. super.layoutSubviews()
  55. let width = (bounds.size.width - (documentSize?.width ?? 0))/2
  56. let height = (bounds.size.height - (documentSize?.height ?? 0))/2
  57. documentView?.frame = CGRect(x: width, y: height, width: documentSize?.width ?? 0, height: (documentSize?.height ?? 0) - 20)
  58. let x = Int((documentView?.bounds.width ?? 60))/2
  59. let y = Int((documentView?.bounds.width ?? 60))/4
  60. preImageView?.center = CGPoint(x: x, y: y)
  61. rotationBtn?.size = CGSize(width: 20, height: 20)
  62. imageTileView?.frame = documentView?.bounds ?? .zero
  63. imageTileView?.centerPoint = preImageView?.center ?? .zero
  64. imageTileView?.setNeedsDisplay()
  65. }
  66. override func draw(_ rect: CGRect) {
  67. super.draw(rect)
  68. rotationBtn?.center = leftBottom(view: preImageView!)
  69. }
  70. // MARK: - Action
  71. @objc func buttonItemClicked_rotate(_ sender: UIButton) {
  72. }
  73. func leftBottom(view: UIView) -> CGPoint {
  74. let transform = view.transform
  75. let scale = sqrt(transform.a * transform.a + transform.c * transform.c) // scaleX
  76. let angle = atan2(transform.b, transform.a)
  77. let originVector = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height / 2)
  78. let rotateVector = CGPoint(x: originVector.x * cos(angle) + originVector.y * sin(angle), y: originVector.y * cos(angle) - originVector.x * sin(angle))
  79. let scaleVector = CGPoint(x: rotateVector.x * scale, y: rotateVector.y * scale)
  80. return CGPoint(x: view.center.x - scaleVector.x, y: view.center.y + scaleVector.y)
  81. }
  82. }