123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- //
- // CPDFAnnotationBaseViewController.swift
- // ComPDFKit_Tools
- //
- // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
- //
- // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
- // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
- // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
- // This notice may not be removed from this file.
- //
- import UIKit
- class CPDFAnnotationBaseViewController: UIViewController, UIColorPickerViewControllerDelegate, CPDFColorSelectViewDelegate, CPDFColorPickerViewDelegate, CPDFOpacitySliderViewDelegate {
- var annotStyle:CAnnotStyle?
-
- var sampleView: CPDFSampleView?
- var colorView: CPDFColorSelectView?
- var opacitySliderView: CPDFOpacitySliderView?
- var colorPicker: CPDFColorPickerView?
- var scrcollView: UIScrollView?
- var backBtn: UIButton?
- var titleLabel: UILabel?
- var sampleBackgoundView: UIView?
- var headerView: UIView?
-
- // MARK: - Initializers
-
- init(annotStyle:CAnnotStyle) {
- self.annotStyle = annotStyle
- super.init(nibName: nil, bundle: nil)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- // MARK: - ViewController Methods
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- // Common initialization code
- self.headerView = UIView()
- self.headerView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
- self.headerView?.layer.borderWidth = 1.0
- self.headerView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
- if(self.headerView != nil) {
- self.view.addSubview(self.headerView!)
- }
-
- self.titleLabel = UILabel()
- self.titleLabel?.autoresizingMask = .flexibleRightMargin
- self.titleLabel?.textAlignment = .center
- self.titleLabel?.font = UIFont.systemFont(ofSize: 20)
- self.titleLabel?.adjustsFontSizeToFitWidth = true
- if(self.titleLabel != nil) {
- self.headerView?.addSubview(self.titleLabel!)
- }
-
- self.scrcollView = UIScrollView()
- self.scrcollView?.isScrollEnabled = true
- if(self.scrcollView != nil) {
- self.view.addSubview(self.scrcollView!)
- }
-
- self.backBtn = UIButton()
- self.backBtn?.autoresizingMask = .flexibleLeftMargin
- self.backBtn?.setImage(UIImage(named: "CPDFAnnotationBaseImageBack", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
- self.backBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
- if(self.backBtn != nil) {
- self.headerView?.addSubview(self.backBtn!)
- }
-
- self.sampleBackgoundView = UIView()
- self.sampleBackgoundView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
- self.sampleBackgoundView?.layer.borderWidth = 1.0
- self.sampleBackgoundView?.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
- if(self.sampleBackgoundView != nil) {
- self.headerView?.addSubview(self.sampleBackgoundView!)
- }
-
- self.sampleView = CPDFSampleView()
- self.sampleView?.backgroundColor = UIColor.white
- self.sampleView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
- self.sampleView?.layer.borderWidth = 1.0
- self.sampleView?.autoresizingMask = .flexibleRightMargin
- if(self.sampleView != nil) {
- self.sampleBackgoundView?.addSubview(self.sampleView!)
- }
-
- self.colorView = CPDFColorSelectView.init(frame: CGRect.zero)
- self.colorView?.delegate = self
- self.colorView?.autoresizingMask = .flexibleWidth
- if (self.colorView != nil) {
- self.scrcollView?.addSubview(self.colorView!)
- }
-
- self.opacitySliderView = CPDFOpacitySliderView(frame: CGRect.zero)
- self.opacitySliderView?.delegate = self
- self.opacitySliderView?.autoresizingMask = .flexibleWidth
- if (self.opacitySliderView != nil) {
- self.scrcollView?.addSubview(self.opacitySliderView!)
- }
-
- self.view.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
- self.updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
-
- }
-
- override func viewWillLayoutSubviews() {
- super.viewWillLayoutSubviews()
- titleLabel?.frame = CGRect(x: (view.frame.size.width - 120) / 2, y: 5, width: 120, height: 50)
- scrcollView?.frame = CGRect(x: 0, y: 170, width: view.frame.size.width, height: 210)
- headerView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 170)
- scrcollView?.contentSize = CGSize(width: view.frame.size.width, height: 330)
- sampleBackgoundView?.frame = CGRect(x: 0, y: 50, width: view.bounds.size.width, height: 120)
- sampleView?.frame = CGRect(x: (view.frame.size.width - 300) / 2, y: 15, width: 300, height: (sampleBackgoundView?.bounds.size.height ?? 0) - 30)
-
- if #available(iOS 11.0, *) {
- colorPicker?.frame = CGRect(x: view.safeAreaInsets.left, y: 0, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: view.frame.size.height)
- colorView?.frame = CGRect(x: view.safeAreaInsets.left, y: 0, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: 90)
- opacitySliderView?.frame = CGRect(x: view.safeAreaInsets.left, y: 90, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: 90)
- backBtn?.frame = CGRect(x: view.frame.size.width - 60 - view.safeAreaInsets.right, y: 5, width: 50, height: 50)
- } else {
- colorView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 90)
- opacitySliderView?.frame = CGRect(x: 0, y: 90, width: view.frame.size.width, height: 90)
- backBtn?.frame = CGRect(x: view.frame.size.width - 60, y: 5, width: 50, height: 50)
- }
- }
-
- override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
- super.willTransition(to: newCollection, with: coordinator)
- updatePreferredContentSizeWithTraitCollection(traitCollection: newCollection)
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- commomInitTitle()
- commomInitFromAnnotStyle()
- }
-
- // MARK: - Protect Methods
-
- func commomInitTitle() {
- self.titleLabel?.text = NSLocalizedString("Note", comment: "")
- self.sampleView?.selecIndex = .highlight
- self.colorView?.colorLabel?.text = NSLocalizedString("Color", comment: "")
- self.colorView?.selectedColor = self.annotStyle?.color
- self.colorView?.setNeedsLayout()
- }
-
- func commomInitFromAnnotStyle() {
- self.sampleView?.color = self.annotStyle?.color
- self.sampleView?.opcity = self.annotStyle?.opacity ?? 0
- self.opacitySliderView?.opacitySlider?.value = Float(self.annotStyle?.opacity ?? 0)
- self.opacitySliderView?.startLabel?.text = "\(Int((self.opacitySliderView?.opacitySlider?.value ?? 0)/1*100))%"
- }
-
- func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
- if self.colorPicker?.superview != nil {
- let currentDevice = UIDevice.current
- if currentDevice.userInterfaceIdiom == .pad {
- // This is an iPad
- self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 520)
- } else {
- // This is an iPhone or iPod touch
- self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 320)
- }
- } else {
- self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 420)
- }
- }
-
- // MARK: - Action
-
- @objc func buttonItemClicked_back(_ button: UIButton) {
- dismiss(animated: true)
- }
-
- // MARK: - CPDFColorSelectViewDelegate
-
- func selectColorView(_ select: CPDFColorSelectView) {
- if #available(iOS 14.0, *) {
- let picker = UIColorPickerViewController()
- picker.delegate = self
- self.present(picker, animated: true, completion: nil)
- } else {
- let currentDevice = UIDevice.current
- if currentDevice.userInterfaceIdiom == .pad {
- // This is an iPad
- 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))
- } else {
- // This is an iPhone or iPod touch
- 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))
- }
- self.colorPicker?.delegate = self
- self.colorView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- self.colorPicker?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
- if(self.colorPicker != nil) {
- self.view.addSubview(self.colorPicker!)
- }
-
- updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
- }
-
- }
-
- func selectColorView(_ select: CPDFColorSelectView, color: UIColor) {
- sampleView?.color = color
- sampleView?.setNeedsDisplay()
- }
-
- // MARK: - CPDFColorPickerViewDelegate
-
- func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
- self.sampleView?.color = color
- self.sampleView?.setNeedsDisplay()
- var red: CGFloat = 0
- var green: CGFloat = 0
- var blue: CGFloat = 0
- var alpha: CGFloat = 0
- color.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
- self.opacitySliderView?.opacitySlider?.value = Float(alpha)
- self.opacitySliderView?.startLabel?.text = "\(Int(((self.opacitySliderView?.opacitySlider?.value ?? 0)/1)*100))%"
- self.updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
- }
-
- // MARK: - CPDFOpacitySliderViewDelegate
-
- func opacitySliderView(_ opacitySliderView: CPDFOpacitySliderView, opacity: CGFloat) {
- sampleView?.opcity = opacity
- sampleView?.setNeedsDisplay()
- }
-
- }
|