博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
swift4.2 - 一个自定义view弹框
阅读量:6996 次
发布时间:2019-06-27

本文共 7246 字,大约阅读时间需要 24 分钟。

 

 

import UIKit/* *  注册协议view:没找到 UI原图,咱不实现 */class JYRegisterProtocolView: UIView {        /// 点击同意协议的回调    private var clickSelectedBtnBlock:((_ isSelected:Bool) -> Void)?    /// 点击完成按钮的回调    private var clickCompleteBtnBlock:(() -> Void)?    /// 单例属性    static let share : JYRegisterProtocolView = {        let view = JYRegisterProtocolView()        return view    }()        /// 背景view    private lazy var bgView : UIView = {        let v = JYUIModel.createView()        v.layer.cornerRadius = 25        v.layer.masksToBounds = true        return v    }()        /// 注册协议标题    private lazy var titleLabel : UILabel = JYUIModel.creatLabe(text: "注册协议", font: UIFont.systemFont(ofSize: 30), textColor: UIColor.red, textAlignment: NSTextAlignment.center)        /// 副标题标题    private lazy var subtitleLabel : UILabel = {        let lab = JYUIModel.creatLabe(text: "疯抢进10万元现金,等你来拿!\n\n参赛资格:剑琅联盟使用用户中:\n1.店铺老板 \n2.店铺发型师 \n3.店铺美甲师 \n活动有效期:2019.1.1~2019.3.31\n \n活动共五期 没齐活动奖励:\n第一名8000元(推荐员工奖励5000元现金,所在店铺老板奖励3000元现金)\n第二名 3000元 \n第三名 2000元", font: UIFont.systemFont(ofSize: 15), textColor: UIColor.red, textAlignment: NSTextAlignment.left)        lab.numberOfLines = 0        return lab    }()        /// 选择按钮    private lazy var selectedBtn : UIButton = {        let btn = JYUIModel.createBtn()        btn.addTarget(self, action: #selector(clickSelectedBtn), for: UIControl.Event.touchUpInside)        btn.backgroundColor = UIColor.orange                return  btn    }()        /// 同意文字标题    private lazy var agreeLabel : UILabel = JYUIModel.creatLabe(text: "我已认真阅读并同意", font: UIFont.systemFont(ofSize: 16), textColor: UIColor.red, textAlignment: NSTextAlignment.center)        /// 完成按钮    private lazy var completeBtn : UIButton = {        let btn = JYUIModel.createBtn()        btn.titleLabel?.font = UIFont.systemFont(ofSize: 30)        btn.layer.cornerRadius = 25        btn.layer.masksToBounds = true        btn.backgroundColor = UIColor.purple        btn.setTitle("完成注册", for: UIControl.State.normal)        btn.setTitleColor(UIColor.red, for: UIControl.State.normal)        btn.setTitle("完成注册", for: UIControl.State.selected)        btn.setTitleColor(UIColor.green, for: UIControl.State.selected)        btn.addTarget(self, action: #selector(clickcompleteBtn), for: UIControl.Event.touchUpInside)        return  btn    }()        override init(frame: CGRect) {        super.init(frame: frame)        configUI()    }        required init?(coder aDecoder: NSCoder) {        fatalError("init(coder:) has not been implemented")    }        //点击背景view 移除当前控件    override func touchesBegan(_ touches: Set
, with event: UIEvent?) { if let touch = touches.first { var point = touch.location(in: self) point = bgView.layer.convert(point, from: self.layer) if !bgView.layer.contains(point){ self.removeView() } } } /// 移除弹框(内部移除) private func removeView() { UIView.animate(withDuration: 0.5, animations: { [weak self] in self?.alpha = 0 }) { [weak self] (res) in self?.removeFromSuperview() } } /// 初始化欢迎弹框 convenience init(titleText:String? = nil, subtitle: String? = nil , agree:String? = nil, buttonText: String? = nil) { self.init() if agree != nil { self.titleLabel.text = titleText } if agree != nil { self.agreeLabel.text = agree } if subtitle != nil{ self.subtitleLabel.text = subtitle } if buttonText != nil{ self.completeBtn.setTitle(buttonText, for: .normal) } } /// 显示弹框 func showAlert(selectedBtnBlock:((_ isSelected:Bool) -> Void)? , completeBtnBlock:(() -> Void)?) { JYWindow.subviews.forEach { (v) in if v is JYRegisterProtocolView { return } } JYWindow.addSubview(self) self.clickSelectedBtnBlock = selectedBtnBlock self.clickCompleteBtnBlock = completeBtnBlock self.alpha = 0 self.isUserInteractionEnabled = false UIView.animate(withDuration: 0.5) { self.alpha = 1 self.isUserInteractionEnabled = true } }}// MARK: - 点击事件extension JYRegisterProtocolView{ /// 点击阅读 @objc private func clickSelectedBtn(){ selectedBtn.isSelected.toggle() if selectedBtn.isSelected == true { selectedBtn.backgroundColor = UIColor.black }else{ selectedBtn.backgroundColor = UIColor.orange } clickSelectedBtnBlock?(selectedBtn.isSelected) } /// 点击完成注册 @objc private func clickcompleteBtn(){ if selectedBtn.isSelected == false{ DDLOG(message: "给个提示") }else{ clickCompleteBtnBlock?() self.removeView() } }}// MARK: - UIextension JYRegisterProtocolView{ func configUI(){ self.backgroundColor = "000000".jy.getColor().withAlphaComponent(0.3) self.frame = UIScreen.main.bounds self.layoutIfNeeded() configBgView() let vd : [String:UIView] = ["bgView":bgView] addSubview(bgView) self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-24-[bgView]-24-|", options: [], metrics: nil, views: vd)) self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[bgView]", options: [], metrics: nil, views: vd)) bgView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true bgView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true addSubview(bgView) } /// 背景view的UI func configBgView(){ let vd : [String : UIView] = ["titleLabel":titleLabel, "subtitleLabel":subtitleLabel, "selectedBtn":selectedBtn, "agreeLabel":agreeLabel, "completeBtn":completeBtn] bgView.addSubview(titleLabel) bgView.addSubview(subtitleLabel) bgView.addSubview(selectedBtn) bgView.addSubview(agreeLabel) bgView.addSubview(completeBtn) bgView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|[titleLabel]|", options: [], metrics: nil, views: vd)) bgView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-15-[subtitleLabel]-15-|", options: [], metrics: nil, views: vd)) bgView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "[selectedBtn(20)]-10-[agreeLabel]", options: [.alignAllCenterY], metrics: nil, views: vd)) bgView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|-50-[completeBtn]-50-|", options: [], metrics: nil, views: vd)) bgView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-15-[titleLabel]-10-[subtitleLabel]-30-[agreeLabel]-20-[completeBtn(50)]-20-|", options: [.alignAllCenterX], metrics: nil, views: vd)) selectedBtn.heightAnchor.constraint(equalToConstant: 20).isActive = true }}

  

