Skip to content

Commit e1e0d57

Browse files
committed
animation direction depending on menu position(Right - Left)
1 parent 76131bf commit e1e0d57

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

Example/ContextMenu/ViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ - (IBAction)presentMenuButtonTapped:(UIBarButtonItem *)sender {
8585
}
8686

8787
// it is better to use this method only for proper animation
88-
[self.contextMenuTableView showInView:self.navigationController.view withEdgeInsets:UIEdgeInsetsZero animated:YES];
88+
[self.contextMenuTableView showInView:self.navigationController.view presentationType:YALPresenationTypeRightToLeft withEdgeInsets:UIEdgeInsetsZero animated:YES];
8989
}
9090

9191
#pragma mark - Local methods

YALContextMenu/YALContextMenuTableView.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
#import <UIKit/UIKit.h>
1010

11+
typedef NS_ENUM(NSInteger, YALPresenationType) {
12+
YALPresenationTypeRightToLeft,
13+
YALPresenationTypeLeftToRight
14+
};
15+
1116
@class YALContextMenuTableView;
1217

1318
@protocol YALContextMenuTableViewDelegate <NSObject>
@@ -51,9 +56,11 @@
5156
5257
@param Superview to present your menu. If you are using a navigation controller it is better to use myViewController.navigationController.view as a suoerview.
5358
59+
@param presentationType depends on position of your cell's animatedIcon(On the right side of the screen - YALPresenationTypeRightToLeft, otherwise - YALPresenationTypeLeftToRight). Default is YALPresenationTypeRightToLeft.
60+
5461
@param YES or NO weather you want appearance animated or not.
5562
*/
56-
- (void)showInView:(UIView *)superview withEdgeInsets:(UIEdgeInsets)edgeInsets animated:(BOOL)animated;
63+
- (void)showInView:(UIView *)superview presentationType:(YALPresenationType)presentationType withEdgeInsets:(UIEdgeInsets)edgeInsets animated:(BOOL)animated;
5764

