NotificationTopBarWindow.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NotificationTopBarWindow.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2019/1/22.
  6. // Copyright © 2019 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. class NotificationTopBarWindow: UIWindow {
  10. // MARK: - 属性列表
  11. /// 显示信息内容
  12. private var textLabel: UILabel!
  13. /// 轻扫手势,用于用户手动隐藏
  14. private var swipGestureRecognizer: UISwipeGestureRecognizer!
  15. /// 是否正在显示模式
  16. var isShowing: Bool
  17. // MARK: - 初始化&&轻扫方法实现
  18. init() {
  19. textLabel = UILabel(frame: CGRect(x: 0, y: kStatusBarHeight, width: kScreenWidth, height: 44))
  20. textLabel.textAlignment = .center
  21. textLabel.backgroundColor = .clear
  22. textLabel.textColor = .white
  23. textLabel.font = UIFont(name: "Helvetica", size: 14)
  24. isShowing = false
  25. super.init(frame: CGRect(x: 0, y: -NaviHeight, width: kScreenWidth, height: NaviHeight))
  26. self.backgroundColor = UIColor(red: 1, green: 66.0/255.0, blue: 66.0/255.0, alpha: 1.0)
  27. self.windowLevel = .statusBar + 1
  28. self.clipsToBounds = true
  29. addSubview(textLabel)
  30. swipGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipGestureRecognizer(sender:)))
  31. swipGestureRecognizer.direction = .up
  32. isHidden = true
  33. }
  34. required init?(coder aDecoder: NSCoder) {
  35. fatalError("init(coder:) has not been implemented")
  36. }
  37. @objc private func swipGestureRecognizer(sender: UISwipeGestureRecognizer) {
  38. dismiss()
  39. }
  40. // MARK: - 显示相关
  41. /// 在app顶部显示提示信息
  42. ///
  43. /// - Parameters:
  44. /// - message: 提示信息内容
  45. /// - delayTime: 停留时间,如果不填,则不会自动消失
  46. func addMessage(_ message: String, delay delayTime: TimeInterval? = nil) {
  47. func completeBlock(delay delayTime: TimeInterval? = nil) {
  48. if let delayTime = delayTime {
  49. DispatchQueue.main.asyncAfter(deadline: .now() + delayTime) {
  50. // 隐藏
  51. self.dismiss()
  52. }
  53. }
  54. }
  55. dPrint("[\(#function)]:\(message)")
  56. if !isShowing {
  57. textLabel.text = message
  58. isHidden = false
  59. isShowing = true
  60. addGestureRecognizer(swipGestureRecognizer)
  61. UIView.animate(withDuration: 0.3, animations: {
  62. self.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: NaviHeight)
  63. }) { (_) in
  64. completeBlock(delay: delayTime)
  65. }
  66. } else {
  67. DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
  68. self.textLabel.text = message
  69. completeBlock(delay: delayTime)
  70. }
  71. }
  72. }
  73. /// 隐藏
  74. func dismiss() {
  75. isShowing = false
  76. UIView.animate(withDuration: 0.3, animations: {
  77. self.frame = CGRect(x: 0, y: -NaviHeight, width: kScreenWidth, height: NaviHeight)
  78. }) { (_) in
  79. self.isHidden = true
  80. self.removeGestureRecognizer(self.swipGestureRecognizer)
  81. }
  82. }
  83. }