酷店

XLPageSegmentedTitleView.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. //
  2. // XLPageSegmentedTitleView.m
  3. // XLPageViewControllerExample
  4. //
  5. // Created by MengXianLiang on 2019/5/9.
  6. // Copyright © 2019 jwzt. All rights reserved.
  7. // https://github.com/mengxianliang/XLPageViewController
  8. #import "XLPageSegmentedTitleView.h"
  9. #import "XLPageViewControllerUtil.h"
  10. @interface XLPageSegmentedTitleView ()
  11. @property (nonatomic, strong) UISegmentedControl *segmentedControl;
  12. //配置信息
  13. @property (nonatomic, strong) XLPageViewControllerConfig *config;
  14. //底部分割线
  15. @property (nonatomic, strong) UIView *separatorLine;
  16. //判断是否已经加载了数据
  17. @property (nonatomic, assign) BOOL haveLoadedDataSource;
  18. @end
  19. @implementation XLPageSegmentedTitleView
  20. //初始化方法
  21. - (instancetype)initWithConfig:(XLPageViewControllerConfig *)config {
  22. if (self = [super init]) {
  23. [self initSegmentedWithConfig:config];
  24. }
  25. return self;
  26. }
  27. //初始化方法
  28. - (void)initSegmentedWithConfig:(XLPageViewControllerConfig *)config {
  29. self.config = config;
  30. self.segmentedControl = [[UISegmentedControl alloc] init];
  31. [self.segmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
  32. self.segmentedControl.tintColor = config.segmentedTintColor;
  33. [self addSubview:self.segmentedControl];
  34. self.separatorLine = [[UIView alloc] init];
  35. self.separatorLine.backgroundColor = config.separatorLineColor;
  36. self.separatorLine.hidden = config.separatorLineHidden;
  37. [self addSubview:self.separatorLine];
  38. }
  39. //自动布局
  40. - (void)layoutSubviews {
  41. [super layoutSubviews];
  42. CGFloat segmentH = self.bounds.size.height - self.config.titleViewInset.top - self.config.titleViewInset.bottom;
  43. CGFloat segmentW = self.bounds.size.width - self.config.titleViewInset.left - self.config.titleViewInset.right;
  44. self.segmentedControl.frame = CGRectMake(self.config.titleViewInset.left, self.config.titleViewInset.top, segmentW, segmentH);
  45. self.separatorLine.frame = CGRectMake(0, self.bounds.size.height - self.config.separatorLineHeight, self.bounds.size.width, self.config.separatorLineHeight);
  46. //加载数据源
  47. if (!self.haveLoadedDataSource) {
  48. [self loadDataSource];
  49. }
  50. }
  51. //加载分段选择器数据源
  52. - (void)loadDataSource {
  53. self.haveLoadedDataSource = true;
  54. for (NSInteger i = 0; i < [self.dataSource pageTitleViewNumberOfTitle]; i++) {
  55. NSString *title = [self.dataSource pageTitleViewTitleForIndex:i];
  56. [self.segmentedControl insertSegmentWithTitle:title atIndex:self.segmentedControl.numberOfSegments animated:false];
  57. }
  58. self.segmentedControl.selectedSegmentIndex = self.selectedIndex;
  59. }
  60. //刷新方法
  61. - (void)reloadData {
  62. [self.segmentedControl removeAllSegments];
  63. for (NSInteger i = 0; i < [self.dataSource pageTitleViewNumberOfTitle]; i++) {
  64. NSString *title = [self.dataSource pageTitleViewTitleForIndex:i];
  65. [self.segmentedControl insertSegmentWithTitle:title atIndex:self.segmentedControl.numberOfSegments animated:false];
  66. }
  67. self.segmentedControl.selectedSegmentIndex = self.selectedIndex;
  68. }
  69. //切换方法
  70. - (void)segmentValueChanged:(UISegmentedControl *)segmentedControl {
  71. [self.delegate pageTitleViewDidSelectedAtIndex:segmentedControl.selectedSegmentIndex];
  72. self.lastSelectedIndex = segmentedControl.selectedSegmentIndex;
  73. }
  74. #pragma mark -
  75. #pragma mark Setter
  76. - (void)setSelectedIndex:(NSInteger)selectedIndex {
  77. [super setSelectedIndex:selectedIndex];
  78. self.segmentedControl.selectedSegmentIndex = selectedIndex;
  79. }
  80. @end