iOS --- 解決RESideMenu中所有介面都能側滑的問題

來源:互聯網
上載者:User

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 @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];

感興趣的小夥伴, 可以試一試.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.