swift4.2 - 一个自定义view弹框

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<UITouch>, 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: - UI
extension 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
}
}
swift4.2 - 一个自定义view弹框的更多相关文章
- CodePush自定义更新弹框及下载进度条
CodePush 热更新之自定义更新弹框及下载进度 先来几张弹框效果图 非强制更新场景 image 强制更新场景 image 更新包下载进度效果 image 核心代码 这里的热更新Modal框,是封装 ...
- 使用xib封装一个自定义view的步骤
使用xib封装一个自定义view的步骤 1> 新建一个继承UIView的自定义view,假设类名叫做(MJAppView) 2> 新建一个MJAppView.xib文件来描述MJAppVi ...
- JavaScript实现自定义alert弹框
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAh0AAAFkCAYAAACEpYlzAAAfj0lEQVR4nO3dC5BddZ0n8F93pxOQCO
- 自定义 popWindow弹框 工具包
前言:因为Android 没有像IOS一样的ActionSheet,虽然在github上看到有一些类似ActionSheet的库,总觉得不好用,不如自己写一个弹框通用类,样式全部自已来多好. Step ...
- vue中超简单的方法实现点击一个按钮出现弹框,点击弹框外关闭弹框
效果图展示: View层 <template> <div> <div class="mask" v-if="showModal" ...
- 如何优雅的写一个Vue 的弹框
写Vue或者是react 都会遇见弹框的问题.也尝试了多种办法来写弹框,一直都不太满意,今天特地看了一下 Element UI 的源码,模仿着写了一个简易版. 大概有一下几个问题: 1.弹框的层级问题 ...
- 自定义alert弹框,title不显示域名
问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...
- 自定义alert弹框,title不显示域名(重写alert)
问题: 系统默认的alert弹框的title会默认显示网页域名 解决办法: (修改弹框样式) (function() { window.alert = function(name) { $(" ...
- IOS中使用.xib文件封装一个自定义View
1.新建一个继承UIView的自定义view,假设类名叫做 MyAppVew #import <UIKit/UIKit.h> @class MyApp; @interface MyAppV ...
随机推荐
- shell脚本运行springboot项目jar包
#!/bin/bash APP_NAME=AutomationGuide-0.0.1-SNAPSHOT.jar #使用说明,用来提示输入参数 usage() { echo "please e ...
- LeetCode题解:Flatten Binary Tree to Linked List:别人的递归!
总是在看完别人的代码之后,才发现自己的差距! 我的递归: 先把左侧扁平化,再把右侧扁平化. 然后找到左侧最后一个节点,把右侧移动过去. 然后把左侧整体移到右侧,左侧置为空. 很复杂吧! 如果节点很长的 ...
- layer.js 学习笔记
了解到一个很好用的弹框插件,名字叫layer.js 前几天要在网页中改写alert函数,自己写了一个模态框,虽然效果出来了,但是点击关闭后,无法返回当前页面.请教大神时,说有一个叫layer.js的东 ...
- 1047B_Cover Points
B. Cover Points time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 2312--1.3.4 Prime Cryptarithm 牛式
Description 下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式. * * * x * * ------- * * * * * * ------ ...
- pip安装提示PermissionError: [WinError 5]错误问题解决
操作环境 Python3.6 + Winodws7 问题现象 新安装python3.6版本后使用pip安装第三方模块失败,报错信息如下: C:\Users\linyfeng>pip inst ...
- 关于新版OPENWRT拔PPTP的619错或PPTPD无法连接问题笔记
旧版的openwrt要安装kmod-ipt-nethelper这个包 对于新版,如3.18或4.xx内核的ROM,要安装kmod-nf-nathelper-extra这个包
- JAVAWEB 一一 userweb2(升级,servlet版,jstl和el)
创建数据库和表 首先,创建一个web项目 然后引入jar包(jstl.jar和standard.jar是jstl和el包,在jsp页面中需要手动加 <%@ taglib uri="ht ...
- C#自制Web 服务器开发:mysql免安装版配置步骤详解分享
mysql免安装版配置步骤详解分享 1.准备工作 下载mysql的最新免安装版本mysql-noinstall-5.1.53-win32.zip,解压缩到相关目录,如:d:\ mysql-noinst ...
- zabbix 分布式zabbix_proxy
Zabbix是一个分布式监控系统,它可以以一个中心点.多个分节点的模式运行,使用Proxy能大大的降低Zabbix Server的压力,Zabbix Proxy可以运行在独立的服务器上 1)下载zab ...