MHRadioButton.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // MHRadioButton.m
  3. //
  4. //
  5. // Created by Macro on 14-11-9.
  6. // Copyright (c) 2014年 Macro. All rights reserved.
  7. //
  8. #import "MHRadioButton.h"
  9. @interface MHRadioButton ()
  10. {
  11. // 每一个实例的
  12. UIButton *_button;
  13. }
  14. // 要用实例来访问这两个属性
  15. @property (nonatomic, strong) NSString *groupId;
  16. @property (nonatomic, assign) NSUInteger index;
  17. @end
  18. // RadioButton实例队列, 用于同组选中时, 取消同种中的其他实例
  19. static NSMutableArray<MHRadioButton *> *kInstances = nil;
  20. // 观察者映射, 通过观察者对RadioButtonDelegate方法的实现,对RadioButton选中状态的监控
  21. static NSMutableDictionary<NSString *, id> *kObservers = nil;
  22. // RadioButton选中状态的组号和选中下标的映射, 用于获取组号的选中下标
  23. static NSMutableDictionary<NSString *, NSNumber *> *kIndexDic;
  24. // RadioButton组号的记录队列, 用于默认情况下, 每组的默认选中下标, 即每组中第一个创建的下标
  25. static NSMutableArray<NSString *> *groupIdsRecord;
  26. @implementation MHRadioButton
  27. /*!
  28. * @author Macro, 14-11-10
  29. *
  30. * @brief 添加观察者 和 组号的映射关系
  31. *
  32. * @param observer 观察者
  33. * @param groupId 组号
  34. */
  35. + (void)addObserver:(id)observer
  36. forFroupId:(NSString *)groupId {
  37. if (!kObservers) {
  38. kObservers = [[NSMutableDictionary alloc] init];
  39. }
  40. if (observer && groupId.length > 0) {
  41. [kObservers setObject:observer forKey:groupId];
  42. }
  43. }
  44. /*!
  45. * @author Macro, 14-11-10
  46. *
  47. * @brief 注册实例, 将当前RadioButton加入实例队列
  48. *
  49. * @param rb 当前RadioButton
  50. */
  51. + (void)registerInstance:(MHRadioButton *)rb {
  52. if (!kInstances) {
  53. kInstances = [[NSMutableArray alloc] init];
  54. }
  55. if (![groupIdsRecord containsObject: rb.groupId]) {
  56. [groupIdsRecord addObject:rb.groupId];
  57. [rb selected];
  58. }
  59. [kInstances addObject:rb];
  60. }
  61. /*!
  62. * @author Macro, 14-11-10
  63. *
  64. * @brief 设置当前选中状态
  65. *
  66. * @param rb 选中的RadioButton
  67. */
  68. + (void)buttonSelected:(MHRadioButton *)rb {
  69. // 响应观察者的协议方法
  70. if (kObservers) {
  71. id observer = [kObservers objectForKey:rb.groupId];
  72. if (observer &&
  73. [observer respondsToSelector:
  74. @selector(radioButtonSelectedAtIndex:inGroup:)]){
  75. [observer radioButtonSelectedAtIndex:rb.index
  76. inGroup:rb.groupId];
  77. }
  78. }
  79. // 把实例队列中的其他RadioButton取消选中
  80. if (kInstances) {
  81. for (int i = 0; i < kInstances.count; i++) {
  82. MHRadioButton *button = [kInstances objectAtIndex:i];
  83. if (![rb isEqual:button] && [button.groupId isEqualToString:rb.groupId]) {
  84. [button otherButtonSelected:rb];
  85. }
  86. }
  87. }
  88. }
  89. /*!
  90. * @author Macro, 14-11-10
  91. *
  92. * @brief 获取特定组号的那组RadioButton的选中下标
  93. *
  94. * @param groupId 组号
  95. *
  96. * @return 下标 -1表示组号不存在
  97. */
  98. + (NSUInteger)getIndexWithGroupId:(NSString *)groupId {
  99. NSNumber *indexNumber = [kIndexDic objectForKey:groupId];
  100. if (!indexNumber) {
  101. return -1;
  102. }
  103. return [indexNumber unsignedLongValue];
  104. }
  105. /*!
  106. * @author Macro, 14-11-09
  107. *
  108. * @brief 初始化一个RadioButton
  109. *
  110. * @param groupId 按钮的组号
  111. * @param index 按钮在组中的下标
  112. *
  113. * @return RadioButton
  114. */
  115. - (instancetype)initWithGroupId:(NSString *)groupId
  116. atIndex:(NSUInteger)index {
  117. self = [super init];
  118. if (self) {
  119. _groupId = groupId;
  120. _index = index;
  121. if (!groupIdsRecord) {
  122. groupIdsRecord = [[NSMutableArray alloc] init];
  123. }
  124. [self defaultInit];
  125. }
  126. return self;
  127. }
  128. /*!
  129. * @author Macro, 14-11-09
  130. *
  131. * @brief 设置一些默认属性
  132. */
  133. - (void)defaultInit {
  134. CGRect frame = CGRectMake(0, 0, kRadioButtonWidth, kRadioButtonHeight);
  135. self.frame = frame;
  136. _button = [UIButton buttonWithType:(UIButtonTypeCustom)];
  137. _button.frame = frame;
  138. _button.adjustsImageWhenHighlighted = NO;
  139. // [_button setBackgroundColor:[UIColor lightGrayColor]]; // test
  140. [_button setImage:[UIImage imageNamed:@"unselect"]
  141. forState:(UIControlStateNormal)];
  142. [_button setImage:[UIImage imageNamed:@"select"]
  143. forState:(UIControlStateSelected)];
  144. [_button addTarget:self
  145. action:@selector(selected)
  146. forControlEvents:(UIControlEventTouchUpInside)];
  147. [self addSubview:_button];
  148. [MHRadioButton registerInstance:self];
  149. }
  150. - (void)selected {
  151. if (!kIndexDic) {
  152. kIndexDic = [[NSMutableDictionary alloc] init];
  153. }
  154. [kIndexDic setObject:@(_index) forKey:_groupId];
  155. _button.selected = YES;
  156. [MHRadioButton buttonSelected:self];
  157. }
  158. - (void)otherButtonSelected:(MHRadioButton *)rb {
  159. if (_button.selected) {
  160. _button.selected = NO;
  161. }
  162. }
  163. /*
  164. // Only override drawRect: if you perform custom drawing.
  165. // An empty implementation adversely affects performance during animation.
  166. - (void)drawRect:(CGRect)rect {
  167. // Drawing code
  168. }
  169. */
  170. @end