Skip to content

Commit

Permalink
Merge pull request #37 from sunilsharma08/develop
Browse files Browse the repository at this point in the history
Pod 2.0 release
  • Loading branch information
sunilsharma08 authored Jan 30, 2024
2 parents 72de373 + 688ffc3 commit f670fc0
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 117 deletions.
4 changes: 2 additions & 2 deletions IGCMenu.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
s.name = "IGCMenu"
s.version = "1.2.1"
s.version = "2.0.0"
s.summary = "Grid and Circular Menu for iOS."
s.description = "IGCMenu library gives easy way to create Grid and Circular menu in app.
This is light weight and highly customisable menu. Support iOS 7 and above."
s.homepage = "https://github.com/sunilsharma08/IGCMenu"
s.license = "MIT"
s.author = { "Sunil Sharma" => "[email protected]" }
s.platform = :ios, "8.0"
s.platform = :ios, "12.0"
s.source = { :git => "https://github.com/sunilsharma08/IGCMenu.git", :tag => "#{s.version}" }
s.source_files = "IGCMenu/**/*.{h,m}"
s.requires_arc = true
Expand Down
8 changes: 8 additions & 0 deletions IGCMenu/IGCMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ typedef enum {
None
}IGCMenuBackgroundOptions;

typedef enum {
Top,
Bottom,
}IGCMenuPositionOptions;

