KWSwitchButton.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // KWSwitchButton.m
  3. // westkissMob
  4. //
  5. // Created by iOS on 2022/9/23.
  6. //
  7. #import "KWSwitchButton.h"
  8. @interface KWSwitchButton ()
  9. @property (nonatomic, assign) BOOL isOpen;
  10. @property (nonatomic, strong) UIButton *btn;
  11. @property (nonatomic, strong) UIView *circleV;
  12. @end
  13. @implementation KWSwitchButton
  14. - (void)setIsOpen:(BOOL)isOpen animate:(BOOL)animate {
  15. self.isOpen = isOpen;
  16. [self setStatus:isOpen animate:animate];
  17. }
  18. -(void)setCloseColor:(UIColor *)closeColor {
  19. _closeColor = closeColor;
  20. if (!self.isOpen) {
  21. self.backgroundColor = closeColor;
  22. }
  23. }
  24. - (void)setOpenColor:(UIColor *)openColor {
  25. _openColor = openColor;
  26. if (self.isOpen) {
  27. self.backgroundColor = openColor;
  28. }
  29. }
  30. - (void)setCircleColor:(UIColor *)circleColor {
  31. _circleColor = circleColor;
  32. self.circleV.backgroundColor = circleColor;
  33. }
  34. - (void)setStatus:(BOOL)flag animate:(BOOL)animated {
  35. if (!animated) {
  36. self.backgroundColor = flag ? self.openColor : self.closeColor;
  37. [self.circleV mas_remakeConstraints:^(MASConstraintMaker *make) {
  38. make.top.equalTo(self).offset(2);
  39. make.bottom.equalTo(self).offset(-2);
  40. if (flag) {
  41. make.right.equalTo(self).offset(-2);
  42. } else {
  43. make.left.equalTo(self).offset(2);
  44. }
  45. make.width.equalTo(self.circleV.mas_height).multipliedBy(1);
  46. }];
  47. return;
  48. }
  49. [UIView animateWithDuration:(NSTimeInterval)(0.25) animations:^{
  50. self.backgroundColor = flag ? self.openColor : self.closeColor;
  51. CGRect rect = self.circleV.frame;
  52. if (flag) {
  53. rect.origin.x = self.bounds.size.width - 2 - (self.bounds.size.height-4);
  54. } else {
  55. rect.origin.x = 2;
  56. }
  57. self.circleV.frame = rect;
  58. } completion:^(BOOL finished) {
  59. self.backgroundColor = flag ? self.openColor : self.closeColor;
  60. [self.circleV mas_remakeConstraints:^(MASConstraintMaker *make) {
  61. make.top.equalTo(self).offset(2);
  62. make.bottom.equalTo(self).offset(-2);
  63. if (flag) {
  64. make.right.equalTo(self).offset(-2);
  65. } else {
  66. make.left.equalTo(self).offset(2);
  67. }
  68. make.width.equalTo(self.circleV.mas_height).multipliedBy(1);
  69. }];
  70. }];
  71. }
  72. - (instancetype)init {
  73. self = [super init];
  74. if (self) {
  75. self.frame = CGRectMake(0, 0, 60, 32);
  76. self.closeColor = [UIColor colorWithHexString:@"#E5E5E5"];
  77. self.openColor = [UIColor colorWithHexString:@"#FFDEE2"];
  78. self.circleColor = [UIColor whiteColor];
  79. self.backgroundColor = _openColor;
  80. self.layer.cornerRadius = 16;
  81. self.layer.masksToBounds = true;
  82. [self configSubV];
  83. self.circleV.layer.cornerRadius = 14;
  84. self.isOpen = true;
  85. }
  86. return self;
  87. }
  88. - (void)configSubV {
  89. [self addSubview:self.circleV];
  90. [self addSubview:self.btn];
  91. [self.circleV mas_makeConstraints:^(MASConstraintMaker *make) {
  92. make.top.equalTo(self).offset(2);
  93. make.right.bottom.equalTo(self).offset(-2);
  94. make.width.equalTo(self.circleV.mas_height).multipliedBy(1);
  95. }];
  96. [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.edges.equalTo(self);
  98. }];
  99. }
  100. - (UIButton *)btn {
  101. if (!_btn) {
  102. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  103. [btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
  104. _btn = btn;
  105. }
  106. return _btn;
  107. }
  108. - (void)btnAction {
  109. // self.isOpen = !self.isOpen;
  110. if (self.clickBack) {
  111. self.clickBack();
  112. }
  113. }
  114. - (UIView *)circleV {
  115. if (!_circleV) {
  116. UIView *v = [[UIView alloc] init];
  117. v.layer.masksToBounds = true;
  118. _circleV = v;
  119. }
  120. return _circleV;
  121. }
  122. @end