SaveSuccessTipsView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // SaveSuccessTipsView.swift
  3. // PhysicalWallPaper
  4. //
  5. // Created by nkl on 2024/11/8.
  6. //
  7. import Foundation
  8. import Photos
  9. import UIKit
  10. class SaveSuccessTipsView: UIView {
  11. lazy var iconView: UIImageView = .simpleImage(imageName: "ic_saved")
  12. lazy var textLabel = UILabel()
  13. lazy var viewButton: UIButton = {
  14. let btn = UIButton()
  15. btn.backgroundColor = .hexColor("#111111")
  16. btn.setTitle("View".localized, for: .normal)
  17. btn.setTitleColor(.hexColor("#6EF4F4"), for: .normal)
  18. btn.titleLabel?.font = .boldSystemFont16
  19. btn.layer.cornerRadius = 14
  20. btn.layer.masksToBounds = true
  21. btn.addTarget(self, action: #selector(openPhotoLibrary), for: .touchUpInside)
  22. return btn
  23. }()
  24. var viewActionHandler: (() -> Void)?
  25. override init(frame: CGRect) {
  26. super.init(frame: CGRect(x: 0, y: 0, width: 288, height: 48))
  27. layer.cornerRadius = 8
  28. backgroundColor = .white
  29. addSubview(iconView)
  30. iconView.snp.makeConstraints { make in
  31. make.width.height.equalTo(24)
  32. make.centerY.equalToSuperview()
  33. make.leading.equalTo(16)
  34. }
  35. textLabel.textColor = UIColor.black
  36. textLabel.font = .boldSystemFont14
  37. textLabel.text = "Saved"
  38. addSubview(textLabel)
  39. textLabel.snp.makeConstraints { make in
  40. make.leading.equalTo(iconView.snp.trailing).offset(12)
  41. make.centerY.equalToSuperview()
  42. }
  43. addSubview(viewButton)
  44. viewButton.snp.makeConstraints { make in
  45. make.width.equalTo(64)
  46. make.height.equalTo(28)
  47. make.centerY.equalToSuperview()
  48. make.trailing.equalTo(-12)
  49. }
  50. viewButton.addTarget(self, action: #selector(buttonClick(_:)), for: .touchUpInside)
  51. }
  52. // 打开相册
  53. @objc func openPhotoLibrary() {
  54. let photosAppURL = URL(string: "photos-redirect://")!
  55. if UIApplication.shared.canOpenURL(photosAppURL) {
  56. UIApplication.shared.open(photosAppURL, options: [:], completionHandler: nil)
  57. } else {
  58. print("无法打开照片应用")
  59. }
  60. }
  61. required init?(coder: NSCoder) {
  62. fatalError("init(coder:) has not been implemented")
  63. }
  64. @objc func buttonClick(_ sender: UIButton) {
  65. if let viewActionHandler = viewActionHandler {
  66. viewActionHandler()
  67. } else {
  68. // UIApplication.shared.openPhotoLibrary()
  69. }
  70. }
  71. static var isShowing = false
  72. }
  73. extension SaveSuccessTipsView {
  74. static func show(at view: UIView?,
  75. at topOffset: CGFloat? = nil,
  76. viewActionHandler: (() -> Void)? = nil) {
  77. guard !Self.isShowing, let view = view else {
  78. return
  79. }
  80. Self.isShowing = true
  81. let tip = SaveSuccessTipsView()
  82. tip.viewActionHandler = viewActionHandler
  83. view.addSubview(tip)
  84. tip.snp.makeConstraints { make in
  85. make.centerX.equalToSuperview()
  86. make.size.equalTo(tip.frame.size)
  87. if let topOffset = topOffset {
  88. make.top.equalTo(topOffset)
  89. } else {
  90. make.bottom.equalTo(-200)
  91. }
  92. }
  93. UIImpactFeedbackGenerator(style: .soft).impactOccurred()
  94. DispatchQueue.global().asyncAfter(deadline: .now() + 2.5) {
  95. DispatchQueue.main.async {
  96. tip.removeFromSuperview()
  97. }
  98. Self.isShowing = false
  99. }
  100. }
  101. }