JXSegmentedIndicatorDoubleLineView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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: JXSegmentedIndicatorSelectedParams) {
  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, itemContentWidth: model.currentItemContentWidth)
  28. let height = getIndicatorHeight(itemFrame: model.currentSelectedItemFrame)
  29. let x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  30. var y: CGFloat = 0
  31. switch indicatorPosition {
  32. case .top:
  33. y = verticalOffset
  34. case .bottom:
  35. y = model.currentSelectedItemFrame.size.height - height - verticalOffset
  36. case .center:
  37. y = (model.currentSelectedItemFrame.size.height - height)/2 + verticalOffset
  38. }
  39. selectedLineView.frame = CGRect(x: x, y: y, width: width, height: height)
  40. otherLineView.frame = selectedLineView.frame
  41. }
  42. open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorTransitionParams) {
  43. super.contentScrollViewDidScroll(model: model)
  44. guard canHandleTransition(model: model) else {
  45. return
  46. }
  47. let rightItemFrame = model.rightItemFrame
  48. let leftItemFrame = model.leftItemFrame
  49. let percent = model.percent
  50. let leftCenter = getCenter(in: leftItemFrame)
  51. let rightCenter = getCenter(in: rightItemFrame)
  52. let leftMaxWidth = getIndicatorWidth(itemFrame: leftItemFrame, itemContentWidth: model.leftItemContentWidth)
  53. let rightMaxWidth = getIndicatorWidth(itemFrame: rightItemFrame, itemContentWidth: model.rightItemContentWidth)
  54. let leftMinWidth = leftMaxWidth*minLineWidthPercent
  55. let rightMinWidth = rightMaxWidth*minLineWidthPercent
  56. let leftWidth: CGFloat = JXSegmentedViewTool.interpolate(from: leftMaxWidth, to: leftMinWidth, percent: CGFloat(percent))
  57. let rightWidth: CGFloat = JXSegmentedViewTool.interpolate(from: rightMinWidth, to: rightMaxWidth, percent: CGFloat(percent))
  58. let leftAlpha: CGFloat = JXSegmentedViewTool.interpolate(from: 1, to: 0, percent: CGFloat(percent))
  59. let rightAlpha: CGFloat = JXSegmentedViewTool.interpolate(from: 0, to: 1, percent: CGFloat(percent))
  60. if model.currentSelectedIndex == model.leftIndex {
  61. selectedLineView.bounds.size.width = leftWidth
  62. selectedLineView.center = leftCenter
  63. selectedLineView.alpha = leftAlpha
  64. otherLineView.bounds.size.width = rightWidth
  65. otherLineView.center = rightCenter
  66. otherLineView.alpha = rightAlpha
  67. }else {
  68. otherLineView.bounds.size.width = leftWidth
  69. otherLineView.center = leftCenter
  70. otherLineView.alpha = leftAlpha
  71. selectedLineView.bounds.size.width = rightWidth
  72. selectedLineView.center = rightCenter
  73. selectedLineView.alpha = rightAlpha
  74. }
  75. }
  76. open override func selectItem(model: JXSegmentedIndicatorSelectedParams) {
  77. super.selectItem(model: model)
  78. let targetWidth = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
  79. let targetCenter = getCenter(in: model.currentSelectedItemFrame)
  80. selectedLineView.bounds.size.width = targetWidth
  81. selectedLineView.center = targetCenter
  82. selectedLineView.alpha = 1
  83. otherLineView.alpha = 0
  84. }
  85. private func getCenter(in frame: CGRect) -> CGPoint {
  86. return CGPoint(x: frame.midX, y: selectedLineView.center.y)
  87. }
  88. }