O2MindMapAddLinkDialog.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // O2MindMapAddLinkDialog.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2022/1/7.
  6. // Copyright © 2022 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. protocol O2MindMapAddLinkDialogDelegate {
  10. func deleteLink()
  11. func saveLink(link: String, linkTitle:String)
  12. }
  13. class O2MindMapAddLinkDialog: UIView, NibLoadable {
  14. @IBOutlet weak var bottomView: UIView!
  15. @IBOutlet weak var linkText: UITextField!
  16. @IBOutlet weak var linkTitleText: UITextField!
  17. @IBOutlet weak var bottomViewConstraint: NSLayoutConstraint!
  18. @IBOutlet weak var submitBtn: UIButton!
  19. private var delegate: O2MindMapAddLinkDialogDelegate? = nil
  20. @IBAction func clickCancelBtn(_ sender: UIButton) {
  21. self.dismiss()
  22. }
  23. @IBAction func clickDeleteLinkBtn(_ sender: Any) {
  24. self.delegate?.deleteLink()
  25. self.dismiss()
  26. }
  27. @IBAction func clickSubmitBtn(_ sender: UIButton) {
  28. let link = self.linkText.text ?? ""
  29. if link == "" || link.trimmingCharacters(in: .whitespacesAndNewlines) == "" {
  30. self.chrysan.show(.error ,message: "请输入链接地址", hideDelay: 2)
  31. return
  32. }
  33. if !Validate.URL(link).isRight {
  34. self.chrysan.show(.error, message: "请输入正确的链接地址", hideDelay: 2)
  35. return
  36. }
  37. let linkTitle = self.linkTitleText.text ?? ""
  38. self.delegate?.saveLink(link: link, linkTitle: linkTitle)
  39. self.dismiss()
  40. }
  41. class func mindMapNodeLinkDialog(link: String?, linkTitle: String?, delegate: O2MindMapAddLinkDialogDelegate?)-> O2MindMapAddLinkDialog {
  42. let dialog: O2MindMapAddLinkDialog = O2MindMapAddLinkDialog.loadViewFromNib()
  43. dialog.initUI(link: link, linkTitle: linkTitle)
  44. dialog.delegate = delegate
  45. return dialog
  46. }
  47. func show() {
  48. UIApplication.shared.keyWindow!.addSubview(self)
  49. self.frame = UIScreen.main.bounds
  50. UIView.animate(withDuration: 0.3, animations: {
  51. self.bottomViewConstraint.constant = 0
  52. self.backgroundColor = UIColor.hexRGB(0x000000, 0.5)
  53. self.layoutIfNeeded()
  54. }, completion: { (finish) in
  55. })
  56. }
  57. @objc func dismiss() {
  58. UIView.animate(withDuration: 0.3, animations: {
  59. self.bottomViewConstraint.constant = self.bottomView.frame.size.height
  60. self.backgroundColor = UIColor.hexRGB(0x000000, 0.0)
  61. self.layoutIfNeeded()
  62. }, completion: { (finished) in
  63. self.removeFromSuperview()
  64. })
  65. }
  66. private func initUI(link: String?, linkTitle: String?) {
  67. self.submitBtn.setBackgroundColor(base_color, forState: .normal)
  68. self.submitBtn.setCornerRadius(radius: 4)
  69. self.linkText.text = link ?? ""
  70. self.linkTitleText.text = linkTitle ?? ""
  71. bottomViewConstraint.constant = bottomView.frame.size.height
  72. backgroundColor = UIColor.hexRGB(0x000000, 0.0)
  73. let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
  74. tap.delegate = self
  75. addGestureRecognizer(tap)
  76. }
  77. }
  78. // MARK: - UIGestureRecognizerDelegate
  79. extension O2MindMapAddLinkDialog: UIGestureRecognizerDelegate {
  80. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  81. guard let touchView = touch.view else { return false }
  82. if touchView.isDescendant(of: bottomView) {
  83. // 点击的view是否是bottomView或者bottomView的子视图
  84. return false
  85. }
  86. if touchView.isDescendant(of: self.chrysan) {
  87. // 提示信息点击不关闭
  88. return false
  89. }
  90. return true
  91. }
  92. }