diff --git a/Classes/MAKDropDownMenu.h b/Classes/MAKDropDownMenu.h index 179544f..d9e6bc2 100644 --- a/Classes/MAKDropDownMenu.h +++ b/Classes/MAKDropDownMenu.h @@ -20,11 +20,13 @@ @property (strong, nonatomic) NSArray *titles; @property (weak, nonatomic) id delegate; @property (strong, nonatomic) UIColor *buttonBackgroundColor; +@property (strong, nonatomic) UIColor *activeButtonBackgroundColor; @property (strong, nonatomic) UIColor *tintColor; @property (assign, nonatomic) CGFloat separatorHeight; @property (assign, nonatomic) UIEdgeInsets buttonsInsets; @property (assign, nonatomic, readonly) BOOL isOpen; - (void)openAnimated:(BOOL)animated; +- (void)openAnimated:(BOOL)animated withActiveButtonId:(NSInteger) buttonId; - (void)closeAnimated:(BOOL)animated; @end diff --git a/Classes/MAKDropDownMenu.m b/Classes/MAKDropDownMenu.m index 9afac4b..9fecaf8 100644 --- a/Classes/MAKDropDownMenu.m +++ b/Classes/MAKDropDownMenu.m @@ -13,8 +13,9 @@ static const NSTimeInterval kAnimationDuration = .3; @implementation MAKDropDownMenu { - NSArray *_buttons; - UIColor *_privateBackgroundColor; + NSArray *_buttons; + UIColor *_privateBackgroundColor; + NSInteger _activeButtonId; } @synthesize isOpen = _isOpen; @@ -41,6 +42,8 @@ - (void)initialize { [self updateBackgroundColor]; self.tintColor = [UIColor blackColor]; _buttonBackgroundColor = [UIColor whiteColor]; + _activeButtonBackgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f]; + _activeButtonId = -1; UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTap)]; [self addGestureRecognizer:recognizer]; _isOpen = NO; @@ -67,10 +70,23 @@ - (void)setTitles:(NSArray *)titles { - (void)setButtonBackgroundColor:(UIColor *)buttonBackgroundColor { _buttonBackgroundColor = buttonBackgroundColor; - dispatch_apply(_buttons.count, dispatch_get_main_queue(), ^(size_t index) { + [self updateBackgroundColor]; +} + +- (void)setActiveButtonBackgroundColor:(UIColor *)activeButtonBackgroundColor { + _activeButtonBackgroundColor = activeButtonBackgroundColor; + [self updateButtonBackgroundColor]; +} + +-(void)updateButtonBackgroundColor { + for (int index = 0; index < _buttons.count; index++) { UIButton *button = _buttons[index]; - [button setBackgroundColor:buttonBackgroundColor]; - }); + if (_activeButtonId == index) { + [button setBackgroundColor:_activeButtonBackgroundColor]; + } else { + [button setBackgroundColor:_buttonBackgroundColor]; + } + } } - (void)setTintColor:(UIColor *)tintColor { @@ -101,6 +117,7 @@ - (void)openAnimated:(BOOL)animated { return; } _isOpen = YES; + _activeButtonId = -1; CGFloat const y = -kButtonHeight; for (UIButton *button in _buttons) { @@ -126,6 +143,12 @@ - (void)openAnimated:(BOOL)animated { } } +- (void)openAnimated:(BOOL)animated withActiveButtonId:(NSInteger) buttonId { + _activeButtonId = buttonId; + [self updateButtonBackgroundColor]; + [self openAnimated:animated]; +} + - (void)closeAnimated:(BOOL)animated { if (!_isOpen) { return; diff --git a/Example/MAKDropDownMenuExample/MAKViewController.m b/Example/MAKDropDownMenuExample/MAKViewController.m index 21aeff3..ea4a6b5 100644 --- a/Example/MAKDropDownMenuExample/MAKViewController.m +++ b/Example/MAKDropDownMenuExample/MAKViewController.m @@ -12,6 +12,7 @@ @interface MAKViewController () @property (weak, nonatomic) IBOutlet UILabel *selectedItem; @property (weak, nonatomic) MAKDropDownMenu *menu; +@property (nonatomic) NSInteger selectedItemId; @end @implementation MAKViewController @@ -25,11 +26,13 @@ - (void)viewWillAppear:(BOOL)animated { menu.backgroundColor = [UIColor colorWithWhite:.3f alpha:.5f]; menu.tintColor = [UIColor colorWithWhite:.1f alpha:1.f]; menu.buttonBackgroundColor = [UIColor whiteColor]; + menu.activeButtonBackgroundColor = [UIColor lightGrayColor]; menu.titles = @[@"Item 1", @"Item 2", @"Item 3", @"Item 4", @"Item 5", @"Item 6", @"Item 7"]; menu.separatorHeight = 1 / [UIScreen mainScreen].scale; menu.layer.masksToBounds = YES; menu.buttonsInsets = UIEdgeInsetsMake(1 / [UIScreen mainScreen].scale, 0, 0, 0); menu.delegate = self; + _selectedItemId = -1; //adding menu to navigation view [self.navigationController.view addSubview:menu]; @@ -56,12 +59,13 @@ - (void)closeMenu { } - (void)openMenu { - [self.menu openAnimated:YES]; + [self.menu openAnimated:YES withActiveButtonId:_selectedItemId]; } #pragma mark - MAKDropDownMenuDelegate - (void)dropDownMenu:(MAKDropDownMenu *)menu itemDidSelect:(NSUInteger)itemIndex { self.selectedItem.text = [NSString stringWithFormat:@"%u", (unsigned int)(itemIndex + 1)]; + _selectedItemId = itemIndex; [self closeMenu]; }