@@ -18,6 +18,13 @@ @implementation IGCMenu{
18
18
NSMutableArray *menuButtonArray; // array of menu buttons
19
19
NSMutableArray *menuNameLabelArray; // array of menu name label
20
20
UIView *pMenuButtonSuperView;
21
+
22
+ int maxRow;
23
+ __block CGFloat topMenuCenterY;
24
+ CGFloat eachMenuWidth;
25
+ CGFloat eachMenuVerticalSpace;
26
+ BOOL isCircularMenu;
27
+ BOOL isGridMenu;
21
28
}
22
29
23
30
- (instancetype )init {
@@ -31,6 +38,13 @@ - (instancetype)init{
31
38
self.menuRadius = 120 ;
32
39
self.maxColumn = 3 ;
33
40
self.backgroundType = BlurEffectDark;
41
+
42
+ // observe orientation changes
43
+ [[UIDevice currentDevice ] beginGeneratingDeviceOrientationNotifications ];
44
+ [[NSNotificationCenter defaultCenter ]
45
+ addObserver: self selector: @selector (orientationChanged: )
46
+ name: UIDeviceOrientationDidChangeNotification
47
+ object: [UIDevice currentDevice ]];
34
48
}
35
49
return self;
36
50
}
@@ -93,15 +107,15 @@ - (void)createMenuButtons{
93
107
}
94
108
95
109
- (void )menuSuperViewBackground {
96
- if (pMenuButtonSuperView == nil ) {
97
- pMenuButtonSuperView = [[UIView alloc ] initWithFrame: [[UIScreen mainScreen ] bounds ]];
98
- pMenuButtonSuperView.tag = MENU_BACKGROUND_VIEW_TAG;
99
- }
100
- if (!self.menuSuperView ) {
110
+ if (pMenuButtonSuperView == nil ) {
111
+ pMenuButtonSuperView = [[UIView alloc ] initWithFrame: [[UIScreen mainScreen ] bounds ]];
112
+ pMenuButtonSuperView.tag = MENU_BACKGROUND_VIEW_TAG;
113
+ }
114
+ if (!self.menuSuperView ) {
101
115
self.menuSuperView = [self .menuButton superview ];
102
- }
103
- [self .menuSuperView bringSubviewToFront: self .menuButton];
104
- [self .menuSuperView insertSubview: pMenuButtonSuperView belowSubview: self .menuButton];
116
+ }
117
+ [self .menuSuperView bringSubviewToFront: self .menuButton];
118
+ [self .menuSuperView insertSubview: pMenuButtonSuperView belowSubview: self .menuButton];
105
119
106
120
if (self.disableBackground ){
107
121
pMenuButtonSuperView.userInteractionEnabled = YES ;
@@ -150,6 +164,7 @@ -(void)setBlurredView:(UIBlurEffectStyle) blurEffectStyle{
150
164
}
151
165
152
166
- (void )showCircularMenu {
167
+ isCircularMenu = true ;
153
168
154
169
[self menuSuperViewBackground ];
155
170
@@ -159,23 +174,28 @@ - (void)showCircularMenu{
159
174
// menuButton.center = CGPointMake(homeButtonCenter.x - radius * cos(angle * i), homeButtonCenter.y - radius * sin(angle * i));
160
175
161
176
for (int i = 1 ; i < menuButtonArray.count * 2 ; i=i+2 ) {
162
-
163
- CGFloat angle = M_PI / (menuButtonArray.count * 2 );
164
177
[UIView animateWithDuration: ANIMATION_DURATION delay: 0 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
165
178
pMenuButtonSuperView.layer .opacity = 1.0 ;
166
- UIButton * menuButton = (UIButton *)[menuButtonArray objectAtIndex: i/2 ];
167
- menuButton.layer .opacity = 1.0 ;
168
- menuButton.center = CGPointMake (self.menuButton .center .x - self.menuRadius * cos (angle * i), self.menuButton .center .y - self.menuRadius * sin (angle * i));
169
- if (menuNameLabelArray.count > (i/2 )) {
170
- UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex: i/2 ];
171
- menuNameLabel.layer .opacity = 1.0 ;
172
- menuNameLabel.center = CGPointMake (menuButton.center .x , menuButton.frame .origin .y + menuButton.frame .size .height + (menuNameLabel.frame .size .height / 2 ) + 5 );
173
- }
179
+ [self updateCircularMenuLayoutAtIndex: i];
174
180
}completion: nil ];
175
181
}
176
182
}
177
183
184
+ - (void )updateCircularMenuLayoutAtIndex : (int )i {
185
+ UIButton * menuButton = (UIButton *)[menuButtonArray objectAtIndex: i/2 ];
186
+ menuButton.layer .opacity = 1.0 ;
187
+ CGFloat angle = M_PI / (menuButtonArray.count * 2 );
188
+ menuButton.center = CGPointMake (self.menuButton .center .x - self.menuRadius * cos (angle * i), self.menuButton .center .y - self.menuRadius * sin (angle * i));
189
+ if (menuNameLabelArray.count > (i/2 )) {
190
+ UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex: i/2 ];
191
+ menuNameLabel.layer .opacity = 1.0 ;
192
+ menuNameLabel.center = CGPointMake (menuButton.center .x , menuButton.frame .origin .y + menuButton.frame .size .height + (menuNameLabel.frame .size .height / 2 ) + 5 );
193
+ }
194
+ }
195
+
178
196
- (void )hideCircularMenu {
197
+ isCircularMenu = false ;
198
+
179
199
[UIView animateWithDuration: ANIMATION_DURATION delay: 0.0 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
180
200
for (int i = 0 ; i < menuButtonArray.count ; i++) {
181
201
UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex: i];
@@ -205,15 +225,24 @@ - (void)hideCircularMenu{
205
225
}
206
226
207
227
-(void )showGridMenu {
228
+ isGridMenu = true ;
208
229
[self menuSuperViewBackground ];
209
230
if (menuButtonArray.count <= 0 ) {
210
231
[self createMenuButtons ];
211
232
}
212
233
213
- int maxRow = ceilf (menuButtonArray.count /(float )self.maxColumn );
214
- __block CGFloat topMenuCenterY = self.menuButton .frame .origin .y - 10 ;
215
- CGFloat eachMenuVerticalSpace = 0 ;
216
- CGFloat eachMenuWidth = 0 ;
234
+ [self setMenuButtonLayout ];
235
+ [UIView animateWithDuration: ANIMATION_DURATION delay: 0 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
236
+ pMenuButtonSuperView.layer .opacity = 1.0 ;
237
+ [self updateGridMenuLayout ];
238
+ }completion: nil ];
239
+ }
240
+
241
+ -(void )setMenuButtonLayout {
242
+ maxRow = ceilf (menuButtonArray.count /(float )self.maxColumn );
243
+ topMenuCenterY = self.menuButton .frame .origin .y - 10 ;
244
+ eachMenuVerticalSpace = 0 ;
245
+ eachMenuWidth = 0 ;
217
246
if (menuButtonArray.count ) {
218
247
UIButton *menuButton = (UIButton *)menuButtonArray[0 ];
219
248
eachMenuVerticalSpace = menuButton.frame .size .height + 20 ;
@@ -228,48 +257,47 @@ -(void)showGridMenu{
228
257
eachMenuVerticalSpace = 100.0 ;
229
258
topMenuCenterY = topMenuCenterY - (eachMenuVerticalSpace * maxRow) + eachMenuVerticalSpace/3 ;
230
259
}
260
+ }
261
+
262
+ - (void )updateGridMenuLayout {
231
263
232
- __block CGFloat distanceBetweenMenu = ((pMenuButtonSuperView.frame .size .width - (self.maxColumn *eachMenuWidth))/(self.maxColumn +1 ));
233
-
234
- [UIView animateWithDuration: ANIMATION_DURATION delay: 0 options: UIViewAnimationOptionCurveEaseInOut animations: ^{
235
- pMenuButtonSuperView. layer . opacity = 1.0 ;
264
+ __block CGFloat distanceBetweenMenu = ((pMenuButtonSuperView.frame .size .width - (self.maxColumn *eachMenuWidth))/(self.maxColumn +1 ));
265
+ int menuIndex = 0 ;
266
+ // for each row
267
+ for ( int i = 1 ; i <= maxRow; i++,topMenuCenterY += eachMenuVerticalSpace) {
236
268
237
- int menuIndex = 0 ;
238
- // for each row
239
- for (int i = 1 ; i <= maxRow; i++,topMenuCenterY += eachMenuVerticalSpace) {
240
-
241
- int remainingMenuButton = self.maxColumn ;
242
- // CGFloat menuCenterX = distanceBetweenMenu;
269
+ int remainingMenuButton = self.maxColumn ;
270
+ // CGFloat menuCenterX = distanceBetweenMenu;
271
+
272
+ CGFloat menuCenterX;
273
+ // for each column
274
+ for (int j = 1 ; j <= remainingMenuButton; j++) {
275
+ UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex: menuIndex];
276
+ menuButton.layer .opacity = 1.0 ;
243
277
244
- CGFloat menuCenterX;
245
- // for each column
246
- for (int j = 1 ; j <= remainingMenuButton; j++) {
247
- UIButton *menuButton = (UIButton *)[menuButtonArray objectAtIndex: menuIndex];
248
- menuButton.layer .opacity = 1.0 ;
249
-
250
- menuCenterX = (distanceBetweenMenu *j) + (2 *j - 1 )*(menuButton.frame .size .width /2 );
251
- if (i == maxRow) {
252
- remainingMenuButton = menuButtonArray.count % self.maxColumn ;
253
- if (remainingMenuButton == 0 ) {
254
- remainingMenuButton = self.maxColumn ;
255
- }
256
- menuCenterX = menuCenterX + ((self.maxColumn - remainingMenuButton)*(distanceBetweenMenu/2 )) + (self.maxColumn - remainingMenuButton)*menuButton.frame .size .width /2 ;
257
- }
258
- menuButton.center = CGPointMake (menuCenterX, topMenuCenterY);
259
-
260
- if (menuNameLabelArray.count > menuIndex) {
261
- UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex: menuIndex];
262
- menuNameLabel.layer .opacity = 1.0 ;
263
- menuNameLabel.center = CGPointMake (menuButton.center .x , menuButton.frame .origin .y + menuButton.frame .size .height + (menuNameLabel.frame .size .height / 2 ) + 5 );
278
+ menuCenterX = (distanceBetweenMenu *j) + (2 *j - 1 )*(menuButton.frame .size .width /2 );
279
+ if (i == maxRow) {
280
+ remainingMenuButton = menuButtonArray.count % self.maxColumn ;
281
+ if (remainingMenuButton == 0 ) {
282
+ remainingMenuButton = self.maxColumn ;
264
283
}
265
-
266
- menuIndex++;
284
+ menuCenterX = menuCenterX + ((self.maxColumn - remainingMenuButton)*(distanceBetweenMenu/2 )) + (self.maxColumn - remainingMenuButton)*menuButton.frame .size .width /2 ;
267
285
}
286
+ menuButton.center = CGPointMake (menuCenterX, topMenuCenterY);
287
+
288
+ if (menuNameLabelArray.count > menuIndex) {
289
+ UILabel *menuNameLabel = (UILabel *)[menuNameLabelArray objectAtIndex: menuIndex];
290
+ menuNameLabel.layer .opacity = 1.0 ;
291
+ menuNameLabel.center = CGPointMake (menuButton.center .x , menuButton.frame .origin .y + menuButton.frame .size .height + (menuNameLabel.frame .size .height / 2 ) + 5 );
292
+ }
293
+
294
+ menuIndex++;
268
295
}
269
- }completion: nil ];
296
+ }
270
297
}
271
298
272
299
-(void )hideGridMenu {
300
+ isGridMenu = false ;
273
301
[self hideCircularMenu ];
274
302
}
275
303
@@ -293,5 +321,27 @@ - (void)menuButtonClicked:(UIButton *)sender{
293
321
}
294
322
}
295
323
324
+ // MARK: Orientation changes
325
+ - (void )orientationChanged : (NSNotification *)note {
326
+ UIDevice * device = note.object ;
327
+ switch (device.orientation ){
328
+ case UIDeviceOrientationPortrait:
329
+ case UIDeviceOrientationLandscapeLeft:
330
+ case UIDeviceOrientationLandscapeRight:
331
+ [pMenuButtonSuperView setFrame: [UIScreen mainScreen ].bounds];
332
+ /* update menu animation */
333
+ if (isCircularMenu){
334
+ for (int i = 1 ; i < menuButtonArray.count * 2 ; i=i+2 ) {
335
+ [self updateCircularMenuLayoutAtIndex: i];
336
+ }
337
+ } else if (isGridMenu){
338
+ [self setMenuButtonLayout ];
339
+ [self updateGridMenuLayout ];
340
+ }
341
+ break ;
342
+ default :
343
+ break ;
344
+ };
345
+ }
296
346
297
347
@end
0 commit comments