iOS --- 解决RESideMenu中所有界面都能侧滑的问题

RESideMenu是iOS中非常好用的一个侧滑布局类库, 很多iOS项目都会用到此类的左右侧滑效果.

You can add menu view controllers on both left and right sides of your content view controller.

基本的使用

RESideMenu的使用非常简单, 在启动RootViewController中引入并继承RESideMenu及其RESideMenuDelegate.
头文件:

// RootViewController.h
#import "RESideMenu.h"
@interface RootViewController : RESideMenu <RESideMenuDelegate>
@end

然后, 在RootViewController.m文件中设置好RESideMenu:

//  RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end

@implementation RootViewController

- (void)viewDidLoad {
  [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

- (void)awakeFromNib {
  self.menuPreferredStatusBarStyle = UIStatusBarStyleLightContent;
  self.contentViewShadowColor = [UIColor blackColor];
  self.contentViewShadowOffset = CGSizeMake(0, 0);
  self.contentViewShadowOpacity = 0.6;
  self.contentViewShadowRadius = 12;
  self.contentViewShadowEnabled = NO;

  self.contentViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentViewController"];
  self.leftMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LeftMenuViewController"];

  self.delegate = self;
}

#pragma mark - RESideMenu Delegate

- (void)sideMenu:(RESideMenu *)sideMenu willShowMenuViewController:(UIViewController *)menuViewController {
}

- (void)sideMenu:(RESideMenu *)sideMenu didShowMenuViewController:(UIViewController *)menuViewController {
}

- (void)sideMenu:(RESideMenu *)sideMenu willHideMenuViewController:(UIViewController *)menuViewController {
}

- (void)sideMenu:(RESideMenu *)sideMenu didHideMenuViewController:(UIViewController *)menuViewController {
}
@end

注意其中的awakeFromNib方法, 设置了RESideMenu的
contentViewController和leftMenuViewController, 这里的ViewController都是通过storyboard来加载的.
到此, RESideMenu即可在项目中自由运用了.

带来的问题

然而, 在RESideMenu的使用过程中, 发现其默认将所有界面都加入了侧滑功能. 如: 我们的导航Menu可以放在屏幕左右两侧, 侧滑可将其显示出来. 但是, 当我们进入到某个次级View中, 甚至更深一层的View中, 侧滑功能仍然可用. 这一点就与UINavigationController有了冲突:
1. 左滑的操作, 预期是返回UINavigationController的上一级View, 却变成了左侧Menu.
2. 在一些编辑文字等的View中, 左滑同样显示出来的是左侧Menu, 则已编辑的文字内容无法保存.

对于以上的问题2, 使用下边的方法可禁止navigationController的左滑效果:

self.navigationController.interactivePopGestureRecognizer.enabled = YES;
  self.navigationController.interactivePopGestureRecognizer.delegate = nil;

但, RESideMenu的左滑效果没有禁止.

解决方法

以上是我在项目中遇到的情况, 不知道是不是使用的姿势不对?
以下是解决方案:
RESideMenu类中有一个BOOL属性panGestureEnabled, 可以将其视为侧滑效果的开关.
那么我们可以通过控制这个属性来解决上边的问题. 这里采用通知的方式:
依然是在.m文件中,

// RootViewController.m
- (void)viewDidLoad {
  [super viewDidLoad];

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(disableRESideMenu) name:@"disableRESideMenu" object:nil];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enableRESideMenu) name:@"enableRESideMenu" object:nil];
}

- (void)enableRESideMenu {
  self.panGestureEnabled = YES;
}

- (void)disableRESideMenu {
  self.panGestureEnabled = NO;
}

在需要禁止侧滑的时候调用如下代码即可:

// 关闭侧滑效果
[[NSNotificationCenter defaultCenter] postNotificationName:@"disableRESideMenu" object:self userInfo:nil];
// 开启侧滑效果
[[NSNotificationCenter defaultCenter] postNotificationName:@"enableRESideMenu" object:self userInfo:nil];

感兴趣的小伙伴, 可以试一试.

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值