1. //弹窗视图
  2. class PopView : UIView {
  3. var selectButtonCallBack:((_ title:String)-> Void)?
  4.  
  5. var contenView:UIView?
  6. {
  7. didSet{
  8. setUpContent()
  9. }
  10. }
  11.  
  12. override init(frame: CGRect) {
  13. super.init(frame: frame)
  14. }
  15.  
  16. required init?(coder aDecoder: NSCoder) {
  17. fatalError("init(coder:) has not been implemented")
  18. }
  19.  
  20. func setUpContent(){
  21.  
  22. if self.contenView != nil {
  23. self.contenView?.frame.origin.y = UIScreen.main.bounds.size.height -
  24. self.addSubview(self.contenView!)
  25. }
  26. self.backgroundColor = newColorWithAlpha(, , , 0.4)
  27. self.isUserInteractionEnabled = true
  28. self.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(dismissView)))
  29. //以下为添加内容,可根据需要删除以下部分
  30. sudokuConstraints()
  31. }
  32.  
  33. @objc func dismissView(){
  34. UIView.animate(withDuration: 0.3, animations: {
  35. self.alpha =
  36. }) { (true) in
  37. self.removeFromSuperview()
  38. self.contenView?.removeFromSuperview()
  39. }
  40. }
  41.  
  42. func showInWindow(){
  43. UIApplication.shared.keyWindow?.addSubview(self)
  44. UIView.animate(withDuration: 0.3, animations: {
  45. self.alpha = 1.0
  46. self.contenView?.frame.origin.y = UIScreen.main.bounds.size.height -
  47. }, completion: nil)
  48. }
  49.  
  50. //MARK: - 布局
  51. func sudokuConstraints() -> Void {
  52. let titleArr = ["京","沪","浙","苏","粤","鲁","晋","翼",
  53. "豫","川","渝","辽","吉","黑","皖","鄂",
  54. "湘","赣","闽","陕","甘","宁","蒙","津",
  55. "贵","云","桂","琼","青","新","藏"]
  56.  
  57. for (index,value) in titleArr.enumerated() {
  58. let button = createButton(title: value)
  59. let margin = (UIScreen.main.bounds.size.width - * )/( + )
  60. let col = CGFloat(index % Int())
  61. let row = CGFloat(index / Int())
  62. let viewX = margin + col * ( + margin)
  63. let viewY = + row * ( + )
  64.  
  65. button.frame = CGRect(x: viewX, y: viewY, width: , height: )
  66. self.contenView!.addSubview(button)
  67. }
  68. }
  69.  
  70. func createButton(title:String) -> UIButton {
  71. let button = UIButton()
  72. button.setTitle(title, for: .normal)
  73. button.setTitleColor(newColor(, , ), for: .normal)
  74. button.backgroundColor = .white
  75. button.layer.masksToBounds = true
  76. button.layer.cornerRadius = 5.0
  77.  
  78. button.addTarget(self, action: #selector(buttonClickAction(button:)), for: .touchUpInside)
  79. return button
  80. }
  81.  
  82. @objc func buttonClickAction(button:UIButton) -> Void {
  83. if self.selectButtonCallBack != nil {
  84. self.selectButtonCallBack!(button.titleLabel?.text ?? "粤")
  85. }
  86. }
  87. }

使用:

  1. let popview = PopView.init(frame:UIScreen.main.bounds)
  2. popview.contenView = UIView.init(frame: CGRect.init(x: , y: UIScreen.main.bounds.size.height - , width: UIScreen.main.bounds.size.width, height: ))
  3. popview.contenView?.backgroundColor = newColor(, , )
  4. popview.selectButtonCallBack = {
  5. (title:String) -> Void in
  6. self.righAbbreviationButton.setTitle(title, for: .normal)
  7. popview.dismissView()
  8. }
  9. popview.showInWindow()

