customSwitchButton.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // customSwitchButton.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2026/1/5.
  6. //
  7. #import "customSwitchButton.h"
  8. @interface customSwitchButton ()
  9. @property (nonatomic, strong) NSArray<UIColor *> *thumbGradientColors; // 滑块渐变色数组
  10. @property (nonatomic, copy) NSString *onText; // 打开状态文字
  11. @property (nonatomic, copy) NSString *offText; // 关闭状态文字
  12. @property (nonatomic, strong) UIColor *textColor; // 文字颜色
  13. @property (nonatomic, strong) UIFont *textFont; // 文字字体
  14. // 视图组件
  15. @property (nonatomic, strong) UIView *backgroundView;//整体背景
  16. @property (nonatomic, strong) UILabel *onLabel;//开提示语
  17. @property (nonatomic, strong) UILabel *offLabel;//关提示语
  18. @property (nonatomic, strong) UIView *thumbView;//滑块
  19. @property (nonatomic, strong) UILabel *thumbLabel;//滑块
  20. @property (nonatomic, strong) CAGradientLayer *gradientLayer;
  21. @property (nonatomic, strong) UIButton *curButton;//处理点击事件
  22. @property (nonatomic, assign) BOOL didClickType; // 用户点击
  23. @end
  24. @implementation customSwitchButton
  25. // 初始化方法
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. onText:(NSString *)onText
  28. offText:(NSString *)offText
  29. {
  30. self = [super initWithFrame:frame];
  31. if (self) {
  32. // 默认值
  33. _on = NO;
  34. _textColor = [UIColor whiteColor];
  35. _textFont = [UIFont systemFontOfSize:12.0];
  36. // 设置文字
  37. _onText = onText ?: @"ON";
  38. _offText = offText ?: @"OFF";
  39. // 设置渐变色
  40. // 默认渐变色:蓝到紫
  41. _thumbGradientColors = @[
  42. [UIColor hwColor:@"#0CDEFD" alpha:1.0],
  43. [UIColor hwColor:@"#058DFB" alpha:1.0]
  44. ];
  45. [self setupUI];
  46. }
  47. return self;
  48. }
  49. - (void)setupUI {
  50. // 背景视图
  51. self.backgroundView = [[UIView alloc] init];
  52. self.backgroundView.layer.cornerRadius = 6;
  53. self.backgroundView.layer.masksToBounds = YES;
  54. self.backgroundView.backgroundColor = [UIColor hwColor:@"#43536B" alpha:1.0];
  55. [self addSubview:self.backgroundView];
  56. // 打开状态文字
  57. self.onLabel = [[UILabel alloc] init];
  58. self.onLabel.text = self.onText;
  59. self.onLabel.textColor = self.textColor;
  60. self.onLabel.font = self.textFont;
  61. self.onLabel.textAlignment = NSTextAlignmentCenter;
  62. self.onLabel.alpha = 1.0;
  63. [self.backgroundView addSubview:self.onLabel];
  64. // 关闭状态文字
  65. self.offLabel = [[UILabel alloc] init];
  66. self.offLabel.text = self.offText;
  67. self.offLabel.textColor = self.textColor;
  68. self.offLabel.font = self.textFont;
  69. self.offLabel.textAlignment = NSTextAlignmentCenter;
  70. self.offLabel.alpha = 1.0;
  71. [self.backgroundView addSubview:self.offLabel];
  72. // 滑块视图
  73. self.thumbView = [[UIView alloc] init];
  74. self.thumbView.layer.cornerRadius = 6.0;
  75. self.thumbView.layer.masksToBounds = YES;
  76. [self addSubview:self.thumbView];
  77. // 滑块渐变色层
  78. self.gradientLayer = [CAGradientLayer layer];
  79. self.gradientLayer.cornerRadius = 6.0;
  80. self.gradientLayer.masksToBounds = YES;
  81. // 设置渐变方向为左上到右下
  82. self.gradientLayer.startPoint = CGPointMake(0, 0.5);
  83. self.gradientLayer.endPoint = CGPointMake(0.97, 0.5);
  84. // 设置渐变色
  85. NSMutableArray *cgColors = [NSMutableArray array];
  86. for (UIColor *color in self.thumbGradientColors) {
  87. [cgColors addObject:(id)color.CGColor];
  88. }
  89. self.gradientLayer.colors = cgColors;
  90. [self.thumbView.layer insertSublayer:self.gradientLayer atIndex:0];
  91. // 滑块上的文字
  92. self.thumbLabel = [[UILabel alloc] init];
  93. //self.thumbLabel.text = self.offText;
  94. self.thumbLabel.textColor = self.textColor;
  95. self.thumbLabel.font = self.textFont;
  96. self.thumbLabel.textAlignment = NSTextAlignmentCenter;
  97. [self.thumbView addSubview:self.thumbLabel];
  98. if (_on) {
  99. self.thumbLabel.text = self.onText;
  100. }
  101. else{
  102. self.thumbLabel.text = self.offText;
  103. }
  104. self.curButton = [[UIButton alloc] init];
  105. [self.curButton addTarget:self action:@selector(didClickButtonFun:) forControlEvents:UIControlEventTouchUpInside];
  106. [self addSubview:self.curButton];
  107. }
  108. - (void)layoutSubviews {
  109. [super layoutSubviews];
  110. // 设置背景视图
  111. self.backgroundView.frame = self.bounds;
  112. // 设置文字标签位置
  113. CGFloat labelWidth = self.frame.size.width/2.0;
  114. self.onLabel.frame = CGRectMake(0, 0, labelWidth, self.frame.size.height);
  115. self.offLabel.frame = CGRectMake(labelWidth, 0, labelWidth, self.frame.size.height);
  116. // 设置滑块位置
  117. if (_on) {
  118. self.thumbView.frame = self.onLabel.frame;
  119. }
  120. else{
  121. self.thumbView.frame = self.offLabel.frame;
  122. }
  123. // 设置渐变色层
  124. self.gradientLayer.frame = self.thumbView.bounds;
  125. // 设置滑块文字
  126. self.thumbLabel.frame = self.thumbView.bounds;
  127. self.curButton.frame = self.bounds;
  128. }
  129. #pragma mark - 点击处理
  130. - (void)didClickButtonFun:(UIButton*)button
  131. {
  132. _didClickType = YES;
  133. self.on = !self.on;
  134. }
  135. #pragma mark - 公共方法
  136. - (void)setOn:(BOOL)on {
  137. [self setOn:on animated:NO];
  138. }
  139. - (void)setOn:(BOOL)on animated:(BOOL)animated {
  140. //if (_on == on) return;
  141. _on = on;
  142. self.thumbLabel.text = on ? self.onText : self.offText;
  143. if (animated) {
  144. // self.isAnimating = YES;
  145. // [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
  146. // self.thumbView.frame = on ? self.thumbOnPosition : self.thumbOffPosition;
  147. // self.backgroundView.backgroundColor = on ? self.onTintColor : self.offTintColor;
  148. // self.onLabel.alpha = on ? 1.0 : 0.0;
  149. // self.offLabel.alpha = on ? 0.0 : 1.0;
  150. // } completion:^(BOOL finished) {
  151. // self.isAnimating = NO;
  152. // }];
  153. } else {
  154. self.thumbView.frame = on ? self.onLabel.frame : self.offLabel.frame;
  155. //self.backgroundView.backgroundColor = on ? self.onTintColor : self.offTintColor;
  156. self.onLabel.alpha = on ? 0.0 : 1.0;
  157. self.offLabel.alpha = on ? 1.0 : 0.0;
  158. }
  159. if(_didClickSwitchButtonFun && _didClickType){
  160. _didClickSwitchButtonFun(_on);
  161. }
  162. _didClickType = NO;
  163. }
  164. - (void)setOnText:(NSString *)onText {
  165. _onText = onText;
  166. self.onLabel.text = onText;
  167. if (self.isOn) {
  168. self.thumbLabel.text = onText;
  169. }
  170. }
  171. - (void)setOffText:(NSString *)offText {
  172. _offText = offText;
  173. self.offLabel.text = offText;
  174. if (!self.isOn) {
  175. self.thumbLabel.text = offText;
  176. }
  177. }
  178. - (void)setEnabled:(BOOL)enabled
  179. {
  180. if (enabled) {
  181. self.textColor = [UIColor hwColor:@"#FFFFFF" alpha:1.0];
  182. // 设置渐变色
  183. self.gradientLayer.colors = @[(__bridge NSString *)[UIColor hwColor:@"#0CDEFD" alpha:1.0].CGColor, (__bridge NSString *)[UIColor hwColor:@"#058DFB" alpha:1.0].CGColor];
  184. }
  185. else{
  186. self.textColor = [UIColor hwColor:@"#FFFFFF" alpha:0.2];
  187. self.gradientLayer.colors = @[(__bridge NSString *)[UIColor hwColor:@"#485C78" alpha:1.0].CGColor, (__bridge NSString *)[UIColor hwColor:@"#40597F" alpha:1.0].CGColor];
  188. }
  189. self.curButton.userInteractionEnabled = enabled;
  190. }
  191. - (void)setOnTintColor:(UIColor *)onTintColor {
  192. // _onTintColor = onTintColor;
  193. // if (self.isOn) {
  194. // self.backgroundView.backgroundColor = onTintColor;
  195. // }
  196. }
  197. - (void)setOffTintColor:(UIColor *)offTintColor {
  198. // _offTintColor = offTintColor;
  199. // if (!self.isOn) {
  200. // self.backgroundView.backgroundColor = offTintColor;
  201. // }
  202. }
  203. - (void)setThumbGradientColors:(NSArray<UIColor *> *)thumbGradientColors {
  204. // _thumbGradientColors = thumbGradientColors;
  205. //
  206. // if (thumbGradientColors && thumbGradientColors.count > 0) {
  207. // NSMutableArray *cgColors = [NSMutableArray array];
  208. // for (UIColor *color in thumbGradientColors) {
  209. // [cgColors addObject:(id)color.CGColor];
  210. // }
  211. // self.gradientLayer.colors = cgColors;
  212. // }
  213. }
  214. - (void)setTextColor:(UIColor *)textColor {
  215. _textColor = textColor;
  216. self.onLabel.textColor = textColor;
  217. self.offLabel.textColor = textColor;
  218. }
  219. - (void)setTextFont:(UIFont *)textFont {
  220. _textFont = textFont;
  221. self.onLabel.font = textFont;
  222. self.offLabel.font = textFont;
  223. self.thumbLabel.font = textFont;
  224. }
  225. @end