5865
/*!
5966
@abstract

YALContextMenu/YALContextMenuTableView.m

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
typedef void(^completionBlock)(BOOL completed);
1919

2020
typedef NS_ENUM(NSUInteger, Direction) {
21+
left,
2122
right,
2223
top,
2324
bottom
@@ -38,6 +39,7 @@ @interface YALContextMenuTableView ()
3839
@property (nonatomic, strong) UITableViewCell<YALContextMenuCell> *selectedCell;
3940
@property (nonatomic, strong) NSIndexPath *dismissalIndexpath;
4041
@property (nonatomic) AnimatingState animatingState;
42+
@property (nonatomic) YALPresenationType presentationType;
4143

4244
@end
4345

@@ -60,6 +62,7 @@ - (instancetype)init {
6062
self.animatingState = Stable;
6163
self.animationDuration = defaultDuration;
6264
self.animatingIndex = 0;
65+
self.presentationType = YALPresenationTypeRightToLeft;
6366

6467
self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7f];
6568
self.separatorColor = [UIColor colorWithRed:181.0/255.0 green:181.0/255.0 blue:181.0/255.0 alpha:0];
@@ -69,7 +72,8 @@ - (instancetype)init {
6972
}
7073

7174
#pragma mark - Show / Dismiss
72-
- (void)showInView:(UIView *)superview withEdgeInsets:(UIEdgeInsets)edgeInsets animated:(BOOL)animated {
75+
- (void)showInView:(UIView *)superview presentationType:(YALPresenationType)presentationType withEdgeInsets:(UIEdgeInsets)edgeInsets animated:(BOOL)animated {
76+
self.presentationType = presentationType;
7377

7478
if (self.animatingState!=Stable) {
7579
return;
@@ -169,7 +173,7 @@ - (void)show:(BOOL)show visibleCellsAnimated:(BOOL)animated {
169173
if (visibleCell) {
170174
[self prepareCellForShowAnimation:visibleCell];
171175
[visibleCell contentView].hidden = NO;
172-
Direction direction = (self.animatingIndex == firstVisibleRowIndex) ? right : top;
176+
Direction direction = (self.animatingIndex == firstVisibleRowIndex) ? [self directionForPresentation] : top;
173177

174178
[self show:show cell:visibleCell animated:animated direction:direction clockwise:NO completion:^(BOOL completed) {
175179
// ignore flag 'completed', cause if user scroll out animating cell, it will be false and menu will be empty(
@@ -210,7 +214,9 @@ - (void)dismissTopCells {
210214
}
211215

212216
- (void)dismissSelf {
213-
[self show:NO cell:self.selectedCell animated:YES direction:right clockwise:NO completion:^(BOOL completed) {
217+
Direction direction = [self directionForPresentation];
218+
BOOL clockwise = [self clockwiseRotationForPresentation];
219+
[self show:NO cell:self.selectedCell animated:YES direction:direction clockwise:clockwise completion:^(BOOL completed) {
214220
[self removeFromSuperview];
215221
if ([self.yalDelegate respondsToSelector:@selector(contextMenuTableView:didDismissWithIndexPath:)]) {
216222
[self.yalDelegate contextMenuTableView:self didDismissWithIndexPath:[self indexPathForCell:self.selectedCell]];
@@ -231,9 +237,10 @@ - (void)prepareCellForShowAnimation:(UITableViewCell<YALContextMenuCell> *)cell
231237

232238
[self resetAnimatedIconForCell:cell];
233239

234-
Direction direction = ([self indexPathForCell:cell].row == 0) ? right : top;
240+
Direction direction = ([self indexPathForCell:cell].row == 0) ? [self directionForPresentation] : top;
241+
BOOL clockwise = ([self indexPathForCell:cell].row == 0) ? [self clockwiseRotationForPresentation] : NO;
235242

236-
[self show:NO cell:cell animated:NO direction:direction clockwise:NO completion:nil];
243+
[self show:NO cell:cell animated:NO direction:direction clockwise:clockwise completion:nil];
237244
}
238245

239246
/*!
@@ -276,6 +283,11 @@ - (void)show:(BOOL)show
276283
}
277284

278285
switch (direction) {
286+
case left: {
287+
[self setAnchorPoint:CGPointMake(0, 0.5) forView:icon];
288+
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, DEGREES_TO_RADIANS(rotation), 0.0f, 1.0f, 0.0f);
289+
break;
290+
}
279291
case right: {
280292
[self setAnchorPoint:CGPointMake(1, 0.5) forView:icon];
281293
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, DEGREES_TO_RADIANS(rotation), 0.0f, 1.0f, 0.0f);
@@ -326,8 +338,7 @@ - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSI
326338
if (cell) {
327339
if (self.animatingState==Showing) {
328340
[cell contentView].hidden = YES;
329-
}else if (self.animatingState==Stable)
330-
{
341+
} else if (self.animatingState==Stable) {
331342
[cell contentView].hidden = NO;
332343
[cell animatedContent].alpha = 1;
333344
}
@@ -364,4 +375,29 @@ - (void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view {
364375
view.layer.anchorPoint = anchorPoint;
365376
}
366377

378+
- (Direction)directionForPresentation {
379+
//Logic will be added
380+
switch (self.presentationType) {
381+
case YALPresenationTypeRightToLeft:
382+
return right;
383+
break;
384+
case YALPresenationTypeLeftToRight:
385+
return left;
386+
break;
387+
}
388+
}
389+
390+
- (BOOL)clockwiseRotationForPresentation {
391+
//Logic will be added
392+
Direction direction = [self directionForPresentation];
393+
switch (direction) {
394+
case left:
395+
return YES;
396+
break;
397+
default:
398+
return NO;
399+
break;
400+
}
401+
}
402+
367403
@end

0 commit comments

Comments
 (0)