123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- //
- // CloudControlButton.m
- // MidiPlayer
- //
- // Created by Kyle on 2021/12/6.
- //
- #import "CloudControlButton.h"
- @interface CloudControlButton ()
- @property (nonatomic, strong) UILabel *titleLabel;
- @property (nonatomic, strong) UIButton *actionButton;
- @property (nonatomic, copy) ControlAction callback;
- @property (nonatomic, strong) NSString *buttonTitle;
- @property (nonatomic, strong) NSString *selectTitle;
- @property (nonatomic, strong) UILabel *speedLabel;
- @end
- @implementation CloudControlButton
- + (instancetype)createButtonWithImage:(NSString *)imageName selectImage:(NSString *)selectImage buttonTitle:(NSString *)buttonTitle selectTitle:(NSString *)selectTitle tag:(NSInteger)tag frame:(CGRect)frame callback:(ControlAction)callback {
-
- CloudControlButton *controlButton = [[CloudControlButton alloc] initWithFrame:frame];
- controlButton.tag = tag;
- UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
- button.backgroundColor = [UIColor clearColor];
- button.frame = CGRectMake(0, 0, frame.size.width, frame.size.width);
- [button addTarget:controlButton action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
- [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
- [button setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
- [controlButton addSubview:button];
-
- [controlButton addSubview:controlButton.titleLabel];
- controlButton.titleLabel.frame = CGRectMake(0, CGRectGetMaxX(button.frame) + 2, frame.size.width, 24);
- controlButton.actionButton = button;
- controlButton.buttonTitle = buttonTitle;
- controlButton.selectTitle = selectTitle;
-
- controlButton.isSelected = NO;
- if (callback) {
- controlButton.callback = callback;
- }
-
- return controlButton;
- }
- - (void)showSpeed:(NSInteger)speed {
- if (!_speedLabel) {
- UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
- view.backgroundColor = UIColor.whiteColor;
- view.layer.cornerRadius = 8.0f;
- view.layer.borderColor = THEMECOLOR.CGColor;
- view.layer.borderWidth = 1.0f;
- [view addSubview:self.speedLabel];
- [self addSubview:view];
-
- [view mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(self.actionButton.mas_right).offset(-8);
- make.top.mas_equalTo(self.actionButton.mas_top);
- make.height.mas_equalTo(16.0f);
- }];
-
- [self.speedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.mas_equalTo(view.mas_left).offset(5);
- make.right.mas_equalTo(view.mas_right).offset(-5);
- make.centerY.mas_equalTo(view.mas_centerY);
- }];
- }
- else {
-
- }
- self.speedLabel.text = [NSString stringWithFormat:@"%zd", speed];
- }
- - (void)buttonClickAction:(UIButton *)sender {
-
- self.isSelected = !self.isSelected;
- if (self.callback) {
- self.callback(self, self.isSelected);
- }
- }
- #pragma mark ---- setter
- - (void)setIsSelected:(BOOL)isSelected {
-
- _isSelected = isSelected;
- self.actionButton.selected = isSelected;
- if (isSelected) {
- self.titleLabel.text = self.selectTitle;
- }
- else {
- self.titleLabel.text = self.buttonTitle;
- }
- }
- #pragma mark ----- lazying
- - (UILabel *)titleLabel {
-
- if (!_titleLabel) {
- _titleLabel = [[UILabel alloc] init];
- _titleLabel.textColor = UIColor.whiteColor;
- _titleLabel.font = [UIFont systemFontOfSize:12.0f weight:UIFontWeightMedium];
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _titleLabel;
- }
- - (UILabel *)speedLabel {
- if (!_speedLabel) {
- _speedLabel = [[UILabel alloc] init];
- [_speedLabel setFont:[UIFont systemFontOfSize:10.0f]];
- _speedLabel.textColor = THEMECOLOR;
- _speedLabel.textAlignment = NSTextAlignmentCenter;
- }
- return _speedLabel;
- }
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- @end
|