IOS之左右滑动效果的实现——利用SWRevealViewController

滑畅
2023-12-01

SWRevealViewController 是一个比较好的实现Sliding功能的第三方类。

今天跟大家分享一下如何使用SWRevealViewController实现左右菜单滑动功能。

首先,在工程中创建三个类,依次为 RootViewController、LeftViewController、RightViewController。

在工程中添加SWRevealViewController类以及所需要的图片。


在AppDelegate中添加如下代码:

RootViewController* rootViewController = [[RootViewController alloc] init];
    UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    LeftViewController* leftViewController = [[LeftViewController alloc] init];
    RightViewController* rightViewController = [[RightViewController alloc] init];
    
    SWRevealViewController* revealViewController = [[SWRevealViewController alloc] initWithRearViewController:leftViewController frontViewController:navigationController];
    [revealViewController setRightViewController:rightViewController];
    
    self.window.rootViewController = revealViewController;

然后在RootViewController中添加按钮触发事件:

SWRevealViewController *revealController = self.revealViewController;
    UIButton* leftButton = [self navigationButtonWithTitle:nil image:[UIImage imageNamed:@"ico_home_menu"] target:revealController action:@selector(revealToggle:)];
    UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    UIButton* rightButton = [self navigationButtonWithTitle:nil image:[UIImage imageNamed:@"ico_home_menu"] target:revealController action:@selector(rightRevealToggle:)];
    UIBarButtonItem* rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButton;

这就完成了利用SWRevealViewController实现滑动效果的全部代码。




 类似资料: