JXSegmentedIndicatorBaseView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // JXSegmentedIndicatorBaseView.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2018/12/26.
  6. // Copyright © 2018 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. public enum JXSegmentedIndicatorPosition {
  10. case top
  11. case bottom
  12. case center
  13. }
  14. open class JXSegmentedIndicatorBaseView: UIView, JXSegmentedIndicatorProtocol {
  15. /// 默认JXSegmentedViewAutomaticDimension(与cell的宽度相等)。内部通过getIndicatorWidth方法获取实际的值
  16. open var indicatorWidth: CGFloat = JXSegmentedViewAutomaticDimension
  17. open var indicatorWidthIncrement: CGFloat = 0 //指示器的宽度增量。比如需求是指示器宽度比cell宽度多10 point。就可以将该属性赋值为10。最终指示器的宽度=indicatorWidth+indicatorWidthIncrement
  18. /// 默认JXSegmentedViewAutomaticDimension(与cell的高度相等)。内部通过getIndicatorHeight方法获取实际的值
  19. open var indicatorHeight: CGFloat = JXSegmentedViewAutomaticDimension
  20. /// 默认JXSegmentedViewAutomaticDimension (等于indicatorHeight/2)。内部通过getIndicatorCornerRadius方法获取实际的值
  21. open var indicatorCornerRadius: CGFloat = JXSegmentedViewAutomaticDimension
  22. /// 指示器的颜色
  23. open var indicatorColor: UIColor = .red
  24. /// 指示器的位置,top、bottom、center
  25. open var indicatorPosition: JXSegmentedIndicatorPosition = .bottom
  26. /// 垂直方向偏移,指示器默认贴着底部或者顶部,verticalOffset越大越靠近中心。
  27. open var verticalOffset: CGFloat = 0
  28. /// 手势滚动、点击切换的时候,是否允许滚动。
  29. open var isScrollEnabled: Bool = true
  30. /// 是否需要将当前的indicator的frame转换到cell。辅助JXSegmentedTitleDataSourced的isTitleMaskEnabled属性使用。
  31. /// 如果添加了多个indicator,仅能有一个indicator的isIndicatorConvertToItemFrameEnabled为true。
  32. /// 如果有多个indicator的isIndicatorConvertToItemFrameEnabled为true,则以最后一个isIndicatorConvertToItemFrameEnabled为true的indicator为准。
  33. open var isIndicatorConvertToItemFrameEnabled: Bool = true
  34. /// 点击选中时的滚动动画时长
  35. open var scrollAnimationDuration: TimeInterval = 0.25
  36. /// 指示器的宽度是否跟随item的内容变化(而不是跟着cell的宽度变化)。indicatorWidth=JXSegmentedViewAutomaticDimension才能生效
  37. open var isIndicatorWidthSameAsItemContent = false
  38. public override init(frame: CGRect) {
  39. super.init(frame: frame)
  40. commonInit()
  41. }
  42. required public init?(coder aDecoder: NSCoder) {
  43. super.init(coder: aDecoder)
  44. commonInit()
  45. }
  46. open func commonInit() {
  47. }
  48. public func getIndicatorCornerRadius(itemFrame: CGRect) -> CGFloat {
  49. if indicatorCornerRadius == JXSegmentedViewAutomaticDimension {
  50. return getIndicatorHeight(itemFrame: itemFrame)/2
  51. }
  52. return indicatorCornerRadius
  53. }
  54. public func getIndicatorWidth(itemFrame: CGRect, itemContentWidth: CGFloat) -> CGFloat {
  55. if indicatorWidth == JXSegmentedViewAutomaticDimension {
  56. if isIndicatorWidthSameAsItemContent {
  57. return itemContentWidth + indicatorWidthIncrement
  58. }else {
  59. return itemFrame.size.width + indicatorWidthIncrement
  60. }
  61. }
  62. return indicatorWidth + indicatorWidthIncrement
  63. }
  64. public func getIndicatorHeight(itemFrame: CGRect) -> CGFloat {
  65. if indicatorHeight == JXSegmentedViewAutomaticDimension {
  66. return itemFrame.size.height
  67. }
  68. return indicatorHeight
  69. }
  70. public func canHandleTransition(model: JXSegmentedIndicatorTransitionParams) -> Bool {
  71. if model.percent == 0 || !isScrollEnabled {
  72. //model.percent等于0时不需要处理,会调用selectItem(model: JXSegmentedIndicatorParamsModel)方法处理
  73. //isScrollEnabled为false不需要处理
  74. return false
  75. }
  76. return true
  77. }
  78. public func canSelectedWithAnimation(model: JXSegmentedIndicatorSelectedParams) -> Bool {
  79. if isScrollEnabled && (model.selectedType == .click || model.selectedType == .code) {
  80. //允许滚动且选中类型是点击或代码选中,才进行动画过渡
  81. return true
  82. }
  83. return false
  84. }
  85. //MARK: - JXSegmentedIndicatorProtocol
  86. open func refreshIndicatorState(model: JXSegmentedIndicatorSelectedParams) {
  87. }
  88. open func contentScrollViewDidScroll(model: JXSegmentedIndicatorTransitionParams) {
  89. }
  90. open func selectItem(model: JXSegmentedIndicatorSelectedParams) {
  91. }
  92. }