引言
? ? ? ? ? ? 在App的開發(fā)中搔体,某些功能執(zhí)行的過程中可能會出現(xiàn)各種各樣的問題靡狞。為了用戶更好的體驗,我們需要讓用戶知道該功能發(fā)生了什么拘央,原因是什么涂屁;而不是在哪傻傻的等著。為了能夠讓用戶看到這個提醒灰伟,就設(shè)計出一個能在視圖上展示的圖框拆又,通過這個圖框來說明出現(xiàn)的問題。這個圖框我們稱之為 RemindBox 或者 Alert 等袱箱。
RemindBox的創(chuàng)建使用到的知識點
- 類的擴展 extension 的使用。
extension UIView {
code.....
}
- 枚舉的創(chuàng)建
/*!
設(shè)置是自動關(guān)閉還是手動關(guān)閉
*/
public enum RemindBoxDeleteType:Int {
case AutomaticityDeleteType
case HandMovementDeleteType
}
- 對象類型的判斷 is
if item is String {
code....
}
if item is UILable {
code....
}
- 計算文本框的實際尺寸
/*!
計算對象的真實尺寸
@lable: 要計算的傳入對象
*/
private func calculateTheTrueSize(lable:UILabel? ,maxSize:CGSize) -> CGSize {
// 判斷出入?yún)?shù)是否存在
if lable != nil {
// 設(shè)置段落規(guī)則
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.lineBreakMode = lable!.lineBreakMode
// 設(shè)置計算屬性
let attributes = [NSAttributedStringKey.font:lable!.font , NSAttributedStringKey.foregroundColor:lable!.textColor, NSAttributedStringKey.paragraphStyle:paragraphStyle] as [NSAttributedStringKey : Any]
// 計算真實尺寸
let tempString = lable!.text! as NSString
return tempString.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size
}
return CGSize.zero
}
- Runtime的頭文件引入
import ObjectiveC.runtime
- 使用運行時給某個對象設(shè)置標(biāo)簽
1义矛、 設(shè)置標(biāo)簽
// 設(shè)置RemindBox標(biāo)記
objc_setAssociatedObject(self, &RemindMark, remindView, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
2发笔、 通過標(biāo)簽獲取對象
let remindBox = objc_getAssociatedObject(self, &RemindMark) as! UIView
remindBox.removeFromSuperview()
- 定時器的使用
// 添加定時器
Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: false, block: { (timer) in
// 停止定時器
timer.invalidate()
// 清除RemindBox
self.hiddenRemindBox()
})
ZSJRemindManager.swift 類的介紹
1、介紹 ZSJRemindManager
? ? ? ? ? ? ZSJRemindManager 是我根據(jù)目前App的提醒框而整理和封裝的一個 Swift 版本的RemindBox凉翻。 ZSJRemindManager 包含多種樣式了讨,還可以自定義樣式和顯示的位置等。
2制轰、 ZSJRemindManager的部分核心代碼展示
1前计、 對象的創(chuàng)建
/*!
要創(chuàng)建一個Remind框
@message: 要顯示的信息
@title: RemindBox 的標(biāo)題
@image: RemindBox 的要展示的圖片
*/
private func createRemind(message:String? ,title:String? , image:UIImage? , isActivityIndicatorView:Bool) -> UIView? {
// 判斷傳入?yún)?shù)的是否有值
if message == nil && title == nil && image == nil && isActivityIndicatorView == false {
return nil
}
// 創(chuàng)建 msg/title/image對象
var msgLable:UILabel?
var titleLable:UILabel?
var imageView:UIImageView?
var activityIndicatorView:UIActivityIndicatorView?
// 創(chuàng)建一個載體View
let remindView = UIView.init()
// 實現(xiàn)子控件相對父控件的布局
remindView.autoresizingMask = [.flexibleWidth , .flexibleHeight]
// RemindBox的元切角
remindView.layer.masksToBounds = true
remindView.layer.cornerRadius = RemindBoxCornerRadius
// RemaindBox 的背景色
remindView.backgroundColor = UIColor.init(white: 0.5, alpha: 0.8)
// TODO: 檢測是否有標(biāo)題
if title != nil {
// 創(chuàng)建標(biāo)題對象
titleLable = UILabel.init()
// 設(shè)置標(biāo)記
titleLable!.tag = 1990516
// 設(shè)置自動換行
titleLable?.numberOfLines = 0
// 設(shè)置顯示的字體大小
titleLable?.font = UIFont.boldSystemFont(ofSize: TitleFont)
// 設(shè)置標(biāo)題文字顯示的樣式
titleLable?.lineBreakMode = .byWordWrapping
titleLable?.textAlignment = .center
// 設(shè)置標(biāo)題的文字
titleLable?.text = title
// 標(biāo)題的渲染
remindView.addSubview(titleLable!)
}
// TODO: 檢測是否有圖像
if image != nil {
// 創(chuàng)建圖像對象
imageView = UIImageView.init()
// 設(shè)置圖像
imageView!.image = image
// 渲染視圖之上
remindView.addSubview(imageView!)
}
// TODO: 是否含有活動指示器
if isActivityIndicatorView {
activityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .whiteLarge)
activityIndicatorView!.startAnimating()
remindView.addSubview(activityIndicatorView!)
}
// TODO:判斷消息是否存在
if message != nil {
// 創(chuàng)建消息對象
msgLable = UILabel.init()
// 設(shè)置標(biāo)記
msgLable!.tag = 1989516
// 設(shè)置可折疊行數(shù)
msgLable!.numberOfLines = 0
// 設(shè)置標(biāo)題文字顯示的樣式
msgLable!.lineBreakMode = .byWordWrapping
msgLable!.textAlignment = .center
// 設(shè)置消息字體的大小
msgLable!.font = UIFont.systemFont(ofSize: MessgaeFont)
// 設(shè)置消息的內(nèi)容
msgLable!.text = message
// 消息的渲染
remindView.addSubview(msgLable!)
}
// 設(shè)置位置
self.remindBoxLayoutBox(tager: remindView)
// 設(shè)置RemindBox標(biāo)記
objc_setAssociatedObject(self, &RemindMark, remindView, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
// 返回對象
return remindView
}
2、檢測視圖上是否有RemindBox的顯示和清除RemindBox
/*!
清除RemindBox
*/
func hiddenRemindBox() -> Void {
// 獲取視圖上的RemindBox
let remindBox = objc_getAssociatedObject(self, &RemindMark) as! UIView
remindBox.removeFromSuperview()
}
/*!
檢測視圖上是否存在RemindBox
@tager: 要檢測視圖
*/
private func isDetectRemindInRootView(tager:UIView) -> Void {
let remind = objc_getAssociatedObject(tager, &RemindMark)
if remind != nil {
return
}
}
3垃杖、自動計算文本的寬高
/*!
計算對象的真實尺寸
@lable: 要計算的傳入對象
*/
private func calculateTheTrueSize(lable:UILabel? ,maxSize:CGSize) -> CGSize {
// 判斷出入?yún)?shù)是否存在
if lable != nil {
// 設(shè)置段落規(guī)則
let paragraphStyle = NSMutableParagraphStyle.init()
paragraphStyle.lineBreakMode = lable!.lineBreakMode
// 設(shè)置計算屬性
let attributes = [NSAttributedStringKey.font:lable!.font , NSAttributedStringKey.foregroundColor:lable!.textColor, NSAttributedStringKey.paragraphStyle:paragraphStyle] as [NSAttributedStringKey : Any]
// 計算真實尺寸
let tempString = lable!.text! as NSString
return tempString.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: attributes, context: nil).size
}
return CGSize.zero
}
3男杈、 ZSJRemindManager的測試使用
1、測試一
// 只顯示消息
self.view.showRemindBox(message: "你的錢掉了调俘,請注意拾取伶棒,謝謝M荨!肤无!")
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.33.29.png
2先蒋、測試二
// 消息與標(biāo)題
self.view.showRemindBox(title: "溫馨提示", message: "你的錢掉了,請注意拾取宛渐,謝謝>貉!窥翩!", image: nil, position: nil)
效果展示:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.41.14.png
3 业岁、測試三
// 圖像
self.view.showRemindBox(image: UIImage.init(named: "Image"))
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.44.57.png
4、測試四
// 圖像加信息
self.view.showRemindBox(title: nil, message: "你非常棒,繼續(xù)努力", image: UIImage.init(named: "Image"), position: nil)
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.51.22.png
5鳍烁、測試五
// 圖像叨襟,標(biāo)題,消息
self.view.showRemindBox(title: "溫馨提示", message: "你是最棒的,繼續(xù)努力哦幔荒!", image: UIImage.init(named: "Image"), position: nil)
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.53.48.png
6糊闽、測試六
// 圖像,標(biāo)題爹梁,消息右犹,位置
self.view.showRemindBox(title: "溫馨提示", message: "你是最棒的,繼續(xù)努力哦!", image: UIImage.init(named: "Image"), position: 0.3)
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.57.52.png
7姚垃、測試七
// 帶文字的活動指示器
self.view.showActivityIndicatorRemindBox(message: "正在加載中念链。。积糯。")
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 15.59.38.png
8掂墓、測試八
// 只是活動指示器
self.view.showActivityIndicatorRemindBox()
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 16.01.36.png
9、測試九
// 帶文字的活動指示器
self.view.showActivityIndicatorRemindBox(message: "正在加載中,請稍后看成。君编。。", position:0.3)
效果如下:
Simulator Screen Shot - iPhone 8 - 2017-11-24 at 16.03.35.png
ZSJRemindManager 的下載方法
聯(lián)系間主(m:18801210281 , q:1542100658)
發(fā)郵箱件 (zhoushuangjian511@163.com)