FUSegmentedControl.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // FUSegmentedControl.m
  3. // FULiveDemo
  4. //
  5. // Created by 项林平 on 2022/10/19.
  6. //
  7. #import "FUSegmentedControl.h"
  8. static NSString * const kFUSegmentedCellIdentifierKey = @"FUSegmentedCellIdentifier";
  9. @interface FUSegmentedControl ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
  10. @property (nonatomic, strong) UICollectionView *segmentedCollection;
  11. @end
  12. @implementation FUSegmentedControl
  13. - (instancetype)initWithFrame:(CGRect)frame {
  14. return [self initWithFrame:frame items:nil];
  15. }
  16. - (instancetype)initWithFrame:(CGRect)frame items:(NSArray<NSString *> *)items {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. _items = items;
  20. _titleColor = [UIColor whiteColor];
  21. _selectedTitleColor = [UIColor blackColor];
  22. _titleFont = [UIFont systemFontOfSize:11 weight:UIFontWeightMedium];
  23. [self addSubview:self.segmentedCollection];
  24. [self.segmentedCollection mas_makeConstraints:^(MASConstraintMaker *make) {
  25. make.edges.equalTo(self);
  26. }];
  27. }
  28. return self;
  29. }
  30. #pragma mark - UICollectionViewDataSource
  31. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  32. return self.items.count;
  33. }
  34. - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  35. FUSegmentedCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFUSegmentedCellIdentifierKey forIndexPath:indexPath];
  36. cell.textColor = self.titleColor;
  37. cell.selectedTextColor = self.selectedTitleColor;
  38. cell.textLabel.font = self.titleFont;
  39. cell.textLabel.text = self.items[indexPath.item];
  40. cell.selected = indexPath.item == self.selectedIndex;
  41. return cell;
  42. }
  43. #pragma mark - UICollectionViewDelegateFlowLayout
  44. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  45. return CGSizeMake(CGRectGetWidth(self.frame) / self.items.count, CGRectGetHeight(self.frame));
  46. }
  47. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  48. return 0;
  49. }
  50. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  51. return 0;
  52. }
  53. #pragma mark - UICollectionViewDelegate
  54. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  55. if (indexPath.item == self.selectedIndex) {
  56. return;
  57. }
  58. self.selectedIndex = indexPath.item;
  59. if (self.selectHandler) {
  60. self.selectHandler(self.selectedIndex);
  61. }
  62. }
  63. #pragma mark - Setters
  64. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  65. if (selectedIndex < 0 || selectedIndex >= self.items.count) {
  66. return;
  67. }
  68. _selectedIndex = selectedIndex;
  69. dispatch_async(dispatch_get_main_queue(), ^{
  70. [self.segmentedCollection reloadData];
  71. [self.segmentedCollection selectItemAtIndexPath:[NSIndexPath indexPathForItem:selectedIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionNone];
  72. });
  73. }
  74. - (void)setItems:(NSArray<NSString *> *)items {
  75. if (items.count == 0) {
  76. return;
  77. }
  78. _items = items;
  79. dispatch_async(dispatch_get_main_queue(), ^{
  80. [self.segmentedCollection reloadData];
  81. });
  82. }
  83. - (void)setTitleColor:(UIColor *)titleColor {
  84. _titleColor = titleColor;
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. [self.segmentedCollection reloadData];
  87. });
  88. }
  89. - (void)setSelectedTitleColor:(UIColor *)selectedTitleColor {
  90. _selectedTitleColor = selectedTitleColor;
  91. dispatch_async(dispatch_get_main_queue(), ^{
  92. [self.segmentedCollection reloadData];
  93. });
  94. }
  95. - (void)setTitleFont:(UIFont *)titleFont {
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. [self.segmentedCollection reloadData];
  98. });
  99. }
  100. #pragma mark - Getters
  101. - (UICollectionView *)segmentedCollection {
  102. if (!_segmentedCollection) {
  103. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  104. layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  105. _segmentedCollection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
  106. _segmentedCollection.showsVerticalScrollIndicator = NO;
  107. _segmentedCollection.showsHorizontalScrollIndicator = NO;
  108. _segmentedCollection.backgroundColor = [UIColor clearColor];
  109. _segmentedCollection.dataSource = self;
  110. _segmentedCollection.delegate = self;
  111. [_segmentedCollection registerClass:[FUSegmentedCell class] forCellWithReuseIdentifier:kFUSegmentedCellIdentifierKey];
  112. }
  113. return _segmentedCollection;
  114. }
  115. @end
  116. @interface FUSegmentedCell ()
  117. @property (nonatomic, strong) UILabel *textLabel;
  118. @end
  119. @implementation FUSegmentedCell
  120. - (instancetype)initWithFrame:(CGRect)frame {
  121. self = [super initWithFrame:frame];
  122. if (self) {
  123. [self.contentView addSubview:self.textLabel];
  124. [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.center.equalTo(self.contentView);
  126. }];
  127. }
  128. return self;
  129. }
  130. - (void)setSelected:(BOOL)selected {
  131. [super setSelected:selected];
  132. if (selected) {
  133. self.textLabel.textColor = self.selectedTextColor ?: FUColorFromHex(0x2C2E30);
  134. self.backgroundColor = [UIColor whiteColor];
  135. } else {
  136. self.textLabel.textColor = self.textColor ?: [UIColor whiteColor];
  137. self.backgroundColor = [UIColor clearColor];
  138. }
  139. }
  140. - (UILabel *)textLabel {
  141. if (!_textLabel) {
  142. _textLabel = [[UILabel alloc] init];
  143. }
  144. return _textLabel;
  145. }
  146. @end