• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
//弹窗视图
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()

效果图:

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
IOSUIWebView与js的简单交互swift3版发布时间:2022-07-13
下一篇:
swift隐藏tabbar最简单的操作发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap