XMWallSwitchView.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //
  2. // XMWallSwitchView.m
  3. // XWorld_General
  4. //
  5. // Created by SaturdayNight on 2018/10/22.
  6. // Copyright © 2018年 xiongmaitech. All rights reserved.
  7. //
  8. #import "XMWallSwitchView.h"
  9. #import <Masonry/Masonry.h>
  10. @implementation XMWallSwitchView
  11. - (instancetype)initWithFrame:(CGRect)frame{
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. [self addSubview:self.btnOne];
  15. [self addSubview:self.btnTwo];
  16. [self addSubview:self.btnThree];
  17. [self myLayout];
  18. }
  19. return self;
  20. }
  21. - (void)myLayout{
  22. [self.btnOne mas_makeConstraints:^(MASConstraintMaker *make) {
  23. make.left.equalTo(self);
  24. make.width.equalTo(self.mas_height).multipliedBy(0.75);
  25. make.height.equalTo(self.mas_height).multipliedBy(1);
  26. make.centerY.equalTo(self);
  27. }];
  28. [self.btnTwo mas_makeConstraints:^(MASConstraintMaker *make) {
  29. make.left.equalTo(self.btnOne.mas_right).mas_offset(5);
  30. make.width.equalTo(self.mas_height).multipliedBy(0.75);
  31. make.height.equalTo(self.mas_height).multipliedBy(1);
  32. make.centerY.equalTo(self);
  33. }];
  34. [self.btnThree mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.left.equalTo(self.btnTwo.mas_right).mas_offset(5);
  36. make.width.equalTo(self.mas_height).multipliedBy(0.75);
  37. make.height.equalTo(self.mas_height).multipliedBy(1);
  38. make.centerY.equalTo(self);
  39. }];
  40. }
  41. - (void)btnClicked:(UIButton *)btn{
  42. btn.selected = !btn.selected;
  43. if (self.WallSwitchClickedAction) {
  44. self.WallSwitchClickedAction(btn.tag, btn.selected);
  45. }
  46. }
  47. - (UIButton *)btnOne{
  48. if (!_btnOne) {
  49. _btnOne = [[UIButton alloc] init];
  50. _btnOne.tag = 1;
  51. [_btnOne setBackgroundImage:[UIImage imageNamed:@"wallswitch_off"] forState:UIControlStateNormal];
  52. [_btnOne setBackgroundImage:[UIImage imageNamed:@"wallswitch_on"] forState:UIControlStateSelected];
  53. [_btnOne addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
  54. }
  55. return _btnOne;
  56. }
  57. - (UIButton *)btnTwo{
  58. if (!_btnTwo) {
  59. _btnTwo = [[UIButton alloc] init];
  60. _btnTwo.tag = 2;
  61. [_btnTwo setBackgroundImage:[UIImage imageNamed:@"wallswitch_off"] forState:UIControlStateNormal];
  62. [_btnTwo setBackgroundImage:[UIImage imageNamed:@"wallswitch_on"] forState:UIControlStateSelected];
  63. [_btnTwo addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
  64. }
  65. return _btnTwo;
  66. }
  67. - (UIButton *)btnThree{
  68. if (!_btnThree) {
  69. _btnThree = [[UIButton alloc] init];
  70. _btnThree.tag = 3;
  71. [_btnThree setBackgroundImage:[UIImage imageNamed:@"wallswitch_off"] forState:UIControlStateNormal];
  72. [_btnThree setBackgroundImage:[UIImage imageNamed:@"wallswitch_on"] forState:UIControlStateSelected];
  73. [_btnThree addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
  74. }
  75. return _btnThree;
  76. }
  77. @end