JXSegmentedIndicatorDoubleLineView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // JXSegmentedIndicatorDoubleLineView.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2019/1/16.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. open class JXSegmentedIndicatorDoubleLineView: JXSegmentedIndicatorBaseView {
  10. /// 线收缩到最小的百分比
  11. open var minLineWidthPercent: CGFloat = 0.2
  12. public let selectedLineView: UIView = UIView()
  13. public let otherLineView: UIView = UIView()
  14. open override func commonInit() {
  15. super.commonInit()
  16. indicatorHeight = 3
  17. addSubview(selectedLineView)
  18. otherLineView.alpha = 0
  19. addSubview(otherLineView)
  20. }
  21. open override func refreshIndicatorState(model: JXSegmentedIndicatorParamsModel) {
  22. super.refreshIndicatorState(model: model)
  23. selectedLineView.backgroundColor = indicatorColor
  24. otherLineView.backgroundColor = indicatorColor
  25. selectedLineView.layer.cornerRadius = getIndicatorCornerRadius(itemFrame: model.currentSelectedItemFrame)
  26. otherLineView.layer.cornerRadius = getIndicatorCornerRadius(itemFrame: model.currentSelectedItemFrame)
  27. let width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame)
  28. let height = getIndicatorHeight(itemFrame: model.currentSelectedItemFrame)
  29. let x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  30. var y = model.currentSelectedItemFrame.size.height - height - verticalOffset
  31. if indicatorPosition == .top {
  32. y = verticalOffset
  33. }
  34. selectedLineView.frame = CGRect(x: x, y: y, width: width, height: height)
  35. otherLineView.frame = selectedLineView.frame
  36. }
  37. open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorParamsModel) {
  38. super.contentScrollViewDidScroll(model: model)
  39. if model.percent == 0 || !isScrollEnabled {
  40. //model.percent等于0时不需要处理,会调用selectItem(model: JXSegmentedIndicatorParamsModel)方法处理
  41. //isScrollEnabled为false不需要处理
  42. return
  43. }
  44. let rightItemFrame = model.rightItemFrame
  45. let leftItemFrame = model.leftItemFrame
  46. let percent = model.percent
  47. let leftCenter = getCenter(in: leftItemFrame)
  48. let rightCenter = getCenter(in: rightItemFrame)
  49. let leftMaxWidth = getIndicatorWidth(itemFrame: leftItemFrame)
  50. let rightMaxWidth = getIndicatorWidth(itemFrame: rightItemFrame)
  51. let leftMinWidth = leftMaxWidth*minLineWidthPercent
  52. let rightMinWidth = rightMaxWidth*minLineWidthPercent
  53. let leftWidth: CGFloat = JXSegmentedViewTool.interpolate(from: leftMaxWidth, to: leftMinWidth, percent: CGFloat(percent))
  54. let rightWidth: CGFloat = JXSegmentedViewTool.interpolate(from: rightMinWidth, to: rightMaxWidth, percent: CGFloat(percent))
  55. let leftAlpha: CGFloat = JXSegmentedViewTool.interpolate(from: 1, to: 0, percent: CGFloat(percent))
  56. let rightAlpha: CGFloat = JXSegmentedViewTool.interpolate(from: 0, to: 1, percent: CGFloat(percent))
  57. if model.currentSelectedIndex == model.leftIndex {
  58. selectedLineView.bounds.size.width = leftWidth
  59. selectedLineView.center = leftCenter
  60. selectedLineView.alpha = leftAlpha
  61. otherLineView.bounds.size.width = rightWidth
  62. otherLineView.center = rightCenter
  63. otherLineView.alpha = rightAlpha
  64. }else {
  65. otherLineView.bounds.size.width = leftWidth
  66. otherLineView.center = leftCenter
  67. otherLineView.alpha = leftAlpha
  68. selectedLineView.bounds.size.width = rightWidth
  69. selectedLineView.center = rightCenter
  70. selectedLineView.alpha = rightAlpha
  71. }
  72. }
  73. open override func selectItem(model: JXSegmentedIndicatorParamsModel) {
  74. super.selectItem(model: model)
  75. let targetWidth = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame)
  76. let targetCenter = getCenter(in: model.currentSelectedItemFrame)
  77. selectedLineView.bounds.size.width = targetWidth
  78. selectedLineView.center = targetCenter
  79. selectedLineView.alpha = 1
  80. otherLineView.alpha = 0
  81. }
  82. private func getCenter(in frame: CGRect) -> CGPoint {
  83. return CGPoint(x: frame.midX, y: selectedLineView.center.y)
  84. }
  85. }