swift3.0 一个屏幕下方灰色提示条
来源:互联网 发布:js exec 编辑:程序博客网 时间:2024/04/28 05:43
import UIKitclass 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 UIKitextension 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
- swift3.0 一个屏幕下方灰色提示条
- Swift3.0 屏幕适配
- UIImage图片彩色转为灰色(swift2.0/swift3.0)
- swift3.0 自动消失提示框----吐司
- swift3.0提示框新用法
- 屏幕保护等待时间灰色
- LinearLayout布局中将一个控件放在屏幕的最下方-Android
- 去除网页下方滚动条
- admob广告条下方显示
- 一个可扩展的下方是导航条,上方是Fragment的UI框架
- 文本框加灰色提示
- 文本框中的灰色提示
- 用户名灰色提示
- Swift3.0获取APP版本号,提示用户升级
- swift3.0 实现自定义圆环进度提示效果
- Swift3.0获取APP版本号,提示用户升级
- iOS--一个简单的提示条代码
- 实现屏幕下方展示的TAB分页
- Groovy学习笔记
- 学习笔记--我的第一个爬虫项目
- 第八周拓展实践(6)都要学C
- NOI2.4 2011
- tomcat 中 web.xml server.xml context.xml tomcat-users.xml 的区分作用
- swift3.0 一个屏幕下方灰色提示条
- Linux系统移植实验之交叉工具链的安装
- 支持向量机通俗导论-我的学习笔记(一)
- 多少人忽略的细节--web.xml解析
- Android版本更新
- 一天一条Linux指令-swig
- jvm内存溢出分析
- HP-ASR机制介绍
- javaEE之-类反射直接封装前台传过来的参数