typedef enum {
Opened,
Closed
Expand Down Expand Up @@ -46,6 +51,7 @@ typedef enum {
@property int maxColumn; //Maximium number of column,default is 3
@property int menuHeight; //height = width ,default is 65
@property IGCMenuBackgroundOptions backgroundType; //Default is BlurEffectDark
@property IGCMenuPositionOptions positionStyle; //Default is Top
@property UIColor *menuTitleColor; // Menu title color, default is white
@property UIFont *menuTitleFont; // Menu title font, default is system regular 12

Expand All @@ -55,3 +61,5 @@ typedef enum {
-(void)hideGridMenu;

@end


205 changes: 129 additions & 76 deletions IGCMenu/IGCMenu.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ @implementation IGCMenu{
NSMutableArray *menuButtonArray; //array of menu buttons
NSMutableArray *menuNameLabelArray; //array of menu name label
UIView *pMenuButtonSuperView;

int maxRow;
__block CGFloat topMenuCenterY;
CGFloat eachMenuWidth;
CGFloat eachMenuVerticalSpace;
BOOL isCircularMenu;
BOOL isGridMenu;
}

- (instancetype)init{
Expand All @@ -34,6 +41,14 @@ - (instancetype)init{
self.backgroundType = BlurEffectDark;
self.menuTitleColor = [UIColor whiteColor];
self.menuTitleFont = [UIFont systemFontOfSize:12];
self.positionStyle = Top;

//Observe orientation changes
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
}
return self;
}
Expand Down Expand Up @@ -83,7 +98,7 @@ - (void)createMenuButtons{
[menuNameLabelArray addObject:menuNameLabel];
}

// set accessibility label and add the label if present
// Set accessibility label and add the label if present
if (self.menuItemsAccessibilityLabelsArray.count > i) {
menuButton.isAccessibilityElement = YES;
menuButton.accessibilityLabel = self.menuItemsAccessibilityLabelsArray[i];
Expand All @@ -102,15 +117,15 @@ - (void)createMenuButtons{
}

- (void)menuSuperViewBackground{
if (pMenuButtonSuperView == nil) {
pMenuButtonSuperView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
pMenuButtonSuperView.tag = MENU_BACKGROUND_VIEW_TAG;
}
if (!self.menuSuperView) {
self.menuSuperView = [self.menuButton superview];
}
[self.menuSuperView bringSubviewToFront:self.menuButton];
[self.menuSuperView insertSubview:pMenuButtonSuperView belowSubview:self.menuButton];
if (pMenuButtonSuperView == nil) {
pMenuButtonSuperView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
pMenuButtonSuperView.tag = MENU_BACKGROUND_VIEW_TAG;
}
if (!self.menuSuperView) {
self.menuSuperView = [self.menuButton superview];
}
[self.menuSuperView bringSubviewToFront:self.menuButton];
[self.menuSuperView insertSubview:pMenuButtonSuperView belowSubview:self.menuButton];

if (self.disableBackground){
pMenuButtonSuperView.userInteractionEnabled = YES;
Expand Down Expand Up @@ -159,6 +174,7 @@ -(void)setBlurredView:(UIBlurEffectStyle) blurEffectStyle{
}

- (void)showCircularMenu{
isCircularMenu = true;

[self menuSuperViewBackground];

Expand All @@ -168,25 +184,30 @@ - (void)showCircularMenu{
//menuButton.center = CGPointMake(homeButtonCenter.x - radius * cos(angle * i), homeButtonCenter.y - radius * sin(angle * i));

for (int i = 1; i < menuButtonArray.count * 2; i=i+2) {

CGFloat angle = M_PI / (menuButtonArray.count * 2);
[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self->pMenuButtonSuperView.layer.opacity = 1.0;
UIButton * menuButton = (UIButton *)[self->menuButtonArray objectAtIndex:i/2];
menuButton.layer.opacity = 1.0;
menuButton.center = CGPointMake(self.menuButton.center.x - self.menuRadius * cos(angle * i), self.menuButton.center.y - self.menuRadius * sin(angle * i));
if (self->menuNameLabelArray.count > (i/2)) {
UILabel *menuNameLabel = (UILabel *)[self->menuNameLabelArray objectAtIndex:i/2];
menuNameLabel.layer.opacity = 1.0;
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
}
self->pMenuButtonSuperView.layer.opacity = 1.0;
[self updateCircularMenuLayoutAtIndex:i];
} completion:^(BOOL finished) {
self.currentState = Opened;
}];
}
}

- (void)updateCircularMenuLayoutAtIndex:(int)i{
UIButton * menuButton = (UIButton *)[menuButtonArray objectAtIndex:i/2];
menuButton.layer.opacity = 1.0;
CGFloat angle = self.positionStyle == Top ? M_PI / (menuButtonArray.count * 2) : -M_PI / (menuButtonArray.count * 2);
menuButton.center = CGPointMake(self.menuButton.center.x - self.menuRadius * cos(angle * i), self.menuButton.center.y - self.menuRadius * sin(angle * i));
if (menuNameLabelArray.count > (i/2)) {
UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex:i/2];
menuNameLabel.layer.opacity = 1.0;
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
}
}

- (void)hideCircularMenu{
isCircularMenu = false;

[UIView animateWithDuration:ANIMATION_DURATION delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
for (int i = 0; i < self->menuButtonArray.count; i++) {
UIButton *menuButton = (UIButton *)[self->menuButtonArray objectAtIndex:i];
Expand Down Expand Up @@ -217,95 +238,127 @@ - (void)hideCircularMenu{
}

-(void)showGridMenu{
isGridMenu = true;
[self menuSuperViewBackground];
if (menuButtonArray.count <= 0) {
[self createMenuButtons];
}

int maxRow = ceilf(menuButtonArray.count /(float)self.maxColumn);
__block CGFloat topMenuCenterY = self.menuButton.frame.origin.y - 10;
CGFloat eachMenuVerticalSpace = 0;
CGFloat eachMenuWidth = 0;
[self setMenuButtonLayout];
[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self->pMenuButtonSuperView.layer.opacity = 1.0;
[self updateGridMenuLayout];
} completion:^(BOOL finished) {
self.currentState = Opened;
}];
}

-(void)setMenuButtonLayout{
maxRow = ceilf(menuButtonArray.count /(float)self.maxColumn);
topMenuCenterY = self.menuButton.frame.origin.y - 10;
eachMenuVerticalSpace = 0;
eachMenuWidth = 0;
if (menuButtonArray.count) {
UIButton *menuButton = (UIButton *)menuButtonArray[0];
eachMenuVerticalSpace = menuButton.frame.size.height + 20;
eachMenuVerticalSpace = self.positionStyle == Top ? menuButton.frame.size.height + 20 : -(menuButton.frame.size.height + 20);
eachMenuWidth = menuButton.frame.size.width;
if (menuNameLabelArray.count) {
UILabel *nameLabel = (UILabel *)menuNameLabelArray[0];
eachMenuVerticalSpace = eachMenuVerticalSpace + nameLabel.frame.size.height;
}
topMenuCenterY = topMenuCenterY - (eachMenuVerticalSpace * maxRow) + menuButton.frame.size.height/2;
}
else{
eachMenuVerticalSpace = 100.0;
else {
eachMenuVerticalSpace = self.positionStyle == Top ? 100.0 : -100.0;
topMenuCenterY = topMenuCenterY - (eachMenuVerticalSpace * maxRow) + eachMenuVerticalSpace/3;
}
}

- (void)updateGridMenuLayout{

__block CGFloat distanceBetweenMenu = ((pMenuButtonSuperView.frame.size.width - (self.maxColumn*eachMenuWidth))/(self.maxColumn +1));

[UIView animateWithDuration:ANIMATION_DURATION delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self->pMenuButtonSuperView.layer.opacity = 1.0;
int menuIndex = 0;
//for each row
for(int i = 1; i <= maxRow; i++,topMenuCenterY += eachMenuVerticalSpace) {

int menuIndex = 0;
//for each row
for(int i = 1; i <= maxRow; i++,topMenuCenterY += eachMenuVerticalSpace) {

int remainingMenuButton = self.maxColumn;
//CGFloat menuCenterX = distanceBetweenMenu;

CGFloat menuCenterX;
//for each column
for (int j = 1; j <= remainingMenuButton; j++) {
UIButton *menuButton = (UIButton *)[self->menuButtonArray objectAtIndex:menuIndex];
menuButton.layer.opacity = 1.0;

menuCenterX = (distanceBetweenMenu *j) + (2*j - 1)*(menuButton.frame.size.width/2);
if (i == maxRow) {
remainingMenuButton = self->menuButtonArray.count % self.maxColumn;
if (remainingMenuButton == 0) {
remainingMenuButton = self.maxColumn;
}
menuCenterX = menuCenterX + ((self.maxColumn - remainingMenuButton)*(distanceBetweenMenu/2)) + (self.maxColumn - remainingMenuButton)*menuButton.frame.size.width/2;
}
menuButton.center = CGPointMake(menuCenterX, topMenuCenterY);

if (self->menuNameLabelArray.count > menuIndex) {
UILabel *menuNameLabel = (UILabel *)[self->menuNameLabelArray objectAtIndex:menuIndex];
menuNameLabel.layer.opacity = 1.0;
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
int remainingMenuButton = self.maxColumn;
//CGFloat menuCenterX = distanceBetweenMenu;

CGFloat menuCenterX;
//for each column
for (int j = 1; j <= remainingMenuButton; j++) {

UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex:menuIndex];
menuButton.layer.opacity = 1.0;

menuCenterX = (distanceBetweenMenu *j) + (2*j - 1)*(menuButton.frame.size.width/2);
if (i == maxRow) {
remainingMenuButton = self->menuButtonArray.count % self.maxColumn;
if (remainingMenuButton == 0) {
remainingMenuButton = self.maxColumn;
}

menuIndex++;
menuCenterX = menuCenterX + ((self.maxColumn - remainingMenuButton)*(distanceBetweenMenu/2)) + (self.maxColumn - remainingMenuButton)*menuButton.frame.size.width/2;
}
menuButton.center = CGPointMake(menuCenterX, topMenuCenterY);

if (menuNameLabelArray.count > menuIndex) {
UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex:menuIndex];
menuNameLabel.layer.opacity = 1.0;
menuNameLabel.center = CGPointMake(menuButton.center.x, menuButton.frame.origin.y + menuButton.frame.size.height + (menuNameLabel.frame.size.height / 2) + 5);
}

menuIndex++;
}
} completion:^(BOOL finished) {
self.currentState = Opened;
}];
}
}

-(void)hideGridMenu{
isGridMenu = false;
[self hideCircularMenu];
}

- (void)menuButtonClicked:(UIButton *)sender{
if ([self.delegate respondsToSelector:@selector(igcMenuSelected:atIndex:)]) {
int index;
NSInteger buttonTag = sender.tag;
for (index = 0; index < menuButtonArray.count; index++) {
UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex:index];
if (menuButton.tag == buttonTag) {
NSString *menuName;
if (self.menuItemsNameArray.count > index) {
menuName = self.menuItemsNameArray[index];
}
if (self.delegate) {
[self.delegate igcMenuSelected:menuName atIndex:index];
int index;
NSInteger buttonTag = sender.tag;
for (index = 0; index < menuButtonArray.count; index++) {
UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex:index];
if (menuButton.tag == buttonTag) {
NSString *menuName;
if (self.menuItemsNameArray.count > index) {
menuName = self.menuItemsNameArray[index];
}
if (self.delegate) {
[self.delegate igcMenuSelected:menuName atIndex:index];
}
break;
}
break;
}
}
}
}

//MARK: Orientation changes
- (void)orientationChanged:(NSNotification *)note{
UIDevice * device = note.object;
switch(device.orientation){
case UIDeviceOrientationPortrait:
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
[pMenuButtonSuperView setFrame: [UIScreen mainScreen].bounds];
/* update menu animation */
if (isCircularMenu){
for (int i = 1; i < menuButtonArray.count * 2; i=i+2) {
[self updateCircularMenuLayoutAtIndex:i];
}
} else if (isGridMenu){
[self setMenuButtonLayout];
[self updateGridMenuLayout];
}
break;
default:
break;
};
}

@end

Loading

0 comments on commit f670fc0

Please sign in to comment.