| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- //
- // customSwitchButton.m
- // 双子星云手机
- //
- // Created by xd h on 2026/1/5.
- //
- #import "customSwitchButton.h"
- @interface customSwitchButton ()
- @property (nonatomic, strong) NSArray<UIColor *> *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<UIColor *> *)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
|