MCNavBarSegmentValue1View.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // MCNavBarSegmentValue1View.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2022/9/22.
  6. // Copyright © 2022 Jyp. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import RxCocoa
  11. /// <#Description#>
  12. class MCNavBarSegmentValue1View: MCNavBarView {
  13. init(dataSource: [String], style: MCNavBarStyle? = nil, tap: ((Int)->Void)?, defaultSelectedIndex: Int = 0){
  14. super.init(title: nil, subTitle: nil, style: style)
  15. self.contentView.addArrangedSubview(self.stackView)
  16. self.stackView.mas_makeConstraints { make in
  17. make?.height.offset()(KNavBarHeight)
  18. }
  19. for i in 0..<dataSource.count{
  20. let segmentView = NavSegmentView.init(name: dataSource[i], style: self.navStyle)
  21. segmentView.tap = {[unowned self] in
  22. segmentView.scaleText()
  23. _ = self.stackView.arrangedSubviews.filter { e in
  24. e != segmentView
  25. }.map { e in
  26. (e as! NavSegmentView).identifyText()
  27. }
  28. tap?(i)
  29. }
  30. self.stackView.addArrangedSubview(segmentView)
  31. segmentView.mas_makeConstraints { make in
  32. make?.height.offset()(KNavBarHeight)
  33. }
  34. }
  35. if self.stackView.arrangedSubviews.canGet(at: defaultSelectedIndex).0{
  36. (self.stackView.arrangedSubviews[defaultSelectedIndex] as! NavSegmentView).scaleText()
  37. _ = self.stackView.arrangedSubviews.filter { e in
  38. e != self.stackView.arrangedSubviews[defaultSelectedIndex]
  39. }.map { e in
  40. (e as! NavSegmentView).identifyText()
  41. }
  42. }
  43. self.resetConstraint()
  44. }
  45. //MARK: - Private Property
  46. private lazy var stackView: UIStackView! = {
  47. let stackView = UIStackView.init(frame: CGRect.zero)
  48. stackView.alignment = .center
  49. stackView.axis = .horizontal
  50. stackView.distribution = .fillEqually
  51. return stackView
  52. }()
  53. //MARK: - Override MCNavBarDelegate
  54. override var type: MCNavBarType{
  55. .segment
  56. }
  57. override func setNavTitle(_ title: String?) {
  58. if let index = Convert.toInt(title) {
  59. if self.stackView.arrangedSubviews.canGet(at: index).0{
  60. (self.stackView.arrangedSubviews[index] as! NavSegmentView).scaleText()
  61. }
  62. }
  63. }
  64. override func setNavSubTitle(_ title: String?) {
  65. return
  66. }
  67. //MARK: -
  68. required init?(coder: NSCoder) {
  69. fatalError("init(coder:) has not been implemented")
  70. }
  71. }
  72. fileprivate class NavSegmentView: UIView{
  73. /// 点击事件
  74. var tap: (()->Void)?{
  75. didSet{
  76. self.addTapAction {[unowned self] _ in
  77. if self.isSelected{
  78. return
  79. }
  80. self.isSelected = true
  81. self.tap?()
  82. }
  83. }
  84. }
  85. init(name: String, style: MCNavBarStyle){
  86. super.init(frame: CGRect.zero)
  87. self.style = style
  88. self.textLabel = UILabel.init(frame: CGRect.zero)
  89. self.textLabel.text = name
  90. self.textLabel.textAlignment = .center
  91. self.textLabel.textColor = style.styleForSubTitleColor
  92. self.textLabel.font = style.styleForTitleFont
  93. self.addSubview(self.textLabel)
  94. self.textLabel.mas_makeConstraints { make in
  95. make?.center.mas_equalTo()(self)
  96. }
  97. }
  98. /// 放大text
  99. func scaleText(){
  100. if !self.isSelected{
  101. self.isSelected = true
  102. }
  103. self.textLabel.transform = CGAffineTransform.identity
  104. self.textLabel.textColor = style.styleForTitleColor
  105. self.textLabel.font = style.styleForTitleFont
  106. self.textLabel.transform = CGAffineTransform.init(scaleX: self.style.styleForScaling, y: self.style.styleForScaling)
  107. }
  108. /// 还原text
  109. func identifyText(){
  110. if self.isSelected{
  111. self.isSelected = false
  112. }
  113. self.textLabel.transform = CGAffineTransform.identity
  114. self.textLabel.textColor = style.styleForScalingBeforeTitleColor
  115. }
  116. //MARK: - Private Property
  117. /// 内容
  118. private var textLabel: UILabel!
  119. private var style: MCNavBarStyle!
  120. private var isSelected: Bool = false
  121. //MARK: -
  122. required init?(coder: NSCoder) {
  123. fatalError("init(coder:) has not been implemented")
  124. }
  125. }