JXSegmentedIndicatorBackgroundView.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // JXSegmentedIndicatorBackgroundView.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2018/12/28.
  6. // Copyright © 2018 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. /// 不支持indicatorPosition、verticalOffset。默认垂直居中。
  10. open class JXSegmentedIndicatorBackgroundView: JXSegmentedIndicatorBaseView {
  11. @available(*, deprecated, renamed: "indicatorWidthIncrement")
  12. open var backgroundWidthIncrement: CGFloat = 20 {
  13. didSet {
  14. indicatorWidthIncrement = backgroundWidthIncrement
  15. }
  16. }
  17. open override func commonInit() {
  18. super.commonInit()
  19. indicatorWidthIncrement = 20
  20. indicatorHeight = 26
  21. indicatorColor = .lightGray
  22. indicatorPosition = .center
  23. verticalOffset = 0
  24. }
  25. open override func refreshIndicatorState(model: JXSegmentedIndicatorSelectedParams) {
  26. super.refreshIndicatorState(model: model)
  27. backgroundColor = indicatorColor
  28. layer.cornerRadius = getIndicatorCornerRadius(itemFrame: model.currentSelectedItemFrame)
  29. let width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
  30. let height = getIndicatorHeight(itemFrame: model.currentSelectedItemFrame)
  31. let x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  32. var y: CGFloat = 0
  33. switch indicatorPosition {
  34. case .top:
  35. y = verticalOffset
  36. case .bottom:
  37. y = model.currentSelectedItemFrame.size.height - height - verticalOffset
  38. case .center:
  39. y = (model.currentSelectedItemFrame.size.height - height)/2 + verticalOffset
  40. }
  41. frame = CGRect(x: x, y: y, width: width, height: height)
  42. }
  43. open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorTransitionParams) {
  44. super.contentScrollViewDidScroll(model: model)
  45. guard canHandleTransition(model: model) else {
  46. return
  47. }
  48. let rightItemFrame = model.rightItemFrame
  49. let leftItemFrame = model.leftItemFrame
  50. let percent = model.percent
  51. var targetWidth = getIndicatorWidth(itemFrame: leftItemFrame, itemContentWidth: model.leftItemContentWidth)
  52. let leftWidth = targetWidth
  53. let rightWidth = getIndicatorWidth(itemFrame: rightItemFrame, itemContentWidth: model.rightItemContentWidth)
  54. let leftX = leftItemFrame.origin.x + (leftItemFrame.size.width - leftWidth)/2
  55. let rightX = rightItemFrame.origin.x + (rightItemFrame.size.width - rightWidth)/2
  56. let targetX = JXSegmentedViewTool.interpolate(from: leftX, to: rightX, percent: CGFloat(percent))
  57. if indicatorWidth == JXSegmentedViewAutomaticDimension {
  58. targetWidth = JXSegmentedViewTool.interpolate(from: leftWidth, to: rightWidth, percent: CGFloat(percent))
  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 width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
  66. var toFrame = self.frame
  67. toFrame.origin.x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  68. toFrame.size.width = width
  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. }