日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/21 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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}()/// 背景viewprivate lazy var bgView : UIView = {let v = JYUIModel.createView()v.layer.cornerRadius = 25v.layer.masksToBounds = truereturn 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 = 0return lab}()/// 選擇按鈕private lazy var selectedBtn : UIButton = {let btn = JYUIModel.createBtn()btn.addTarget(self, action: #selector(clickSelectedBtn), for: UIControl.Event.touchUpInside)btn.backgroundColor = UIColor.orangereturn 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 = 25btn.layer.masksToBounds = truebtn.backgroundColor = UIColor.purplebtn.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] inself?.alpha = 0}) { [weak self] (res) inself?.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) inif v is JYRegisterProtocolView {return}}JYWindow.addSubview(self)self.clickSelectedBtnBlock = selectedBtnBlockself.clickCompleteBtnBlock = completeBtnBlockself.alpha = 0self.isUserInteractionEnabled = falseUIView.animate(withDuration: 0.5) {self.alpha = 1self.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.boundsself.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 = truebgView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = trueaddSubview(bgView)}/// 背景view的UIfunc 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

總結

以上是生活随笔為你收集整理的swift4.2 - 一个自定义view弹框的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。