BDFaceSelectRadio.m 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // BDFaceSelectRadio.m
  3. // FaceSDKSample_IOS
  4. //
  5. // Created by Zhang,Jian(MBD) on 2020/12/2.
  6. // Copyright © 2020 Baidu. All rights reserved.
  7. //
  8. #import "BDFaceSelectRadio.h"
  9. static NSMutableArray *radioGroup;
  10. static NSString *const BDFaceSelectRadioSelectedImage = @"set_config_selected";
  11. static NSString *const BDFaceSelectRadioUnselectedImage = @"set_config_unselected";
  12. @interface BDFaceSelectRadio()
  13. @property(nonatomic, assign) BOOL radioState;
  14. @end
  15. @implementation BDFaceSelectRadio
  16. + (instancetype)buttonWithType:(UIButtonType)buttonType selected:(BOOL)selected {
  17. BDFaceSelectRadio *button = [BDFaceSelectRadio buttonWithType:buttonType];
  18. static dispatch_once_t onceToken;
  19. dispatch_once(&onceToken, ^{
  20. radioGroup = [NSMutableArray array];
  21. });
  22. [button initRadioState:selected];
  23. button.userInteractionEnabled = NO;
  24. [radioGroup addObject:button];
  25. return button;
  26. }
  27. /// 初始化时改变状态
  28. - (void)initRadioState:(BOOL)state {
  29. self.radioState = state;
  30. }
  31. /// 点击的时候改变状态,只有未选择才改变
  32. - (void)changeRadioState:(BOOL)state {
  33. if (self.radioState == NO
  34. && state == YES) {
  35. NSInteger index = 0;
  36. for (int i = 0; i < radioGroup.count; i++) {
  37. BDFaceSelectRadio *each = radioGroup[i];
  38. if (each != self) {
  39. // 其他按钮选择为未选择,自身为true
  40. each.radioState = NO;
  41. if (each.radioTaped) {
  42. each.radioTaped(index, NO);
  43. }
  44. } else {
  45. // 选择了这个
  46. index = i;
  47. each.radioState = YES;
  48. if (each.radioTaped) {
  49. each.radioTaped(index, TRUE);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. - (void)setRadioState:(BOOL)radioState {
  56. _radioState = radioState;
  57. if (radioState) {
  58. [self setImage:[UIImage imageNamed:BDFaceSelectRadioSelectedImage] forState:UIControlStateNormal];
  59. } else {
  60. [self setImage:[UIImage imageNamed:BDFaceSelectRadioUnselectedImage] forState:UIControlStateNormal];
  61. }
  62. }
  63. - (void)dealloc {
  64. if ([radioGroup containsObject:self]) {
  65. [radioGroup removeObject:self];
  66. }
  67. }
  68. @end