// // customSwitchButton.m // 双子星云手机 // // Created by xd h on 2026/1/5. // #import "customSwitchButton.h" @interface customSwitchButton () @property (nonatomic, strong) NSArray *thumbGradientColors; // 滑块渐变色数组 @property (nonatomic, copy) NSString *onText; // 打开状态文字 @property (nonatomic, copy) NSString *offText; // 关闭状态文字 @property (nonatomic, strong) UIColor *textColor; // 文字颜色 @property (nonatomic, strong) UIFont *textFont; // 文字字体 // 视图组件 @property (nonatomic, strong) UIView *backgroundView;//整体背景 @property (nonatomic, strong) UILabel *onLabel;//开提示语 @property (nonatomic, strong) UILabel *offLabel;//关提示语 @property (nonatomic, strong) UIView *thumbView;//滑块 @property (nonatomic, strong) UILabel *thumbLabel;//滑块 @property (nonatomic, strong) CAGradientLayer *gradientLayer; @property (nonatomic, strong) UIButton *curButton;//处理点击事件 @property (nonatomic, assign) BOOL didClickType; // 用户点击 @end @implementation customSwitchButton // 初始化方法 - (instancetype)initWithFrame:(CGRect)frame onText:(NSString *)onText offText:(NSString *)offText { self = [super initWithFrame:frame]; if (self) { // 默认值 _on = NO; _textColor = [UIColor whiteColor]; _textFont = [UIFont systemFontOfSize:12.0]; // 设置文字 _onText = onText ?: @"ON"; _offText = offText ?: @"OFF"; // 设置渐变色 // 默认渐变色:蓝到紫 _thumbGradientColors = @[ [UIColor hwColor:@"#0CDEFD" alpha:1.0], [UIColor hwColor:@"#058DFB" alpha:1.0] ]; [self setupUI]; } return self; } - (void)setupUI { // 背景视图 self.backgroundView = [[UIView alloc] init]; self.backgroundView.layer.cornerRadius = 6; self.backgroundView.layer.masksToBounds = YES; self.backgroundView.backgroundColor = [UIColor hwColor:@"#43536B" alpha:1.0]; [self addSubview:self.backgroundView]; // 打开状态文字 self.onLabel = [[UILabel alloc] init]; self.onLabel.text = self.onText; self.onLabel.textColor = self.textColor; self.onLabel.font = self.textFont; self.onLabel.textAlignment = NSTextAlignmentCenter; self.onLabel.alpha = 1.0; [self.backgroundView addSubview:self.onLabel]; // 关闭状态文字 self.offLabel = [[UILabel alloc] init]; self.offLabel.text = self.offText; self.offLabel.textColor = self.textColor; self.offLabel.font = self.textFont; self.offLabel.textAlignment = NSTextAlignmentCenter; self.offLabel.alpha = 1.0; [self.backgroundView addSubview:self.offLabel]; // 滑块视图 self.thumbView = [[UIView alloc] init]; self.thumbView.layer.cornerRadius = 6.0; self.thumbView.layer.masksToBounds = YES; [self addSubview:self.thumbView]; // 滑块渐变色层 self.gradientLayer = [CAGradientLayer layer]; self.gradientLayer.cornerRadius = 6.0; self.gradientLayer.masksToBounds = YES; // 设置渐变方向为左上到右下 self.gradientLayer.startPoint = CGPointMake(0, 0.5); self.gradientLayer.endPoint = CGPointMake(0.97, 0.5); // 设置渐变色 NSMutableArray *cgColors = [NSMutableArray array]; for (UIColor *color in self.thumbGradientColors) { [cgColors addObject:(id)color.CGColor]; } self.gradientLayer.colors = cgColors; [self.thumbView.layer insertSublayer:self.gradientLayer atIndex:0]; // 滑块上的文字 self.thumbLabel = [[UILabel alloc] init]; //self.thumbLabel.text = self.offText; self.thumbLabel.textColor = self.textColor; self.thumbLabel.font = self.textFont; self.thumbLabel.textAlignment = NSTextAlignmentCenter; [self.thumbView addSubview:self.thumbLabel]; if (_on) { self.thumbLabel.text = self.onText; } else{ self.thumbLabel.text = self.offText; } self.curButton = [[UIButton alloc] init]; [self.curButton addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:self.curButton]; } - (void)layoutSubviews { [super layoutSubviews]; // 设置背景视图 self.backgroundView.frame = self.bounds; // 设置文字标签位置 CGFloat labelWidth = self.frame.size.width/2.0; self.onLabel.frame = CGRectMake(0, 0, labelWidth, self.frame.size.height); self.offLabel.frame = CGRectMake(labelWidth, 0, labelWidth, self.frame.size.height); // 设置滑块位置 if (_on) { self.thumbView.frame = self.onLabel.frame; } else{ self.thumbView.frame = self.offLabel.frame; } // 设置渐变色层 self.gradientLayer.frame = self.thumbView.bounds; // 设置滑块文字 self.thumbLabel.frame = self.thumbView.bounds; self.curButton.frame = self.bounds; } #pragma mark - 点击处理 - (void)didClickButtonFun:(UIButton*)button { _didClickType = YES; self.on = !self.on; } #pragma mark - 公共方法 - (void)setOn:(BOOL)on { [self setOn:on animated:NO]; } - (void)setOn:(BOOL)on animated:(BOOL)animated { //if (_on == on) return; _on = on; self.thumbLabel.text = on ? self.onText : self.offText; if (animated) { // self.isAnimating = YES; // [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ // self.thumbView.frame = on ? self.thumbOnPosition : self.thumbOffPosition; // self.backgroundView.backgroundColor = on ? self.onTintColor : self.offTintColor; // self.onLabel.alpha = on ? 1.0 : 0.0; // self.offLabel.alpha = on ? 0.0 : 1.0; // } completion:^(BOOL finished) { // self.isAnimating = NO; // }]; } else { self.thumbView.frame = on ? self.onLabel.frame : self.offLabel.frame; //self.backgroundView.backgroundColor = on ? self.onTintColor : self.offTintColor; self.onLabel.alpha = on ? 0.0 : 1.0; self.offLabel.alpha = on ? 1.0 : 0.0; } if(_didClickSwitchButtonFun && _didClickType){ _didClickSwitchButtonFun(_on); } _didClickType = NO; } - (void)setOnText:(NSString *)onText { _onText = onText; self.onLabel.text = onText; if (self.isOn) { self.thumbLabel.text = onText; } } - (void)setOffText:(NSString *)offText { _offText = offText; self.offLabel.text = offText; if (!self.isOn) { self.thumbLabel.text = offText; } } - (void)setEnabled:(BOOL)enabled { if (enabled) { self.textColor = [UIColor hwColor:@"#FFFFFF" alpha:1.0]; // 设置渐变色 self.gradientLayer.colors = @[(__bridge NSString *)[UIColor hwColor:@"#0CDEFD" alpha:1.0].CGColor, (__bridge NSString *)[UIColor hwColor:@"#058DFB" alpha:1.0].CGColor]; } else{ self.textColor = [UIColor hwColor:@"#FFFFFF" alpha:0.2]; self.gradientLayer.colors = @[(__bridge NSString *)[UIColor hwColor:@"#485C78" alpha:1.0].CGColor, (__bridge NSString *)[UIColor hwColor:@"#40597F" alpha:1.0].CGColor]; } self.curButton.userInteractionEnabled = enabled; } - (void)setOnTintColor:(UIColor *)onTintColor { // _onTintColor = onTintColor; // if (self.isOn) { // self.backgroundView.backgroundColor = onTintColor; // } } - (void)setOffTintColor:(UIColor *)offTintColor { // _offTintColor = offTintColor; // if (!self.isOn) { // self.backgroundView.backgroundColor = offTintColor; // } } - (void)setThumbGradientColors:(NSArray *)thumbGradientColors { // _thumbGradientColors = thumbGradientColors; // // if (thumbGradientColors && thumbGradientColors.count > 0) { // NSMutableArray *cgColors = [NSMutableArray array]; // for (UIColor *color in thumbGradientColors) { // [cgColors addObject:(id)color.CGColor]; // } // self.gradientLayer.colors = cgColors; // } } - (void)setTextColor:(UIColor *)textColor { _textColor = textColor; self.onLabel.textColor = textColor; self.offLabel.textColor = textColor; } - (void)setTextFont:(UIFont *)textFont { _textFont = textFont; self.onLabel.font = textFont; self.offLabel.font = textFont; self.thumbLabel.font = textFont; } @end