Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/MAKDropDownMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
@property (strong, nonatomic) NSArray *titles;
@property (weak, nonatomic) id<MAKDropDownMenuDelegate> 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
33 changes: 28 additions & 5 deletions Classes/MAKDropDownMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
static const NSTimeInterval kAnimationDuration = .3;

@implementation MAKDropDownMenu {
NSArray *_buttons;
UIColor *_privateBackgroundColor;
NSArray *_buttons;
UIColor *_privateBackgroundColor;
NSInteger _activeButtonId;
}
@synthesize isOpen = _isOpen;

Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -101,6 +117,7 @@ - (void)openAnimated:(BOOL)animated {
return;
}
_isOpen = YES;
_activeButtonId = -1;

CGFloat const y = -kButtonHeight;
for (UIButton *button in _buttons) {
Expand All @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion Example/MAKDropDownMenuExample/MAKViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@interface MAKViewController () <MAKDropDownMenuDelegate>
@property (weak, nonatomic) IBOutlet UILabel *selectedItem;
@property (weak, nonatomic) MAKDropDownMenu *menu;
@property (nonatomic) NSInteger selectedItemId;
@end

@implementation MAKViewController
Expand All @@ -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];
Expand All @@ -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];
}

Expand Down