swift protocal实现提示更新弹框

样式如下:

提示更新样式

本着学习的态度(其实是我不会...),写了这么个东西...

需求是,检测后端返回接口,给个更新提示框,框的样式自定义,本着不影响主题代码,降低耦合性的态度,利用协议实现弹出层

步骤如下:
先定义一个弹出层

enum UpdateType: String {
    case remind
    case update
}

class UpdateView: UIView {
    
    var isForcedToUpdate: Bool = false
    var serverVersionStr: String? = ""
    var noticeStr: String = ""
    
    var updateType: UpdateType = .remind {
        didSet {
            setupUI()
        }
    }
    
    var updateString: String? = ""
    
    var backView = UIView()
    
    let backImageView: UIImageView = UIImageView()
    let titleLabel: UILabel = UILabel()
    
    let serverVersionLabel = UILabel()
    var noticeLabel = UILabel()
    
    lazy var cancelButton = UIButton()
    lazy var updateButton = UIButton()
    
    var cancelHandler: (()->())?
    var updateHandler: (()->())?
    
    init(type: UpdateType) {
        updateType = type
        
        super.init(frame: UIScreen.main.bounds)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        
        addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.width.equalTo(280)
            make.height.equalTo(320)
            make.center.equalToSuperview()
        }
        backView.layer.cornerRadius = 12
        backView.layer.masksToBounds = true
        
        backView.addSubview(backImageView)
        backImageView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        backImageView.image = #imageLiteral(resourceName: "imgToastToUpdate")
        
        backView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(116)
            make.centerX.equalToSuperview()
        }
        titleLabel.text = "رسالة التحديث"
        titleLabel.textColor = UIColor.black
        
        backView.addSubview(serverVersionLabel)
        serverVersionLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
        }
        serverVersionLabel.text = serverVersionStr
        serverVersionLabel.textColor = UIColor.black
        serverVersionLabel.backgroundColor = UIColor.red
        
        backView.addSubview(noticeLabel)
        noticeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(serverVersionLabel.snp.centerX)
            make.width.equalTo(215)
            make.height.equalTo(80)
            make.top.equalTo(serverVersionLabel.snp.bottom)
        }
        do {
            let attrStr = try NSAttributedString(data: noticeStr.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
            noticeLabel.attributedText = attrStr
        } catch let error as NSError {
            print(error)
            noticeLabel.text = "提示更新"
        }
        noticeLabel.backgroundColor = UIColor.cyan
        
        switch updateType {
        case .remind:
            backView.addSubview(cancelButton)
            cancelButton.snp.makeConstraints({ (make) in
                make.left.equalTo(19)
                make.bottom.equalToSuperview().offset(-16)
                make.top.equalTo(noticeLabel.snp.bottom).offset(16)
                make.width.equalTo(112)
            })
            cancelButton.titleLabel?.text = "以后提示"
            cancelButton.backgroundColor = UIColor.gray
            cancelButton.addTarget(self, action: #selector(cancelButtonAction), for: .touchUpInside)
            
            backView.addSubview(updateButton)
            updateButton.snp.makeConstraints({ (make) in
                make.right.equalTo(-19)
                make.bottom.equalToSuperview().offset(-16)
                make.top.equalTo(noticeLabel.snp.bottom).offset(16)
                make.width.equalTo(112)
            })
        case .update:
            backView.addSubview(updateButton)
            updateButton.snp.makeConstraints({ (make) in
                make.centerX.equalToSuperview()
                make.left.equalTo(19)
                make.right.equalTo(-19)
                make.bottom.equalToSuperview().offset(-16)
                make.top.equalTo(noticeLabel.snp.bottom).offset(16)
            })
        }
        updateButton.backgroundColor = UIColor.blue
        updateButton.titleLabel?.text = "立刻更新"
        updateButton.addTarget(self, action: #selector(updateButtonAction), for: .touchUpInside)
        
        self.layoutIfNeeded()
    }
    
    @objc private func updateButtonAction() {
        updateHandler?()
    }
    
    @objc private func cancelButtonAction() {
        cancelHandler?()
    }
}

然后定义一个围绕弹出层的协议


import UIKit
import SnapKit

protocol UpdateMasked {
    
}

extension UpdateMasked {
    
    func addUpdateMask(updateType: UpdateType) {
        
        let vw = UpdateView.init(type: updateType)
        vw.backgroundColor = UIColor.gray.withAlphaComponent(0.4)
        vw.cancelHandler = {() in
            vw.removeFromSuperview()
        }
        vw.updateHandler = {() in
            let url = URL(string: "https://itunes.apple.com/us/app/nawyn/id" + "xxxxx") // "xxxxx"是更新地址
            UIApplication.shared.openURL(url!)
        }
        UIApplication.shared.keyWindow?.addSubview(vw)
    }
}

最后再要的地方遵循协议,调用方法即可
想在哪调用都可以,我的应用里是封装了统一管理类,遵循NSObject的

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        addUpdateMask(updateType: .remind)

    }

完事儿了,就这些,写的low...,慢慢改进

Demo地址
https://github.com/XHJiang/remindUpdateView

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,302评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,563评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,433评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,628评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,467评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,354评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,777评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,419评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,725评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,768评论 2 314
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,543评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,387评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,794评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,032评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,305评论 1 252
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,741评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,946评论 2 336

推荐阅读更多精彩内容