JXSegmentedIndicatorDotLineView.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // JXSegmentedIndicatorDotLineView.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 JXSegmentedIndicatorDotLineView: JXSegmentedIndicatorBaseView {
  10. /// 线的最大宽度
  11. open var lineMaxWidth: CGFloat = 50
  12. open override func commonInit() {
  13. super.commonInit()
  14. //配置点的size
  15. indicatorWidth = 10
  16. indicatorHeight = 10
  17. }
  18. open override func refreshIndicatorState(model: JXSegmentedIndicatorParamsModel) {
  19. super.refreshIndicatorState(model: model)
  20. backgroundColor = indicatorColor
  21. layer.cornerRadius = getIndicatorCornerRadius(itemFrame: model.currentSelectedItemFrame)
  22. let width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame)
  23. let height = getIndicatorHeight(itemFrame: model.currentSelectedItemFrame)
  24. let x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  25. var y = model.currentSelectedItemFrame.size.height - height - verticalOffset
  26. if indicatorPosition == .top {
  27. y = verticalOffset
  28. }
  29. frame = CGRect(x: x, y: y, width: width, height: height)
  30. }
  31. open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorParamsModel) {
  32. super.contentScrollViewDidScroll(model: model)
  33. if model.percent == 0 || !isScrollEnabled {
  34. //model.percent等于0时不需要处理,会调用selectItem(model: JXSegmentedIndicatorParamsModel)方法处理
  35. //isScrollEnabled为false不需要处理
  36. return
  37. }
  38. let rightItemFrame = model.rightItemFrame
  39. let leftItemFrame = model.leftItemFrame
  40. let percent = model.percent
  41. var targetX: CGFloat = leftItemFrame.origin.x
  42. let dotWidth = getIndicatorWidth(itemFrame: leftItemFrame)
  43. var targetWidth = dotWidth
  44. let leftWidth = targetWidth
  45. let rightWidth = getIndicatorWidth(itemFrame: rightItemFrame)
  46. let leftX = leftItemFrame.origin.x + (leftItemFrame.size.width - leftWidth)/2
  47. let rightX = rightItemFrame.origin.x + (rightItemFrame.size.width - rightWidth)/2
  48. let centerX = leftX + (rightX - leftX - lineMaxWidth)/2
  49. //前50%,移动x,增加宽度;后50%,移动x并减小width
  50. if percent <= 0.5 {
  51. targetX = JXSegmentedViewTool.interpolate(from: leftX, to: centerX, percent: CGFloat(percent*2))
  52. targetWidth = JXSegmentedViewTool.interpolate(from: dotWidth, to: lineMaxWidth, percent: CGFloat(percent*2))
  53. }else {
  54. targetX = JXSegmentedViewTool.interpolate(from: centerX, to: rightX, percent: CGFloat((percent - 0.5)*2))
  55. targetWidth = JXSegmentedViewTool.interpolate(from: lineMaxWidth, to: dotWidth, percent: CGFloat((percent - 0.5)*2))
  56. }
  57. self.frame.origin.x = targetX
  58. self.frame.size.width = targetWidth
  59. }
  60. open override func selectItem(model: JXSegmentedIndicatorParamsModel) {
  61. super.selectItem(model: model)
  62. let targetWidth = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame)
  63. var toFrame = self.frame
  64. toFrame.origin.x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - targetWidth)/2
  65. toFrame.size.width = targetWidth
  66. if isScrollEnabled && (model.selectedType == .click || model.selectedType == .code) {
  67. //允许滚动且选中类型是点击或代码选中,才进行动画过渡
  68. UIView.animate(withDuration: scrollAnimationDuration, delay: 0, options: .curveEaseOut, animations: {
  69. self.frame = toFrame
  70. }) { (_) in
  71. }
  72. }else {
  73. frame = toFrame
  74. }
  75. }
  76. }