转载于:https://www.cnblogs.com/qingzZ/p/10315171.html

你可能感兴趣的文章
Flex4中动态生成RadioButton,绑定数据源
查看>>
开源CMS大PK:WordPress vs Drupal vs Joomla ,谁更强大更好用
查看>>
HDU-1004 Let the Balloon Rise STL map
查看>>
单片机低功耗设计杂谈
查看>>
Ubuntu上怎么安装Eclipse Android 开发环境
查看>>
转载:解答Google的一道面试题
查看>>
在 ASP.NET 页面中使用 TreeView 控件
查看>>
走进单元测试二:测试需要从哪些方面着手
查看>>
著名编程语录
查看>>
后台修改,订单锁定
查看>>
CentOS 5.4 安裝 boost 1.4.1 筆記 - 杨毅的电子笔记
查看>>
工欲善其事,必先利其器:分享一套Code Smith 搭建N层架构模板
查看>>
几何变换详解
查看>>
Cocoa设计模式之KVC
查看>>
POJ 2063 Investment(完全背包)
查看>>
netmon工作原理
查看>>
通过python的import hooks实现像引用代码一样使用配置文件
查看>>
Google Code在Windows下面设置.netrc
查看>>
Oracle笔记(4):一个存储过程编写及C#调用
查看>>
在MyEclipse中配置Tomcat服务器
查看>>