单军华
2018-03-29 89d748b77b478905732e60f0b4c5807c274b6565
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
//  RadioButton.m
//  GoldRich
//
//  Created by WindShan on 2017/2/15.
//  Copyright © 2017年 WindShan. All rights reserved.
//
 
#import "RadioButton.h"
 
#define RADIO_ICON_WH                     (50.0)
#define RADIO_ICON_WH_5                     (40.0)
#define ICON_TITLE_MARGIN                 (5.0)
 
 
@implementation RadioButton
 
static NSMutableDictionary *_groupRadioDic = nil;
 
 
@synthesize delegate = _delegate;
@synthesize checked  = _checked;
 
- (id)initWithDelegate:(id)delegate groupId:(NSString*)groupId
{
    self = [super init];
    if (self) {
        _delegate = delegate;
        _groupId = [groupId copy];
        
        [self addToGroup];
        
        self.exclusiveTouch = YES;
        
        [self addTarget:self action:@selector(radioBtnChecked) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
 
- (void)setImageName:(NSString *)normalName selImage:(NSString *)selName
{
    [self setImage:[UIImage imageNamed:normalName] forState:UIControlStateNormal];
    [self setImage:[UIImage imageNamed:selName] forState:UIControlStateSelected];
}
 
- (void)addToGroup
{
    if(!_groupRadioDic)
    {
        _groupRadioDic = [[NSMutableDictionary alloc] init];
    }
    
    NSMutableArray *_gRadios = [_groupRadioDic objectForKey:_groupId];
    if (!_gRadios)
    {
        _gRadios = [NSMutableArray array];
    }
    
    [_gRadios addObject:self];
    [_groupRadioDic setObject:_gRadios forKey:_groupId];
}
 
- (void)removeFromGroup
{
    if (_groupRadioDic)
    {
        NSMutableArray *_gRadios = [_groupRadioDic objectForKey:_groupId];
        if (_gRadios)
        {
            [_gRadios removeObject:self];
            if (_gRadios.count == 0)
            {
                [_groupRadioDic removeObjectForKey:_groupId];
            }
        }
    }
}
 
- (void)uncheckOtherRadios
{
    NSMutableArray *_gRadios = [_groupRadioDic objectForKey:_groupId];
    if (_gRadios.count > 0)
    {
        for (RadioButton *_radio in _gRadios)
        {
            if (_radio.checked && ![_radio isEqual:self])
            {
                _radio.checked = NO;
            }
        }
    }
}
 
- (void)setChecked:(BOOL)checked
{
    if (_checked == checked)
    {
        return;
    }
    
    _checked = checked;
    self.selected = checked;
    
    if (self.selected)
    {
        [self uncheckOtherRadios];
    }
    
    if (self.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectedRadioButton:groupId:)])
    {
        [_delegate didSelectedRadioButton:self groupId:_groupId];
    }
}
 
- (void)radioBtnChecked
{
    if (_checked)
    {
        return;
    }
    
    self.selected = !self.selected;
    _checked = self.selected;
    
    if (self.selected)
    {
        [self uncheckOtherRadios];
    }
    
    if (self.selected && _delegate && [_delegate respondsToSelector:@selector(didSelectedRadioButton:groupId:)])
    {
        [_delegate didSelectedRadioButton:self groupId:_groupId];
        
    }
}
 
//- (CGRect)imageRectForContentRect:(CGRect)contentRect
//{
 //   if(IsiPhone4 || IsiPhone5)
//        return CGRectMake(0, (CGRectGetHeight(contentRect) - RADIO_ICON_WH_5)/2.0, RADIO_ICON_WH_5, RADIO_ICON_WH_5);
//    else
//        return CGRectMake(0, (CGRectGetHeight(contentRect) - RADIO_ICON_WH)/2.0, RADIO_ICON_WH, RADIO_ICON_WH);
//}
 
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    if(IsiPhone4 || IsiPhone5)
        return CGRectMake(0, (CGRectGetHeight(contentRect) - RADIO_ICON_WH_5)/2.0+RADIO_ICON_WH_5,
                      RADIO_ICON_WH_5,
                      RADIO_ICON_WH_5);
    else
    {
        return CGRectMake(0, CGRectGetHeight(contentRect) - RADIO_ICON_WH-ICON_TITLE_MARGIN,
                          RADIO_ICON_WH,
                          RADIO_ICON_WH);
    }
}
 
 
 
- (void)dealloc
{
    [self removeFromGroup];
    
    _delegate = nil;
    _groupId = nil;
}
 
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
 
@end