123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- //
- // YMChoiceButton.m
- // MSYOUPAI
- //
- // Created by YoMi on 2022/9/20.
- //
- #import "YMChoiceButton.h"
- @implementation YMChoiceButton
- + (id)buttonWithFrame:(CGRect)frame andChoiceType:(YMChoiceType)choiceType withIndex:(NSUInteger)index inGroup:(NSString*)groupId withSelectedImage:(UIImage*)selectedImage andNormalImage:(UIImage*)normalImage{
- YMChoiceButton *choiceButton = [[YMChoiceButton alloc] initWithChoiceType:choiceType inGroup:groupId];
- choiceButton.frame = frame;
- choiceButton.index = index;
- [choiceButton setImage:selectedImage forState:UIControlStateSelected];
- [choiceButton setImage:normalImage forState:UIControlStateNormal];
- [choiceButton addTarget:choiceButton action:@selector(checkBoxPress) forControlEvents:UIControlEventTouchUpInside];
- return choiceButton;
- }
- - (id)initWithChoiceType:(YMChoiceType)choiceType inGroup:(NSString*)groupId{
- if (self = [super init]) {
- _choiceType = choiceType;
- _groupId = groupId;
- if (choiceType == YMChoiceTypeRadio) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelOthers:) name:groupId object:nil];
- }
- }
- return self;
- }
- - (void)cancelOthers:(NSNotification*)notification{
- NSInteger selectedIndex = [[notification.userInfo objectForKey:@"index"] integerValue];
- if (selectedIndex != self.index) {
- self.statusType = YMChoiceButtonStatusTypeCancel;
- }
- }
- - (void)checkBoxPress{
- switch (_statusType) {
- case YMChoiceButtonStatusTypeSelect:
- if ((_choiceType == YMChoiceTypeRadio) && _mustChooseOne) {
- return;
- }
- _statusType = YMChoiceButtonStatusTypeCancel;
- self.selected = NO;
- if ([_delegate respondsToSelector:@selector(ym_choiceButtonCancelAtIndex:inGroup:)]) {
- [_delegate ym_choiceButtonCancelAtIndex:_index inGroup:_groupId];
- }
- break;
- case YMChoiceButtonStatusTypeCancel:
- _statusType = YMChoiceButtonStatusTypeSelect;
- self.selected = YES;
- if ([_delegate respondsToSelector:@selector(ym_choiceButtonSelectedAtIndex:inGroup:)]) {
- [_delegate ym_choiceButtonSelectedAtIndex:_index inGroup:_groupId];
- }
- if (_choiceType == YMChoiceTypeRadio) {
- NSDictionary* dict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%lu",(unsigned long)_index] forKey:@"index"];
- [[NSNotificationCenter defaultCenter] postNotificationName:_groupId object:self userInfo:dict];
- }
- break;
- default:
- break;
- }
- }
- - (void)setStatusType:(YMChoiceButtonStatusType)statusType{
- _statusType = statusType;
- switch (statusType) {
- case YMChoiceButtonStatusTypeSelect:
- self.selected = YES;
- if (_choiceType == YMChoiceTypeRadio) {
- NSDictionary* dict=[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%lu",(unsigned long)_index] forKey:@"index"];
- [[NSNotificationCenter defaultCenter] postNotificationName:_groupId object:self userInfo:dict];
- }
- break;
- case YMChoiceButtonStatusTypeCancel:
- self.selected=NO;
- break;
- }
- }
- - (void)dealloc{
- if (_choiceType == YMChoiceTypeRadio) {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- }
- @end
|