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

//弹窗视图
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, 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)
    }
    
    //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, 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()

效果图:

 

转载于:https://www.cnblogs.com/xjf125/p/10613363.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt窗体自定义标题栏基类是一种用于创建自定义窗体标题栏的基类。在Qt中,默认情况下,窗体的标题栏是由操作系统提供的,并且通常具有标准的布局和样式。但是,有时我们可能希望根据自己的需求,自定义窗体标题栏的外观和行为。 Qt窗体自定义标题栏基类可以通过重写一些方法和信号槽来实现自定义标题栏。使用这种基类,我们可以实现以下功能: 1. 自定义标题栏的颜色和样式:我们可以通过重写paintEvent方法来绘制自定义的标题栏,包括设置背景颜色、绘制按钮、标题等。 2. 实现窗口移动:通常情况下,窗口可以通过鼠标左键点击标题栏并拖动来移动。我们可以使用mousePressEvent、mouseMoveEvent和mouseReleaseEvent重写这些事件来实现窗口移动的功能。 3. 添加自定义按钮:我们可以在自定义标题栏中添加自定义的按钮,如最小化、最大化和关闭按钮。这些按钮可以连接到相应的槽函数来实现相应的窗口操作。 4. 响应标题栏双击事件:通常情况下,双击标题栏会触发窗口的最大化/还原操作。我们可以通过重写mouseDoubleClickEvent方法来实现此功能。 使用Qt窗体自定义标题栏基类,我们可以根据自己的需求轻松创建具有自定义外观和行为的窗体标题栏。这为我们提供了更大的自由度来设计窗口界面,并为用户提供更好的使用体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值