//
|
// 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
|