效果图:

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

  1. 编写高质量代码改善C#程序的157个建议——建议23:避免将List<T>作为自定义集合类的基类

    建议23:避免将List<T>作为自定义集合类的基类 如果要实现一个自定义的集合类,不应该以一个FCL集合类为基类,反而应扩展相应的泛型接口.FCL结合类应该以组合的形式包含至自定义的集合 ...

  2. 【转】C++ 虚函数&纯虚函数&抽象类&接口&虚基类

    1. 动态多态 在面向对象语言中,接口的多种不同实现方式即为多态.多态是指,用父类的指针指向子类的实例(对象),然后通过父类的指针调用实际子类的成员函数. 多态性就是允许将子类类型的指针赋值给父类类型 ...

  3. C++ 虚函数&纯虚函数&抽象类&接口&虚基类(转)

    http://www.cnblogs.com/fly1988happy/archive/2012/09/25/2701237.html 1. 多态 在面向对象语言中,接口的多种不同实现方式即为多态.多 ...

  4. 关于TagHelper的那些事情——如何自定义TagHelper(TagHelper基类)

    写在开头 前面介绍了TagHelper的基本概念和内嵌的TagHelpers,想必大家对TagHelper都有一定的了解.TagHelper看上去有点像WebControl,但它不同于WebContr ...

  5. qt 5 小练习 纯代码制作自定义按钮

    大家都知道QT设计师中直接拖动的按钮是长方形带有圆角的图案,那我们如何来设置自定义按钮呢 要设计一个按钮,我们必须要知道按钮有什么属性,首先,按钮必须有一个位置 第二,按钮必须有一个名称.还有当我们点 ...

  6. 25.自定义mixin和基类

    很多时候业务需求并不是几个简单的mixin就可以满足,需要我们自定义mixin # get_object源码中字段查询源代码 filter_kwargs = {self.lookup_field: s ...

  7. C++ - 虚基类、虚函数与纯虚函数

    虚基类       在说明其作用前先看一段代码 class A{public:    int iValue;}; class B:public A{public:    void bPrintf(){ ...

  8. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  9. 编写高质量代码改善C#程序的157个建议[勿选List<T>做基类、迭代器是只读的、慎用集合可写属性]

    前言 本文已更新至http://www.cnblogs.com/aehyok/p/3624579.html .本文主要学习记录以下内容: 建议23.避免将List<T>作为自定义集合类的基 ...

随机推荐

  1. DocumentFragment 不支持 innerHTML

    在需要多次使用 innerHTML 的地方,一般是推荐用 DocumentFragment 来缓存,最后一次性插入 body,从而减少浏览器的渲染,提高性能,不过最近也发现一个 bug: Docume ...

  2. 3 differences between Savepoints and Checkpoints in Apache Flink

    https://mp.weixin.qq.com/s/nQOxsZUZSiPi7Sx40mgwsA 20181104 3 differences between Savepoints and Chec ...

  3. php和jsCOOKIE实现前端交互

    w如何精简? <script> document.cookie = 'wjs_cookie=' + 'amz_reviews'; function w(id) { document.coo ...

  4. angular(二)

    angularjs第二章 自定义指令 scope 控制器 AngularJS控制器控制AngularJS应用程序的数据,是常规的JavaScript对象. ng-controller指令就是用来定义应 ...

  5. Sqoop简介及使用

    一.Sqoop概述 1)官网 http://sqoop.apache.org/ 2)场景 传统型缺点,分布式存储.把传统型数据库数据迁移. Apache Sqoop(TM)是一种用于在Apache H ...

  6. Buy the souvenirs---hdu2126(01背包输出方案数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2126 有n个物品每个物品的价格是v[i],现在有m元钱问最多买多少种物品,并求出有多少种选择方法: 如 ...

  7. vue中获取客户端IP地址(不需要额外引入三方文件)

    之前看了几种方法 ,都是引入腾讯,新浪,搜狐等的三方js文件来查询IP地址,但是我自己测试的时候IP地址不准确,所以就找了找,发现了这个方法,准确的获取到了IP地址和cmd的ipconfig获取到的I ...

  8. django组件之ContentType

    ContentTyep组件: 帮助我们关联所有数据库的表 帮助我们反向查询关联数据表中的所有策略信息 GenericForeignkey(帮助我们快速插入数据) GenericRelation(用于反 ...

  9. C#建WindowForm调用R可视化

    众所周知R软件功能非常强大,可以很好的进行各类统计,并能输出图形.下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上的方法,文中介绍的很详细,需要的朋友可以参考下. ...

  10. Navicat连接mysql8出现1251错误

    我的博客:www.yuehan.online   因为加密方式的问题,在使用mysql8.0的时候需要修改加密规则才能连接navicat. 打开cmd,输入以下命令: ALTER USER 'root ...