NavView.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // NavView1.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/4/3.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. class MyView:UIView {
  10. var tableViews = Array<UITableView>()
  11. var myLabel:UILabel?
  12. var scanBtn:UIButton?
  13. var addBtn:UIButton?
  14. override init(frame: CGRect) {
  15. super.init(frame: frame)
  16. myLabel = UILabel(frame: .zero)
  17. myLabel?.text = "首页"
  18. myLabel?.textColor = UIColor.white
  19. myLabel?.center = CGPoint(x: width / 2 - 15, y: 25)
  20. addSubview(myLabel!)
  21. let y = safeAreaTopHeight / 2
  22. scanBtn = UIButton(type: .custom)
  23. scanBtn?.frame = CGRect(x: 20, y: y, width: 20, height: 20)
  24. scanBtn?.setBackgroundImage(UIImage(named: "menu_scan_qrcode"), for: .normal)
  25. addSubview(scanBtn!)
  26. addBtn = UIButton(type: .custom)
  27. addBtn?.frame = CGRect(x: self.width-45, y: y, width: 20, height: 20)
  28. addBtn?.setBackgroundImage(UIImage(named:"add"), for: .normal)
  29. addSubview(addBtn!)
  30. }
  31. required init?(coder aDecoder: NSCoder) {
  32. fatalError("init(coder:) has not been implemented")
  33. }
  34. override func willMove(toSuperview newSuperview: UIView?) {
  35. super.willMove(toSuperview: newSuperview)
  36. for table in self.tableViews {
  37. table.addObserver(self, forKeyPath: "contentOffset", options: .new, context: nil)
  38. }
  39. }
  40. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  41. if (!(keyPath == "contentOffset")) {
  42. super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  43. return;
  44. }
  45. let tableView = object as! UITableView
  46. let tableViewoffsetY = tableView.contentOffset.y
  47. self.backgroundColor = base_color.withAlphaComponent(min(1, tableViewoffsetY/136))
  48. if tableViewoffsetY < 125 {
  49. for table in self.tableViews {
  50. table.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 56, right: 0)
  51. }
  52. UIView.animate(withDuration: 0.25, animations: {
  53. self.myLabel?.isHidden = true
  54. self.addBtn?.alpha = 1 - (min(1, tableViewoffsetY/136))
  55. self.scanBtn?.alpha = 1 - (min(1, tableViewoffsetY/136))
  56. })
  57. }else{
  58. for table in self.tableViews {
  59. table.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 56, right: 0)
  60. }
  61. UIView.animate(withDuration: 0.25, animations: {
  62. self.myLabel?.isHidden = false
  63. self.scanBtn?.alpha = 1
  64. self.addBtn?.alpha = 1
  65. })
  66. }
  67. }
  68. }
  69. //public class NavView:UIView {
  70. //
  71. // var tableViews = Array<UITableView>()
  72. //
  73. // var titleLabel:UILabel?
  74. //
  75. // var scanBtn: UIButton?
  76. //
  77. // var addBtn:UIButton?
  78. //
  79. // public override init(frame: CGRect) {
  80. // super.init(frame: frame)
  81. // //commonInit()
  82. // }
  83. //
  84. // private func commonInit(){
  85. //// do {
  86. //// titleLabel = UILabel(frame: .zero)
  87. //// titleLabel?.text = "首页"
  88. //// titleLabel?.textColor = UIColor.white
  89. //// titleLabel?.center = CGPoint(x: self.width / 2 - 15, y: 25)
  90. //// titleLabel?.size = CGSize(width: 100, height: 30)
  91. //// addSubview(titleLabel!)
  92. //// }
  93. ////
  94. //// do {
  95. //// scanBtn = UIButton(type: UIButtonType.custom)
  96. //// scanBtn?.frame = CGRect(x: 20, y: 30, width: 20, height: 20)
  97. //// scanBtn?.setBackgroundImage(UIImage(named: "menu_scan_qrcode"), for: .normal)
  98. //// addSubview(scanBtn!)
  99. //// }
  100. ////
  101. //// do {
  102. //// addBtn = UIButton(type: UIButtonType.custom)
  103. //// addBtn?.frame = CGRect(x: self.width-45, y: 30, w: 20, h: 20)
  104. //// addBtn?.setBackgroundImage(UIImage(named:"add"), for: .normal)
  105. //// addSubview(addBtn!)
  106. //// }
  107. // }
  108. //
  109. // public required init(coder aDecoder: NSCoder) {
  110. // super.init(coder: aDecoder)!
  111. // //commonInit()
  112. // }
  113. //
  114. // override public func willMove(toSuperview newSuperview: UIView?) {
  115. // super.willMove(toSuperview: newSuperview)
  116. // for table in self.tableViews {
  117. // table.addObserver(self, forKeyPath: "contentOffset", options: .new, context: nil)
  118. // }
  119. // }
  120. //
  121. // override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  122. // if (!(keyPath == "contentOffset")) {
  123. // super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
  124. // return;
  125. // }
  126. //
  127. // let tableView = object as! UITableView
  128. // let tableViewoffsetY = tableView.contentOffset.y
  129. //
  130. // self.backgroundColor = base_color.withAlphaComponent(min(1, tableViewoffsetY/136))
  131. // if tableViewoffsetY < 125 {
  132. // for table in self.tableViews {
  133. // table.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 56, right: 0)
  134. // }
  135. // UIView.animate(withDuration: 0.25, animations: {
  136. // self.titleLabel?.isHidden = true
  137. // self.addBtn?.alpha = 1 - (min(1, tableViewoffsetY/136))
  138. // self.scanBtn?.alpha = 1 - (min(1, tableViewoffsetY/136))
  139. //
  140. // })
  141. // }else{
  142. // for table in self.tableViews {
  143. // table.contentInset = UIEdgeInsets(top: 64, left: 0, bottom: 56, right: 0)
  144. // }
  145. //
  146. // UIView.animate(withDuration: 0.25, animations: {
  147. // self.titleLabel?.isHidden = false
  148. // self.scanBtn?.alpha = 1
  149. // self.addBtn?.alpha = 1
  150. // })
  151. // }
  152. //
  153. // }
  154. //
  155. //}