JXSegmentedIndicatorBackgroundView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. /// 宽度增量,背景指示器一般要比cell宽一些
  12. open var backgroundWidthIncrement: CGFloat = 20
  13. open override func commonInit() {
  14. super.commonInit()
  15. indicatorHeight = 26
  16. indicatorColor = .lightGray
  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. let y = (model.currentSelectedItemFrame.size.height - height)/2
  26. frame = CGRect(x: x, y: y, width: width, height: height)
  27. }
  28. open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorParamsModel) {
  29. super.contentScrollViewDidScroll(model: model)
  30. if model.percent == 0 || !isScrollEnabled {
  31. //model.percent等于0时不需要处理,会调用selectItem(model: JXSegmentedIndicatorParamsModel)方法处理
  32. //isScrollEnabled为false不需要处理
  33. return
  34. }
  35. let rightItemFrame = model.rightItemFrame
  36. let leftItemFrame = model.leftItemFrame
  37. let percent = model.percent
  38. var targetWidth = getIndicatorWidth(itemFrame: leftItemFrame)
  39. let leftWidth = targetWidth
  40. let rightWidth = getIndicatorWidth(itemFrame: rightItemFrame)
  41. let leftX = leftItemFrame.origin.x + (leftItemFrame.size.width - leftWidth)/2
  42. let rightX = rightItemFrame.origin.x + (rightItemFrame.size.width - rightWidth)/2
  43. let targetX = JXSegmentedViewTool.interpolate(from: leftX, to: rightX, percent: CGFloat(percent))
  44. if indicatorWidth == JXSegmentedViewAutomaticDimension {
  45. targetWidth = JXSegmentedViewTool.interpolate(from: leftWidth, to: rightWidth, percent: CGFloat(percent))
  46. }
  47. self.frame.origin.x = targetX
  48. self.frame.size.width = targetWidth
  49. }
  50. open override func selectItem(model: JXSegmentedIndicatorParamsModel) {
  51. super.selectItem(model: model)
  52. let width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame)
  53. var toFrame = self.frame
  54. toFrame.origin.x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
  55. toFrame.size.width = width
  56. if isScrollEnabled && (model.selectedType == .click || model.selectedType == .code) {
  57. //允许滚动且选中类型是点击或代码选中,才进行动画过渡
  58. UIView.animate(withDuration: scrollAnimationDuration, delay: 0, options: .curveEaseOut, animations: {
  59. self.frame = toFrame
  60. }) { (_) in
  61. }
  62. }else {
  63. frame = toFrame
  64. }
  65. }
  66. open override func getIndicatorWidth(itemFrame: CGRect) -> CGFloat {
  67. return super.getIndicatorWidth(itemFrame: itemFrame) + backgroundWidthIncrement
  68. }
  69. }