YMChoiceButton.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // YMChoiceButton.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2022/9/20.
  6. //
  7. #import "YMChoiceButton.h"
  8. @implementation YMChoiceButton
  9. + (id)buttonWithFrame:(CGRect)frame andChoiceType:(YMChoiceType)choiceType withIndex:(NSUInteger)index inGroup:(NSString*)groupId withSelectedImage:(UIImage*)selectedImage andNormalImage:(UIImage*)normalImage{
  10. YMChoiceButton *choiceButton = [[YMChoiceButton alloc] initWithChoiceType:choiceType inGroup:groupId];
  11. choiceButton.frame = frame;
  12. choiceButton.index = index;
  13. [choiceButton setImage:selectedImage forState:UIControlStateSelected];
  14. [choiceButton setImage:normalImage forState:UIControlStateNormal];
  15. [choiceButton addTarget:choiceButton action:@selector(checkBoxPress) forControlEvents:UIControlEventTouchUpInside];
  16. return choiceButton;
  17. }
  18. - (id)initWithChoiceType:(YMChoiceType)choiceType inGroup:(NSString*)groupId{
  19. if (self = [super init]) {
  20. _choiceType = choiceType;
  21. _groupId = groupId;
  22. if (choiceType == YMChoiceTypeRadio) {
  23. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelOthers:) name:groupId object:nil];
  24. }
  25. }
  26. return self;
  27. }
  28. - (void)cancelOthers:(NSNotification*)notification{
  29. NSInteger selectedIndex = [[notification.userInfo objectForKey:@"index"] integerValue];
  30. if (selectedIndex != self.index) {
  31. self.statusType = YMChoiceButtonStatusTypeCancel;
  32. }
  33. }
  34. - (void)checkBoxPress{
  35. switch (_statusType) {
  36. case YMChoiceButtonStatusTypeSelect:
  37. if ((_choiceType == YMChoiceTypeRadio) && _mustChooseOne) {
  38. return;
  39. }
  40. _statusType = YMChoiceButtonStatusTypeCancel;
  41. self.selected = NO;
  42. if ([_delegate respondsToSelector:@selector(ym_choiceButtonCancelAtIndex:inGroup:)]) {
  43. [_delegate ym_choiceButtonCancelAtIndex:_index inGroup:_groupId];
  44. }
  45. break;
  46. case YMChoiceButtonStatusTypeCancel:
  47. _statusType = YMChoiceButtonStatusTypeSelect;
  48. self.selected = YES;
  49. if ([_delegate respondsToSelector:@selector(ym_choiceButtonSelectedAtIndex:inGroup:)]) {
  50. [_delegate ym_choiceButtonSelectedAtIndex:_index inGroup:_groupId];
  51. }
  52. if (_choiceType == YMChoiceTypeRadio) {
  53. NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%lu",(unsigned long)_index] forKey:@"index"];
  54. [[NSNotificationCenter defaultCenter] postNotificationName:_groupId object:self userInfo:dict];
  55. }
  56. break;
  57. default:
  58. break;
  59. }
  60. }
  61. - (void)setStatusType:(YMChoiceButtonStatusType)statusType{
  62. _statusType = statusType;
  63. switch (statusType) {
  64. case YMChoiceButtonStatusTypeSelect:
  65. self.selected = YES;
  66. if (_choiceType == YMChoiceTypeRadio) {
  67. NSDictionary* dict=[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%lu",(unsigned long)_index] forKey:@"index"];
  68. [[NSNotificationCenter defaultCenter] postNotificationName:_groupId object:self userInfo:dict];
  69. }
  70. break;
  71. case YMChoiceButtonStatusTypeCancel:
  72. self.selected=NO;
  73. break;
  74. }
  75. }
  76. - (void)dealloc{
  77. if (_choiceType == YMChoiceTypeRadio) {
  78. [[NSNotificationCenter defaultCenter] removeObserver:self];
  79. }
  80. }
  81. @end