您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 實戰:使用 PPRevealViewController 實現側邊欄

編輯:iOS 開發綜合

定義 PPRevealViewController

PPRevealSideViewControllerDelegate

@property (strong, nonatomic) PPRevealSideViewController *revealSideViewController;

初始化

HomeViewController *con = [[HomeViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];

_revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:nav];
_revealSideViewController.delegate = self;
_revealSideViewController.options = PPRevealSideOptionsNone; // 中間界面無陰影
_revealSideViewController.bouncingOffset = 0;
// navBar 和內容都支持滑動彈出側邊欄
_revealSideViewController.panInteractionsWhenClosed = PPRevealSideInteractionNavigationBar | PPRevealSideInteractionContentView;

self.window.rootViewController = _revealSideViewController;

// 只支持左滑
- (PPRevealSideDirection)pprevealSideViewController:(PPRevealSideViewController *)controller directionsAllowedForPanningOnView:(UIView *)view {
    return PPRevealSideDirectionLeft;
}

使用方式

在主頁中添加側邊欄(預加載側邊欄控制器):

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    SideViewController *side = [[SideViewController alloc] init];
    [self.revealSideViewController preloadViewController:side forSide:PPRevealSideDirectionLeft];
}

進入側邊欄:

SideViewController *c = [[SideViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:c];
[self.revealSideViewController pushViewController:nav onDirection:PPRevealSideDirectionLeft withOffset:70 animated:YES];

從側邊欄彈回上一個視圖(self 為側邊欄):

[self.revealSideViewController popViewControllerAnimated:YES];

從其他頁面進入主頁:

HomeViewController *c = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *n = [[UINavigationController alloc] initWithRootViewController:c];
[self.revealSideViewController popViewControllerWithNewCenterController:n animated:YES];

從其他頁面進入側邊欄:

HomeViewController *con = [[HomeViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:con];
[self.revealSideViewController popViewControllerWithNewCenterController:nav animated:YES completion:^{
    SideViewController *c = [[SideViewController alloc] initWithNibName:nil bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:c];
    [self.revealSideViewController pushViewController:nav onDirection:PPRevealSideDirectionLeft withOffset:70 animated:YES];
}];

後記:限定屏幕滑動範圍

當需要限定只在螢幕固定範圍內滑動才觸發側邊欄時,可以在 gestureRecognizer:shouldReceiveTouch: 方法的 return 之前加入判斷:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    CGPoint point = [touch locationInView:self.view];
    if (point.x > 100) {
        if (!_wasClosed) {
            return YES;
        } else {
            return NO;
        }
    }
    return YES;
}

透過判斷觸摸點的 x 坐標,就能讓側邊欄只在指定的螢幕區域內響應滑動手勢,避免與其他交互衝突。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved