ZonePickerView.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // ZonePickerView.swift
  3. // ZoneBarManager
  4. //
  5. // Created by 刘振兴 on 2017/3/15.
  6. // Copyright © 2017年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. public func ZoneRect(_ x:Double,_ y:Double,_ w:Double,_ h:Double) -> CGRect{
  10. return CGRect(x: Double(UIScreen.main.bounds.width) * x, y: Double(UIScreen.main.bounds.height) * y, width: Double(UIScreen.main.bounds.width) * w, height: Double(UIScreen.main.bounds.height) * h)
  11. }
  12. public func ZoneColorAlpha(_ r:CGFloat,_ g:CGFloat,_ b:CGFloat,_ a:CGFloat) -> UIColor {
  13. return UIColor(red: r / 255.0, green: g/255.0, blue: b/255.0, alpha: a)
  14. }
  15. class ZonePickerView: UIView,UITableViewDataSource,UITableViewDelegate,ZoneFileListCellDelegate{
  16. var title:String = "附件列表"
  17. var pickViewHeight = 350
  18. var topView:UIView!
  19. var doneBtn:UIButton!
  20. var pickerView:UITableView!
  21. var result:String!
  22. var models:[ZonePickerModel] = []
  23. var selectModel:ZonePickerModel!
  24. override init(frame: CGRect) {
  25. super.init(frame: ZoneRect(0, 0, 1, 917/617))
  26. }
  27. required init?(coder aDecoder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. override func layoutSubviews() {
  31. super.layoutSubviews()
  32. self.backgroundColor = ZoneColorAlpha(0, 0, 0, 0.4)
  33. //
  34. self.topView = UIView(frame: ZoneRect(0, 667/667, 1, Double(pickViewHeight)/667))
  35. self.topView.backgroundColor = UIColor.white
  36. self.addSubview(topView)
  37. self.doneBtn = UIButton(type: .custom)
  38. self.doneBtn.setImage(#imageLiteral(resourceName: "icon_delete2_por"), for: .normal)
  39. // self.doneBtn.setTitle("Close", for: .normal)
  40. // self.doneBtn.setTitleColor(UIColor.gray, for: .normal)
  41. self.doneBtn.frame = ZoneRect(330/375, 5/667, 50/375, 40/667)
  42. self.doneBtn.addTarget(self, action: #selector(quit), for: .touchUpInside)
  43. self.topView.addSubview(doneBtn)
  44. let titleLB = UILabel(frame: ZoneRect(10/375, 0, 175/375, 50/667))
  45. titleLB.backgroundColor = UIColor.clear
  46. titleLB.textAlignment = .left
  47. //titleLB.text = title
  48. titleLB.attributedText = NSAttributedString(string: title, attributes: [NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 16.0)!,NSAttributedString.Key.foregroundColor:ZoneColorAlpha(102, 102, 102, 1)])
  49. //titleLB.font = UIFont(name: "PingFangSC-Regular", size: 16.0)!
  50. self.topView.addSubview(titleLB)
  51. self.pickerView = UITableView(frame: ZoneRect(0,50/667,1,300/667), style: .plain)
  52. self.pickerView.tableFooterView = UIView()
  53. self.pickerView.register(UINib(nibName: "ZoneFileListCell", bundle: Bundle.main), forCellReuseIdentifier: "ZonePickerCell")
  54. self.pickerView.dataSource = self
  55. self.pickerView.delegate = self
  56. self.topView.addSubview(self.pickerView)
  57. }
  58. public func showPickerView(){
  59. self.showInView(UIApplication.shared.keyWindow!)
  60. }
  61. public func hidePickerView(){
  62. UIView.animate(withDuration: 0.5, animations: {
  63. self.alpha = 0
  64. var point = self.center
  65. point.y += CGFloat(self.pickViewHeight)
  66. self.center = point
  67. }) { (finished) in
  68. //NotificationCenter.default.post(name: NSNotification.Name("QUIT_ATTACH_OBJ"), object: nil)
  69. self.removeFromSuperview()
  70. }
  71. }
  72. private func showInView(_ view:UIView){
  73. UIView.animate(withDuration: 0.5, animations: {
  74. var point = self.center
  75. point.y -= CGFloat(self.pickViewHeight)
  76. self.center = point
  77. }) { (finished) in
  78. }
  79. view.addSubview(self)
  80. }
  81. @objc private func quit(){
  82. UIView.animate(withDuration: 0.5, animations: {
  83. self.alpha = 0
  84. var point = self.center
  85. point.y += CGFloat(self.pickViewHeight)
  86. self.center = point
  87. }) { (finished) in
  88. NotificationCenter.default.post(name: NSNotification.Name("QUIT_ATTACH_OBJ"), object: nil)
  89. self.removeFromSuperview()
  90. }
  91. }
  92. func numberOfSections(in tableView: UITableView) -> Int {
  93. return 1
  94. }
  95. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  96. return models.count
  97. }
  98. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  99. let cell = tableView.dequeueReusableCell(withIdentifier: "ZonePickerCell", for: indexPath) as! ZoneFileListCell
  100. cell.delegate = self
  101. cell.pickerModel = self.models[indexPath.row]
  102. return cell
  103. }
  104. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  105. print("the \(indexPath.row) row")
  106. }
  107. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  108. return UIView()
  109. }
  110. func cellClick(_ model: ZonePickerModel) {
  111. self.selectModel = model
  112. NotificationCenter.default.post(name: NSNotification.Name("SHOW_ATTACH_OBJ"), object: model)
  113. }
  114. }