CloudControlButton.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // CloudControlButton.m
  3. // MidiPlayer
  4. //
  5. // Created by Kyle on 2021/12/6.
  6. //
  7. #import "CloudControlButton.h"
  8. @interface CloudControlButton ()
  9. @property (nonatomic, strong) UILabel *titleLabel;
  10. @property (nonatomic, strong) UIButton *actionButton;
  11. @property (nonatomic, copy) ControlAction callback;
  12. @property (nonatomic, strong) NSString *buttonTitle;
  13. @property (nonatomic, strong) NSString *selectTitle;
  14. @property (nonatomic, strong) UILabel *speedLabel;
  15. @end
  16. @implementation CloudControlButton
  17. + (instancetype)createButtonWithImage:(NSString *)imageName selectImage:(NSString *)selectImage buttonTitle:(NSString *)buttonTitle selectTitle:(NSString *)selectTitle tag:(NSInteger)tag frame:(CGRect)frame callback:(ControlAction)callback {
  18. CloudControlButton *controlButton = [[CloudControlButton alloc] initWithFrame:frame];
  19. controlButton.tag = tag;
  20. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
  21. button.backgroundColor = [UIColor clearColor];
  22. button.frame = CGRectMake(0, 0, frame.size.width, frame.size.width);
  23. [button addTarget:controlButton action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
  24. [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
  25. [button setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
  26. [controlButton addSubview:button];
  27. [controlButton addSubview:controlButton.titleLabel];
  28. controlButton.titleLabel.frame = CGRectMake(0, CGRectGetMaxX(button.frame) + 2, frame.size.width, 24);
  29. controlButton.actionButton = button;
  30. controlButton.buttonTitle = buttonTitle;
  31. controlButton.selectTitle = selectTitle;
  32. controlButton.isSelected = NO;
  33. if (callback) {
  34. controlButton.callback = callback;
  35. }
  36. return controlButton;
  37. }
  38. - (void)showSpeed:(NSInteger)speed {
  39. if (!_speedLabel) {
  40. UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  41. view.backgroundColor = UIColor.whiteColor;
  42. view.layer.cornerRadius = 8.0f;
  43. view.layer.borderColor = THEMECOLOR.CGColor;
  44. view.layer.borderWidth = 1.0f;
  45. [view addSubview:self.speedLabel];
  46. [self addSubview:view];
  47. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  48. make.left.mas_equalTo(self.actionButton.mas_right).offset(-8);
  49. make.top.mas_equalTo(self.actionButton.mas_top);
  50. make.height.mas_equalTo(16.0f);
  51. }];
  52. [self.speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.left.mas_equalTo(view.mas_left).offset(5);
  54. make.right.mas_equalTo(view.mas_right).offset(-5);
  55. make.centerY.mas_equalTo(view.mas_centerY);
  56. }];
  57. }
  58. else {
  59. }
  60. self.speedLabel.text = [NSString stringWithFormat:@"%zd", speed];
  61. }
  62. - (void)buttonClickAction:(UIButton *)sender {
  63. self.isSelected = !self.isSelected;
  64. if (self.callback) {
  65. self.callback(self, self.isSelected);
  66. }
  67. }
  68. #pragma mark ---- setter
  69. - (void)setIsSelected:(BOOL)isSelected {
  70. _isSelected = isSelected;
  71. self.actionButton.selected = isSelected;
  72. if (isSelected) {
  73. self.titleLabel.text = self.selectTitle;
  74. }
  75. else {
  76. self.titleLabel.text = self.buttonTitle;
  77. }
  78. }
  79. #pragma mark ----- lazying
  80. - (UILabel *)titleLabel {
  81. if (!_titleLabel) {
  82. _titleLabel = [[UILabel alloc] init];
  83. _titleLabel.textColor = UIColor.whiteColor;
  84. _titleLabel.font = [UIFont systemFontOfSize:12.0f weight:UIFontWeightMedium];
  85. _titleLabel.textAlignment = NSTextAlignmentCenter;
  86. }
  87. return _titleLabel;
  88. }
  89. - (UILabel *)speedLabel {
  90. if (!_speedLabel) {
  91. _speedLabel = [[UILabel alloc] init];
  92. [_speedLabel setFont:[UIFont systemFontOfSize:10.0f]];
  93. _speedLabel.textColor = THEMECOLOR;
  94. _speedLabel.textAlignment = NSTextAlignmentCenter;
  95. }
  96. return _speedLabel;
  97. }
  98. /*
  99. // Only override drawRect: if you perform custom drawing.
  100. // An empty implementation adversely affects performance during animation.
  101. - (void)drawRect:(CGRect)rect {
  102. // Drawing code
  103. }
  104. */
  105. @end