| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- //
- // CustomSwitch.m
- // AIIM
- //
- // Created by qitewei on 2025/5/22.
- //
- #import "CustomSwitch.h"
- @implementation CustomSwitch
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- [self commonInit];
- }
- return self;
- }
- - (instancetype)initWithCoder:(NSCoder *)coder {
- self = [super initWithCoder:coder];
- if (self) {
- [self commonInit];
- }
- return self;
- }
- - (void)commonInit {
- self.offBackgroundColor = [UIColor lightGrayColor];
- [self addTarget:self action:@selector(updateAppearance) forControlEvents:UIControlEventValueChanged];
- [self updateAppearance];
- }
- - (void)setOffBackgroundColor:(UIColor *)offBackgroundColor {
- _offBackgroundColor = offBackgroundColor;
- [self updateAppearance];
- }
- - (void)updateAppearance {
- if (self.isOn) {
- self.backgroundColor = self.onTintColor;
- } else {
- self.backgroundColor = self.offBackgroundColor;
- }
- self.layer.cornerRadius = CGRectGetHeight(self.bounds) / 2.0;
- self.clipsToBounds = YES;
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
- [self updateAppearance];
- }
- @end
|