UGCKitSlideButton.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2019 Tencent. All rights reserved.
  2. #import "UGCKitSlideButton.h"
  3. @implementation UGCKitSlideButton
  4. {
  5. CGSize _buttonSize;
  6. CGSize _instrinctSize;
  7. CGFloat _spacing;
  8. BOOL _expanded;
  9. }
  10. - (instancetype)initWithButtons:(NSArray<UIButton *> *)buttons buttonSize:(CGSize)size spacing:(CGFloat)spacing
  11. {
  12. self = [super initWithFrame:CGRectZero];
  13. if (self) {
  14. _buttons = buttons;
  15. [buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  16. [obj addTarget:self action:@selector(onButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
  17. [self addSubview:obj];
  18. }];
  19. _buttonSize = size;
  20. _spacing = spacing;
  21. _instrinctSize = CGSizeMake((size.width + _spacing) * _buttons.count - _spacing, size.height);
  22. }
  23. return self;
  24. }
  25. - (void)shrink {
  26. if (!_expanded) {
  27. return;
  28. }
  29. _expanded = !_expanded;
  30. [self switchExpansionAnimated:YES];
  31. }
  32. - (void)awakeFromNib
  33. {
  34. [super awakeFromNib];
  35. [_buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  36. [obj addTarget:self action:@selector(onButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
  37. [self addSubview:obj];
  38. }];
  39. _instrinctSize = CGSizeMake((_size.width + _spacing) * _buttons.count - _spacing, _size.height);
  40. }
  41. - (void)setEnabled:(BOOL)enabled {
  42. [super setEnabled:enabled];
  43. [_buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  44. obj.enabled = enabled;
  45. }];
  46. }
  47. - (void)onButtonTouched:(id)sender {
  48. NSUInteger index = [self.buttons indexOfObject:sender];
  49. if (index == NSNotFound) {
  50. return;
  51. }
  52. _selectedIndex = index;
  53. if (_expanded) {
  54. [self sendActionsForControlEvents:UIControlEventValueChanged];
  55. }
  56. _expanded = !_expanded;
  57. [self switchExpansionAnimated:YES];
  58. }
  59. - (void)layoutSubviews
  60. {
  61. [super layoutSubviews];
  62. [self switchExpansionAnimated:NO];
  63. }
  64. - (CGSize)sizeThatFits:(CGSize)size
  65. {
  66. return _instrinctSize;
  67. }
  68. - (CGSize)intrinsicContentSize {
  69. return _instrinctSize;
  70. }
  71. - (void)switchExpansionAnimated:(BOOL)animated;
  72. {
  73. CGSize buttonSize = _buttonSize;
  74. NSArray<UIButton *> *buttons = _buttons;
  75. BOOL expanded = _expanded;
  76. NSUInteger selectedIndex = _selectedIndex;
  77. CGFloat spacing = _spacing;
  78. void (^action)(void) = ^{
  79. __block CGRect frame = (CGRect){CGPointZero, buttonSize};
  80. [buttons enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  81. if (idx != selectedIndex) {
  82. obj.frame = frame;
  83. frame.origin.x += (buttonSize.width + spacing);
  84. buttons[idx].hidden = !expanded;
  85. } else {
  86. buttons[idx].hidden = NO;
  87. }
  88. }];
  89. buttons[selectedIndex].frame = CGRectMake(CGRectGetWidth(self.bounds) - buttonSize.width, 0, buttonSize.width, buttonSize.height);
  90. };
  91. if (animated) {
  92. [UIView animateWithDuration:0.2 animations:action];
  93. } else {
  94. action();
  95. }
  96. }
  97. - (void)setSelectedIndex:(NSUInteger)selectedIndex
  98. {
  99. if (_selectedIndex == selectedIndex) return;
  100. _selectedIndex = selectedIndex;
  101. [self switchExpansionAnimated:NO];
  102. }
  103. @end