如何把菜单放在MVYSideMenu的右边?

时间:2014-03-27 10:07:03

标签: objective-c

我想使用MVYSideMenu 对于我的新iOS项目。它完全符合我的要求 - 除了一件事。我希望侧面菜单从屏幕右侧出来 - 而不是左侧

我尝试更改菜单框架 - 但它不会改变或破坏它的滑动部分。如何让它从右边出来?

1 个答案:

答案 0 :(得分:0)

试试这个,我在现有的 MVYSideMenu 菜单中添加了更改,从屏幕右侧出来,

按照步骤

1)在AppDelegate.m文件中添加代码

- 导入这些文件,“MVYMenuViewController.h”,“MVYSideMenuController.h”,“MVYSideMenuOptions.h”

- 在 didFinishLaunchingWithOptions

中添加代码
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOption{

  self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

 MVYMenuViewController *menuVC = [[MVYMenuViewController alloc] initWithNibName:@"MVYMenuViewController" bundle:nil];
 self.homeViewController = [HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
UINavigationController *contentNavigationController = [[UINavigationController alloc] initWithRootViewController: self.homeViewController];
MVYSideMenuOptions *options = [[MVYSideMenuOptions alloc] init];
options.contentViewScale = 1.0;
options.contentViewOpacity = 0.5;
options.shadowOpacity = 0.2;

MVYSideMenuController *sideMenuController = [[MVYSideMenuController alloc] initWithMenuViewController:menuVC                                                                                 contentViewController:contentNavigationController                                                                                                  options:options];

sideMenuController.menuFrame = CGRectMake(0, 0, self.window.bounds.size.width * 0.75, self.window.bounds.size.height);

self.window.rootViewController = sideMenuController;
[self.window makeKeyAndVisible];
return YES;
}

2)** MVYSideMenuController.m 仅限文件中的更改

分享 MVYSideMenuController.m 功能只需替换为现有代码。

1)功能的第一次变化

 -(BOOL)isMenuOpen {
  return self.menuContainerView.frame.origin.x == 
  self.view.frame.size.width-self.menuContainerView.frame.size.width; 
 //320- Added width as per screen size.
 } 

2)功能的第二次变化

- (CGFloat)closedOriginX {
return  self.menuFrame.size.width+200;
 }

3)功能的第三次变化

 - (CGRect)applyTranslation:(CGPoint)translation toFrame:(CGRect)frame {

CGFloat newOrigin = frame.origin.x;
newOrigin += translation.x;
CGFloat minOrigin = self.view.frame.size.width-
self.menuContainerView.frame.size.width;//320- Added width as per screen size.
 CGFloat maxOrigin = [self closedOriginX];
 CGRect newFrame = frame;

 if (newOrigin < minOrigin) {
    newOrigin = minOrigin;
 } else if (newOrigin > maxOrigin) {
    newOrigin = maxOrigin;
 }
 newFrame.origin.x = newOrigin;
 return newFrame;
}

4)功能的第四个变化

 -(void)openMenuWithVelocity:(CGFloat)velocity 
 {
 CGFloat menuXOrigin = self.menuContainerView.frame.origin.x;
 CGFloat finalXOrigin = self.view.frame.size.width-
self.menuContainerView.frame.size.width;//320- Added width as per 
 screen size.
 CGRect frame = self.menuContainerView.frame;
frame.origin.x = finalXOrigin;
NSTimeInterval duration;
if (velocity == 0.0f) {
    duration = self.options.animationDuration;
 } else {
    duration = fabs(menuXOrigin - finalXOrigin) / velocity;
    duration = fmax(0.1, fmin(1.0f, duration));
 }
 [self addShadowToMenuView];
  [UIView animateWithDuration:duration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
    self.menuContainerView.frame = frame;
    self.opacityView.layer.opacity = self.options.contentViewOpacity;
    [self.contentContainerView setTransform:CGAffineTransformMakeScale(self.options.contentViewScale, self.options.contentViewScale)];
  } completion:^(BOOL finished) {
    [self disableContentInteraction];
 }];
 }

在HomeViewControlle中添加以下代码

-In.h文件导入“MVYSideMenuController.h”文件,

@property (strong, nonatomic) MVYSideMenuController *slideMenu;

-In.m文件添加以下代码,

 -(void)viewDidLoad
 {
 [super viewDidLoad];
// Do any additional setup after loading the view from its nib.
 self.navigationItem.title = @"MVYSideMenu!";
 [self addLeftMenuButtonWithImage:[UIImage imageNamed:@"menu_icon"]];
 }

按照上述步骤,您将看到右侧菜单正确打开。

enter image description here

enter image description here

希望这可以帮助某人使用 MVYSideMenu

在右侧打开菜单