From 4bcb5ab4f2c15383a0ee3c2cdc524be8845e1923 Mon Sep 17 00:00:00 2001 From: AntiVIRUZ Date: Fri, 31 Jul 2015 14:26:25 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=B2=D1=8B=D0=B4=D0=B5=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B2=D1=8B=D0=B1=D1=80=D0=B0=D0=BD=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/MAKDropDownMenu.h | 2 ++ Classes/MAKDropDownMenu.m | 32 ++++++++++++++++--- .../MAKViewController.m | 5 ++- 3 files changed, 33 insertions(+), 6 deletions(-) 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..9527cef 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 = 4; 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 updateButtonBackgroundColor]; +} + +- (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 { @@ -126,6 +142,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..609f0b1 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 @@ -30,6 +31,7 @@ - (void)viewWillAppear:(BOOL)animated { 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 +58,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]; } From 9eac8ac1f0df49834686648279c4cc2f23ba626b Mon Sep 17 00:00:00 2001 From: Vasiliy Yatsevich Date: Sat, 17 Oct 2015 22:07:38 +0300 Subject: [PATCH 2/3] Add ability to highlight row. Fix bug: list freesed when background color is changed --- Classes/MAKDropDownMenu.h | 2 ++ Classes/MAKDropDownMenu.m | 35 ++++++++++++++++--- .../MAKViewController.m | 6 +++- 3 files changed, 37 insertions(+), 6 deletions(-) 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..1e34d6b 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,25 @@ - (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 { + //dispatch_apply(_buttons.count, dispatch_get_main_queue(), ^(size_t index) { + 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 +119,7 @@ - (void)openAnimated:(BOOL)animated { return; } _isOpen = YES; + _activeButtonId = -1; CGFloat const y = -kButtonHeight; for (UIButton *button in _buttons) { @@ -126,6 +145,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]; } From 2e214d2539e8667db03c9e2f6f437283e0a9a03b Mon Sep 17 00:00:00 2001 From: Vasiliy Yatsevich Date: Sat, 17 Oct 2015 22:13:24 +0300 Subject: [PATCH 3/3] Commentaries have been deleted --- Classes/MAKDropDownMenu.m | 2 -- 1 file changed, 2 deletions(-) diff --git a/Classes/MAKDropDownMenu.m b/Classes/MAKDropDownMenu.m index 1e34d6b..9fecaf8 100644 --- a/Classes/MAKDropDownMenu.m +++ b/Classes/MAKDropDownMenu.m @@ -79,7 +79,6 @@ - (void)setActiveButtonBackgroundColor:(UIColor *)activeButtonBackgroundColor { } -(void)updateButtonBackgroundColor { - //dispatch_apply(_buttons.count, dispatch_get_main_queue(), ^(size_t index) { for (int index = 0; index < _buttons.count; index++) { UIButton *button = _buttons[index]; if (_activeButtonId == index) { @@ -88,7 +87,6 @@ -(void)updateButtonBackgroundColor { [button setBackgroundColor:_buttonBackgroundColor]; } } - //}); } - (void)setTintColor:(UIColor *)tintColor {