// // NotificationTopBarWindow.swift // MTP2_iOS // // Created by Handy_Cao on 2019/1/22. // Copyright © 2019 Muchinfo. All rights reserved. // import UIKit class NotificationTopBarWindow: UIWindow { // MARK: - 属性列表 /// 显示信息内容 private var textLabel: UILabel! /// 轻扫手势,用于用户手动隐藏 private var swipGestureRecognizer: UISwipeGestureRecognizer! /// 是否正在显示模式 var isShowing: Bool // MARK: - 初始化&&轻扫方法实现 init() { textLabel = UILabel(frame: CGRect(x: 0, y: kStatusBarHeight, width: kScreenWidth, height: 44)) textLabel.textAlignment = .center textLabel.backgroundColor = .clear textLabel.textColor = .white textLabel.font = UIFont(name: "Helvetica", size: 14) isShowing = false super.init(frame: CGRect(x: 0, y: -NaviHeight, width: kScreenWidth, height: NaviHeight)) self.backgroundColor = UIColor(red: 1, green: 66.0/255.0, blue: 66.0/255.0, alpha: 1.0) self.windowLevel = .statusBar + 1 self.clipsToBounds = true addSubview(textLabel) swipGestureRecognizer = UISwipeGestureRecognizer(target: self, action: #selector(swipGestureRecognizer(sender:))) swipGestureRecognizer.direction = .up isHidden = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc private func swipGestureRecognizer(sender: UISwipeGestureRecognizer) { dismiss() } // MARK: - 显示相关 /// 在app顶部显示提示信息 /// /// - Parameters: /// - message: 提示信息内容 /// - delayTime: 停留时间,如果不填,则不会自动消失 func addMessage(_ message: String, delay delayTime: TimeInterval? = nil) { func completeBlock(delay delayTime: TimeInterval? = nil) { if let delayTime = delayTime { DispatchQueue.main.asyncAfter(deadline: .now() + delayTime) { // 隐藏 self.dismiss() } } } dPrint("[\(#function)]:\(message)") if !isShowing { textLabel.text = message isHidden = false isShowing = true addGestureRecognizer(swipGestureRecognizer) UIView.animate(withDuration: 0.3, animations: { self.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: NaviHeight) }) { (_) in completeBlock(delay: delayTime) } } else { DispatchQueue.main.asyncAfter(deadline: .now() + 1) { self.textLabel.text = message completeBlock(delay: delayTime) } } } /// 隐藏 func dismiss() { isShowing = false UIView.animate(withDuration: 0.3, animations: { self.frame = CGRect(x: 0, y: -NaviHeight, width: kScreenWidth, height: NaviHeight) }) { (_) in self.isHidden = true self.removeGestureRecognizer(self.swipGestureRecognizer) } } }