JXSegmentedIndicatorDotLineView.swift 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: JXSegmentedIndicatorSelectedParams) {
  19. super.refreshIndicatorState(model: model)
  20. backgroundColor = indicatorColor
  21. layer.cornerRadius = getIndicatorCornerRadius(itemFrame: model.currentSelectedItemFrame)
  22. let width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
  23. let height = getIndicatorHeight(itemFrame: model.currentSelectedItemFrame)
  24. let x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  25. var y: CGFloat = 0
  26. switch indicatorPosition {
  27. case .top:
  28. y = verticalOffset
  29. case .bottom:
  30. y = model.currentSelectedItemFrame.size.height - height - verticalOffset
  31. case .center:
  32. y = (model.currentSelectedItemFrame.size.height - height)/2 + verticalOffset
  33. }
  34. frame = CGRect(x: x, y: y, width: width, height: height)
  35. }
  36. open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorTransitionParams) {
  37. super.contentScrollViewDidScroll(model: model)
  38. guard canHandleTransition(model: model) else {
  39. return
  40. }
  41. let rightItemFrame = model.rightItemFrame
  42. let leftItemFrame = model.leftItemFrame
  43. let percent = model.percent
  44. var targetX: CGFloat = leftItemFrame.origin.x
  45. let dotWidth = getIndicatorWidth(itemFrame: leftItemFrame, itemContentWidth: model.leftItemContentWidth)
  46. var targetWidth = dotWidth
  47. let leftWidth = targetWidth
  48. let rightWidth = getIndicatorWidth(itemFrame: rightItemFrame, itemContentWidth: model.rightItemContentWidth)
  49. let leftX = leftItemFrame.origin.x + (leftItemFrame.size.width - leftWidth)/2
  50. let rightX = rightItemFrame.origin.x + (rightItemFrame.size.width - rightWidth)/2
  51. let centerX = leftX + (rightX - leftX - lineMaxWidth)/2
  52. //前50%,移动x,增加宽度;后50%,移动x并减小width
  53. if percent <= 0.5 {
  54. targetX = JXSegmentedViewTool.interpolate(from: leftX, to: centerX, percent: CGFloat(percent*2))
  55. targetWidth = JXSegmentedViewTool.interpolate(from: dotWidth, to: lineMaxWidth, percent: CGFloat(percent*2))
  56. }else {
  57. targetX = JXSegmentedViewTool.interpolate(from: centerX, to: rightX, percent: CGFloat((percent - 0.5)*2))
  58. targetWidth = JXSegmentedViewTool.interpolate(from: lineMaxWidth, to: dotWidth, percent: CGFloat((percent - 0.5)*2))
  59. }
  60. self.frame.origin.x = targetX
  61. self.frame.size.width = targetWidth
  62. }
  63. open override func selectItem(model: JXSegmentedIndicatorSelectedParams) {
  64. super.selectItem(model: model)
  65. let targetWidth = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
  66. var toFrame = self.frame
  67. toFrame.origin.x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - targetWidth)/2
  68. toFrame.size.width = targetWidth
  69. if canSelectedWithAnimation(model: model) {
  70. UIView.animate(withDuration: scrollAnimationDuration, delay: 0, options: .curveEaseOut, animations: {
  71. self.frame = toFrame
  72. }) { (_) in
  73. }
  74. }else {
  75. frame = toFrame
  76. }
  77. }
  78. }