123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //
- // KWSwitchButton.m
- // westkissMob
- //
- // Created by iOS on 2022/9/23.
- //
- #import "KWSwitchButton.h"
- @interface KWSwitchButton ()
- @property (nonatomic, assign) BOOL isOpen;
- @property (nonatomic, strong) UIButton *btn;
- @property (nonatomic, strong) UIView *circleV;
- @end
- @implementation KWSwitchButton
- - (void)setIsOpen:(BOOL)isOpen animate:(BOOL)animate {
- self.isOpen = isOpen;
- [self setStatus:isOpen animate:animate];
-
- }
- -(void)setCloseColor:(UIColor *)closeColor {
- _closeColor = closeColor;
- if (!self.isOpen) {
- self.backgroundColor = closeColor;
- }
- }
- - (void)setOpenColor:(UIColor *)openColor {
- _openColor = openColor;
- if (self.isOpen) {
- self.backgroundColor = openColor;
- }
- }
- - (void)setCircleColor:(UIColor *)circleColor {
- _circleColor = circleColor;
- self.circleV.backgroundColor = circleColor;
- }
- - (void)setStatus:(BOOL)flag animate:(BOOL)animated {
- if (!animated) {
- self.backgroundColor = flag ? self.openColor : self.closeColor;
- [self.circleV mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(2);
- make.bottom.equalTo(self).offset(-2);
- if (flag) {
- make.right.equalTo(self).offset(-2);
- } else {
- make.left.equalTo(self).offset(2);
- }
- make.width.equalTo(self.circleV.mas_height).multipliedBy(1);
- }];
- return;
- }
- [UIView animateWithDuration:(NSTimeInterval)(0.25) animations:^{
- self.backgroundColor = flag ? self.openColor : self.closeColor;
- CGRect rect = self.circleV.frame;
- if (flag) {
- rect.origin.x = self.bounds.size.width - 2 - (self.bounds.size.height-4);
- } else {
- rect.origin.x = 2;
- }
- self.circleV.frame = rect;
- } completion:^(BOOL finished) {
- self.backgroundColor = flag ? self.openColor : self.closeColor;
- [self.circleV mas_remakeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(2);
- make.bottom.equalTo(self).offset(-2);
- if (flag) {
- make.right.equalTo(self).offset(-2);
- } else {
- make.left.equalTo(self).offset(2);
- }
- make.width.equalTo(self.circleV.mas_height).multipliedBy(1);
- }];
- }];
- }
-
- - (instancetype)init {
- self = [super init];
- if (self) {
- self.frame = CGRectMake(0, 0, 60, 32);
- self.closeColor = [UIColor colorWithHexString:@"#E5E5E5"];
- self.openColor = [UIColor colorWithHexString:@"#FFDEE2"];
- self.circleColor = [UIColor whiteColor];
- self.backgroundColor = _openColor;
- self.layer.cornerRadius = 16;
- self.layer.masksToBounds = true;
-
- [self configSubV];
-
- self.circleV.layer.cornerRadius = 14;
- self.isOpen = true;
- }
- return self;
- }
- - (void)configSubV {
- [self addSubview:self.circleV];
- [self addSubview:self.btn];
-
- [self.circleV mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self).offset(2);
- make.right.bottom.equalTo(self).offset(-2);
- make.width.equalTo(self.circleV.mas_height).multipliedBy(1);
- }];
- [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.edges.equalTo(self);
- }];
- }
- - (UIButton *)btn {
- if (!_btn) {
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
- [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
- _btn = btn;
- }
- return _btn;
- }
- - (void)btnAction {
- // self.isOpen = !self.isOpen;
- if (self.clickBack) {
- self.clickBack();
- }
- }
- - (UIView *)circleV {
- if (!_circleV) {
- UIView *v = [[UIView alloc] init];
- v.layer.masksToBounds = true;
- _circleV = v;
- }
- return _circleV;
- }
- @end
|