swift3.0 一个屏幕下方灰色提示条

提示条效果


import UIKit

class PromptView: UIView {


    /// 显示提示文字
    ///
    /// - Parameter text: 显示的文字
    public class func show(text: String) {
        PromptView.share.textDidSet(text: text)
    }

    /// 显示提示文字并收起键盘
    ///
    /// - Parameters:
    ///   - text: 显示的文字
    ///   - hideKeyboardView: 需要收起键盘的view
    public class func show(text: String, hideKeyboardView: UIView) {
        hideKeyboardView.endEditing(true)
        PromptView.share.textDidSet(text: text)
    }

    private override init(frame: CGRect) {
        super.init(frame: frame)
        loadView()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: >> 属性定义
    private static let share = PromptView()
    fileprivate var promptView = UIView()
    fileprivate var promptLabel = UILabel()
    fileprivate var timer: DispatchSourceTimer?

    // MARK: >> 常量定义
    fileprivate let padding: CGFloat = 80
    fileprivate let screenPadding: CGFloat = 80
    fileprivate let height: CGFloat = 40
    fileprivate let fontsize: CGFloat = 15
    fileprivate let backAlpha: CGFloat = 0.7
}

// MARK: >> 初始化
fileprivate extension PromptView {

    fileprivate func loadView() {

        loadSelf()
        loadPrompt()
    }

    private func loadSelf() {

        layer.cornerRadius = 5
        clipsToBounds = true
        isUserInteractionEnabled = false
        alpha = 0
    }

    private func loadPrompt() {
        promptView.alpha = backAlpha
        promptView.backgroundColor = UIColor.black
        promptLabel.textAlignment = .center
        promptLabel.font = UIFont.systemFont(ofSize: fontsize)
        promptLabel.textColor = UIColor.white

        addSubview(promptView)
        addSubview(promptLabel)
        UIApplication.shared.keyWindow?.addSubview(self)
    }
}

// MARK: >> 刷新控件
fileprivate extension PromptView {

    fileprivate func textDidSet(text: String) {
        reloadView(text: text)
        refreshTimer()
    }

    // MARK: >> 更新view
    private func reloadView(text: String) {
        promptLabel.text = text
        let sz = text.rect(fontsize: fontsize, size: CGSize(width: screenWidth - screenPadding, height: 100))
        UIView.animate(withDuration: 0.2) {
            self.alpha = 1
        }
        frame = CGRect(x: 0, y: screenHeight-140, width: sz.width + padding, height: height)
        center = CGPoint(x: screenWidth/2, y: center.y)
        promptLabel.frame = CGRect(x: 0, y: 0, width: sz.width + padding, height: height)
        promptView.frame = promptLabel.bounds
        UIApplication.shared.keyWindow?.bringSubview(toFront: self)
    }

    // MARK: >> 刷新timer
    private func refreshTimer() {
        timer?.cancel()
        timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
        timer?.scheduleRepeating(wallDeadline: .now(), interval: .seconds(1))
        var timerTime = 3
        timer?.setEventHandler {
            if timerTime<=0 {
                UIView.animate(withDuration: 0.2) {
                    self.alpha = 0
                }
                self.timer?.cancel()
            }
            timerTime-=1
        }
        timer?.resume()
    }

}

报错请添加这个extension

import UIKit
extension String {
    func rect(fontsize:CGFloat, size: CGSize) -> CGSize {
        let string = self as NSString
        return string.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: fontsize)], context: nil).size
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值