UGCKitSlideOptionControl.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitSlideOptionControl.h"
  3. @implementation UGCKitSlideOptionControl
  4. {
  5. NSArray<NSString *> *_options;
  6. NSArray<UILabel *> *_labels;
  7. UIImageView *_indicatorView;
  8. }
  9. - (instancetype)initWithFrame:(CGRect)frame theme:(UGCKitTheme*)theme options:(NSArray<NSString *> *)options {
  10. if (self = [super initWithFrame:frame]) {
  11. _options = [options copy];
  12. NSMutableArray *labels = [NSMutableArray arrayWithCapacity:options.count];
  13. for (NSString *str in _options) {
  14. UILabel *label = [[UILabel alloc] init];
  15. label.textColor = [theme titleColor];
  16. label.text = str;
  17. [labels addObject:label];
  18. [label sizeToFit];
  19. [self addSubview:label];
  20. }
  21. _labels = labels;
  22. _indicatorView = [[UIImageView alloc] initWithImage:theme.recordButtonModeSwitchIndicatorIcon];
  23. [self addSubview:_indicatorView];
  24. }
  25. return self;
  26. }
  27. - (void)layoutSubviews {
  28. [super layoutSubviews];
  29. [self doLayoutSubviews];
  30. }
  31. - (void)setDisabledIndexes:(NSIndexSet *)disabledIndexes {
  32. _disabledIndexes = [disabledIndexes copy];
  33. [disabledIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  34. if (idx < self->_labels.count) {
  35. self->_labels[idx].hidden = YES;
  36. }
  37. }];
  38. }
  39. - (void)doLayoutSubviews {
  40. if (_selectedIndex >= _labels.count) {
  41. return;
  42. }
  43. const CGFloat centerY = (CGRectGetHeight(self.bounds) - 10 - CGRectGetHeight(_indicatorView.bounds))/2;
  44. const CGFloat centerX = CGRectGetMidX(self.bounds);
  45. UILabel *centerLabel = _labels[_selectedIndex];
  46. centerLabel.center = CGPointMake(centerX, centerY);
  47. CGFloat x = centerX - CGRectGetMidX(centerLabel.bounds) - 30;
  48. for (NSInteger i = _selectedIndex-1; i >= 0; -- i) {
  49. UILabel *label = _labels[i];
  50. x -= CGRectGetMidX(label.bounds);
  51. label.center = CGPointMake(x, centerY);
  52. x -= (CGRectGetMidX(label.bounds) + 30);
  53. }
  54. x = centerX + CGRectGetMidX(centerLabel.bounds) + 30;
  55. for (NSInteger i = _selectedIndex+1; i < _labels.count; ++ i) {
  56. UILabel *label = _labels[i];
  57. x += CGRectGetMidX(label.bounds);
  58. label.center = CGPointMake(x, centerY);
  59. x += (CGRectGetMidX(label.bounds) + 30);
  60. }
  61. _indicatorView.center = CGPointMake(centerX, CGRectGetHeight(self.bounds) - CGRectGetMidY(_indicatorView.bounds) - 10);
  62. }
  63. - (void)setCurrentIndex:(NSUInteger)index {
  64. [self setCurrentIndex:index animated:NO];
  65. }
  66. - (void)setCurrentIndex:(NSUInteger)index animated:(BOOL)animated {
  67. _selectedIndex = index;
  68. if (animated) {
  69. [UIView animateWithDuration:0.2 animations:^{
  70. [self doLayoutSubviews];
  71. }];
  72. } else {
  73. [self setNeedsLayout];
  74. }
  75. }
  76. - (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event
  77. {
  78. CGPoint location = [touch locationInView:self];
  79. NSUInteger index = 0;
  80. for (UILabel *label in _labels) {
  81. CGRect frame = label.frame;
  82. if (location.x >= CGRectGetMinX(frame) && location.x <= CGRectGetMaxX(frame)) {
  83. if (![_disabledIndexes containsIndex:index]) {
  84. [self setCurrentIndex:index animated:YES];
  85. [self sendActionsForControlEvents:UIControlEventValueChanged];
  86. }
  87. break;
  88. }
  89. ++index;
  90. }
  91. }
  92. @end