JLChen
2021-11-04 1443556e9ccb1a19ed8e6710c16c8adc4d4f4fb3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
//  Copyright © 2020 dahua. All rights reserved.
//
 
#import "LCDeviceSwitchCell.h"
#import <LCBaseModule/DHActivityIndicatorView.h>
#import "LCUIKit.h"
 
@interface LCDeviceSwitchCell ()
 
/**
 switch控制器
 */
@property (strong,nonatomic)UISwitch * switchControl;
/**
 转圈圈
 */
@property (strong,nonatomic)DHActivityIndicatorView * activityView;
/**
 titleView
 */
@property (strong,nonatomic)UILabel * titleLab;
 
@end
 
@implementation LCDeviceSwitchCell
 
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupView];
    }
    return self;
}
 
-(void)setupView{
    weakSelf(self);
    self.titleLab = [UILabel new];
    [self addSubview:self.titleLab];
    self.titleLab.textColor = [UIColor dhcolor_c40];
    self.titleLab.font = [UIFont lcFont_t3];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.centerY.mas_equalTo(self.mas_centerY);
    }];
    
    self.switchControl = [UISwitch new];
    [self.switchControl addTarget:self action:@selector(switchValueChange:) forControlEvents:UIControlEventValueChanged];
    [self.switchControl setOnTintColor:[UIColor dhcolor_c10]];
    [self addSubview:self.switchControl];
    [self.switchControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakself.mas_centerY);
        make.right.mas_equalTo(weakself.mas_right).offset(-15);
    }];
    self.switchControl.hidden = YES;
    
    self.activityView = [[DHActivityIndicatorView alloc] init];
    [self addSubview:self.activityView];
    [self.activityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.switchControl.mas_centerY);
        make.centerX.mas_equalTo(self.switchControl.mas_centerX);
    }];
    [self.activityView startAnimating];
}
 
-(void)setSwitch:(BOOL)switchValue{
    self.switchControl.on = switchValue;
    self.activityView.hidden = YES;
    self.switchControl.hidden = NO;
}
-(void)switchValueChange:(UISwitch *)switchItem{
    if (self.block) {
        self.block(switchItem.isOn);
    }
}
 
-(void)setTitle:(NSString *)title{
    _title = title;
    [self updateConstraintsIfNeeded];
    self.titleLab.text = title;
}
 
-(void)setEnable:(BOOL)enable{
    self.switchControl.enabled = enable;
}
 
@end