JXSegmentedIndicatorBaseView.swift 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. }
  13. open class JXSegmentedIndicatorBaseView: UIView, JXSegmentedIndicatorProtocol {
  14. /// 默认JXSegmentedViewAutomaticDimension(与cell的宽度相等)。内部通过getIndicatorWidth方法获取实际的值
  15. open var indicatorWidth: CGFloat = JXSegmentedViewAutomaticDimension
  16. open var indicatorWidthIncrement: CGFloat = 0 //指示器的宽度增量。比如需求是指示器宽度比cell宽度多10 point。就可以将该属性赋值为10。最终指示器的宽度=indicatorWidth+indicatorWidthIncrement
  17. /// 默认JXSegmentedViewAutomaticDimension(与cell的高度相等)。内部通过getIndicatorHeight方法获取实际的值
  18. open var indicatorHeight: CGFloat = JXSegmentedViewAutomaticDimension
  19. /// 默认JXSegmentedViewAutomaticDimension (等于indicatorHeight/2)。内部通过getIndicatorCornerRadius方法获取实际的值
  20. open var indicatorCornerRadius: CGFloat = JXSegmentedViewAutomaticDimension
  21. /// 指示器的颜色
  22. open var indicatorColor: UIColor = .red
  23. /// 指示器的位置,top或者bottom
  24. open var indicatorPosition: JXSegmentedIndicatorPosition = .bottom
  25. /// 垂直方向偏移,指示器默认贴着底部或者顶部,verticalOffset越大越靠近中心。
  26. open var verticalOffset: CGFloat = 0
  27. /// 手势滚动、点击切换的时候,是否允许滚动。
  28. open var isScrollEnabled: Bool = true
  29. /// 是否需要将当前的indicator的frame转换到cell。辅助JXSegmentedTitleDataSourced的isTitleMaskEnabled属性使用。
  30. /// 如果添加了多个indicator,仅能有一个indicator的isIndicatorConvertToItemFrameEnabled为true。
  31. /// 如果有多个indicator的isIndicatorConvertToItemFrameEnabled为true,则以最后一个isIndicatorConvertToItemFrameEnabled为true的indicator为准。
  32. open var isIndicatorConvertToItemFrameEnabled: Bool = true
  33. /// 点击选中时的滚动动画时长
  34. open var scrollAnimationDuration: TimeInterval = 0.25
  35. public override init(frame: CGRect) {
  36. super.init(frame: frame)
  37. commonInit()
  38. }
  39. required public init?(coder aDecoder: NSCoder) {
  40. super.init(coder: aDecoder)
  41. commonInit()
  42. }
  43. open func commonInit() {
  44. }
  45. public func getIndicatorCornerRadius(itemFrame: CGRect) -> CGFloat {
  46. if indicatorCornerRadius == JXSegmentedViewAutomaticDimension {
  47. return getIndicatorHeight(itemFrame: itemFrame)/2
  48. }
  49. return indicatorCornerRadius
  50. }
  51. public func getIndicatorWidth(itemFrame: CGRect) -> CGFloat {
  52. if indicatorWidth == JXSegmentedViewAutomaticDimension {
  53. return itemFrame.size.width + indicatorWidthIncrement
  54. }
  55. return indicatorWidth + indicatorWidthIncrement
  56. }
  57. public func getIndicatorHeight(itemFrame: CGRect) -> CGFloat {
  58. if indicatorHeight == JXSegmentedViewAutomaticDimension {
  59. return itemFrame.size.height
  60. }
  61. return indicatorHeight
  62. }
  63. //MARK: - JXSegmentedIndicatorProtocol
  64. open func refreshIndicatorState(model: JXSegmentedIndicatorParamsModel) {
  65. }
  66. open func contentScrollViewDidScroll(model: JXSegmentedIndicatorParamsModel) {
  67. }
  68. open func selectItem(model: JXSegmentedIndicatorParamsModel) {
  69. }
  70. }