From 5ac530efcb4155ac6c9aaba47cde894377c025a9 Mon Sep 17 00:00:00 2001 From: Hector Matos Date: Wed, 23 Apr 2014 14:50:27 -0700 Subject: [PATCH] added flag for when menu is showing and additional delegate methods to notify the developer when the contextual menu presents and dismiss in order to give flexibility to take action when the menu presents/dismisses --- Classes/BAMContextualMenu.h | 6 ++++++ Classes/BAMContextualMenu.m | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Classes/BAMContextualMenu.h b/Classes/BAMContextualMenu.h index cd24dce..d79bacd 100644 --- a/Classes/BAMContextualMenu.h +++ b/Classes/BAMContextualMenu.h @@ -34,6 +34,7 @@ typedef enum HMContextualMenuActivateOption : NSUInteger { @interface BAMContextualMenu : UIView +@property (nonatomic) BOOL menuIsShowing; //Flag to turn off the ability to activate the popup menu. Defaults to YES but will be NO if menuItems array is nil or empty. @property (nonatomic) BOOL shouldActivateMenu; //Flag to turn off the ability to activate the popup menu. Defaults to YES but will be NO if menuItems array is nil or empty. @property (nonatomic) BOOL shouldHighlightOutwards; //Defaults to YES. This flag determines whether or not the menu item will animate outwards on highlight. Or just stay in place. @@ -62,6 +63,11 @@ typedef enum HMContextualMenuActivateOption : NSUInteger { - (UIView *)contextualMenu:(BAMContextualMenu *)contextualMenu viewForMenuItemAtIndex:(NSUInteger)index; @optional +//You would use this delegate method to pause a game or take action when contextual menu overlays your entire screen. For example, If my iOS game implemented this contextual menu by tapping on a character in motion, I would probably pause the game while it's up to give the user time to make a selection on the menu. This is one such example, feel free to use it in any way you would need it. +- (void)contextualMenuActivated:(BAMContextualMenu *)contextualMenu; +//This method could be used to undo any action taken in contextualMenuActivated:. +- (void)contextualMenuDismissed:(BAMContextualMenu *)contextualMenu; + - (void)contextualMenu:(BAMContextualMenu *)contextualMenu didSelectItemAtIndex:(NSUInteger)index; - (void)contextualMenu:(BAMContextualMenu *)contextualMenu didHighlightItemAtIndex:(NSUInteger)index; - (UIView *)contextualMenu:(BAMContextualMenu *)contextualMenu viewForHighlightedMenuItemAtIndex:(NSUInteger)index; diff --git a/Classes/BAMContextualMenu.m b/Classes/BAMContextualMenu.m index 8a43b1f..3b4a928 100644 --- a/Classes/BAMContextualMenu.m +++ b/Classes/BAMContextualMenu.m @@ -86,7 +86,6 @@ @interface BAMContextualMenu () BOOL shouldRelayoutSubviews; BOOL menuItemIsAnimating; BOOL shouldSelectMenuItem; - BOOL menuIsShowing; } @property (nonatomic, weak) id delegate; @@ -105,7 +104,7 @@ - (id)initWithContainingView:(UIView *)containingView activateOption:(BAMContext self.dataSource = contextualDataSource; self.shouldHighlightOutwards = YES; - menuIsShowing = NO; + _menuIsShowing = NO; _menuItemDistancePadding = 30.0f; @@ -422,7 +421,7 @@ - (NSUInteger)indexAtPoint:(CGPoint)point #pragma mark Presentation/Dismissal Method - (void)showMenuItems:(BOOL)show completion:(void (^)())completion { - menuIsShowing = show; + _menuIsShowing = show; //totalCircle in this context means the circle from the starting location of the user's touch to the edge of the biggest menu item CGFloat totalCircleRectX = startingLocation.x - (circleViewWidthHeight / 2.0) - _menuItemDistancePadding - biggestMenuItemWidthHeight; CGFloat totalCircleRadius = startingLocation.x - totalCircleRectX; @@ -431,6 +430,9 @@ - (void)showMenuItems:(BOOL)show completion:(void (^)())completion [menuItemRectsInWindowArray removeAllObjects]; if (show) { + if (self.delegate && [self.delegate respondsToSelector:@selector(contextualMenuActivated:)]) { + [self.delegate contextualMenuActivated:self]; + } //Calculate proper angle offset ZZScreenEdge screenCorner = (startingLocation.x < mainWindow.frame.size.width / 2.0) ? kZZScreenEdgeLeft : kZZScreenEdgeRight; @@ -511,6 +513,9 @@ - (void)showMenuItems:(BOOL)show completion:(void (^)())completion } }]; } else { + if (self.delegate && [self.delegate respondsToSelector:@selector(contextualMenuDismissed:)]) { + [self.delegate contextualMenuDismissed:self]; + } //Animations for dismissal [UIView animateKeyframesWithDuration:0.3 delay:0.0