Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【纯代码】Swift - 自定义底部弹窗基类(可根据需要自行扩展内容)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_772_1@

概述

//弹窗视图 class PopView : UIView { var SELEctButtonCallBACk:((_ title:string)-> Void)? var contenView:UIView? { didSet{ setUpContent() } }
//弹窗视图
class PopView : UIView {
    var SELEctButtonCallBACk:((_ title:string)-> Void)?
    
    var contenView:UIView?
    {
        didSet{
            setUpContent()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: framE)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpContent(){
        
        if self.contenView != nil {
            self.contenView?.frame.origin.y = UIScreen.main.bounds.size.height - 191
            self.addSubview(self.contenView!)
        }
        self.BACkgroundColor = newColorWithAlpha(0,0,0.4)
        self.isUserInteractionEnabled = true
        self.addGestureRecognizer(UITapGestureRecognizer.init(target: self,action: #SELEctor(dismissview)))
        //以下为添加内容,可根据需要删除以下部分
        sudokuConsTraints()
    }
    
    @objc func dismissview(){
        UIView.animate(withDuration: 0.3,animations: {
            self.alpha = 0
        }) { (true) in
            self.removeFromSuperview()
            self.contenView?.removeFromSuperview()
        }
    }
    
    func showInWindow(){
        UIApplication.shared.keyWindow?.addSubview(self)
        UIView.animate(withDuration: 0.3,animations: {
            self.alpha = 1.0
            self.contenView?.frame.origin.y = UIScreen.main.bounds.size.height - 191
        },completion: nil)
    }
    
    //@H_23_20@mARK: - 布局
    func sudokuConsTraints() -> Void {
        let titleArr = ["","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""]
        
        for (index,value) in titleArr.enumerated() {
            let button = createButton(title: value)
            let margin = (UIScreen.main.bounds.size.width - 8 * 39)/(8 + 1)
            let col  = CGFloat(index % Int(8))
            let row  = CGFloat(index / Int(8))
            let viewX = margin +  col * (39 + margin)
            let viewY = 7 + row * (39 + 7)
            
            button.frame = CGRect(x: viewX,y: viewY,width: 39,height: 39)
            self.contenView!.addSubview(button)
        }
    }
    
    func createButton(title:string) -> UIButton {
        let button = UIButton()
        button.settitle(title,for: .normal)
        button.settitleColor(newColor(0,0),for: .normal)
        button.BACkgroundColor = .white
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 5.0
        
        button.addTarget(self,action: #SELEctor(buttonClickAction(button:)),for: .touchUpInsidE)
        return button
    }
    
    @objc func buttonClickAction(button:UIButton) -> Void {
        if self.SELEctButtonCallBACk != nil {
            self.SELEctButtonCallBACk!(button.titleLabel?.text ?? "")
        }
    }
}

使用:

let popview = PopView.init(frame:UIScreen.main.bounds)
        popview.contenView = UIView.init(frame: CGRect.init(x: 0,y: UIScreen.main.bounds.size.height - 191,width: UIScreen.main.bounds.size.width,height:191 ))
popview.contenView?.BACkgroundColor = newColor(206,206,206)
popview.SELEctButtonCallBACk = {
    (title:string) -> Void in
    self.righAbbreviationButton.settitle(title,for: .normal)
    popview.dismissview()
}
popview.showInWindow()

效果图:

【纯代码】Swift - 自定义底部弹窗基类(可根据需要自行扩展内容)

大佬总结

以上是大佬教程为你收集整理的【纯代码】Swift - 自定义底部弹窗基类(可根据需要自行扩展内容)全部内容,希望文章能够帮你解决【纯代码】Swift - 自定义底部弹窗基类(可根据需要自行扩展内容)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。