美文网首页iOS Developer
swift protocal实现提示更新弹框

swift protocal实现提示更新弹框

作者: 蒋昉霖 | 来源:发表于2017-05-09 16:14 被阅读92次

样式如下:

提示更新样式

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

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

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

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

相关文章

网友评论

    本文标题:swift protocal实现提示更新弹框

    本文链接:https://www.haomeiwen.com/subject/aoxitxtx.html